ggregory 2003/11/24 16:53:55
Modified: lang/src/java/org/apache/commons/lang/builder
ToStringStyle.java
Added: lang/src/test/org/apache/commons/lang/builder
ShortPrefixToStringStyleTest.java
Log:
Add ToStringStyle.SHORT_PREFIX_STYLE as discussed on commons-dev.
Revision Changes Path
1.28 +58 -2
jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringStyle.java
Index: ToStringStyle.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringStyle.java,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -r1.27 -r1.28
--- ToStringStyle.java 23 Aug 2003 00:21:49 -0000 1.27
+++ ToStringStyle.java 25 Nov 2003 00:53:55 -0000 1.28
@@ -94,31 +94,42 @@
* The default toString style.
*/
public static final ToStringStyle DEFAULT_STYLE = new DefaultToStringStyle();
+
/**
* The multi line toString style.
*/
public static final ToStringStyle MULTI_LINE_STYLE = new
MultiLineToStringStyle();
+
/**
* The no field names toString style.
*/
public static final ToStringStyle NO_FIELD_NAMES_STYLE = new
NoFieldNameToStringStyle();
+
+ /**
+ * The short prefix toString style.
+ */
+ public static final ToStringStyle SHORT_PREFIX_STYLE = new
ShortPrefixToStringStyle();
+
/**
* The simple toString style.
*/
public static final ToStringStyle SIMPLE_STYLE = new SimpleToStringStyle();
-
+
/**
* Whether to use the field names, the default is <code>true</code>.
*/
private boolean useFieldNames = true;
+
/**
* Whether to use the class name, the default is <code>true</code>.
*/
private boolean useClassName = true;
+
/**
* Whether to use short class names, the default is <code>false</code>.
*/
private boolean useShortClassName = false;
+
/**
* Whether to use the identity hash code, the default is <code>true</code>.
*/
@@ -128,63 +139,78 @@
* The content start <code>'['</code>.
*/
private String contentStart = "[";
+
/**
* The content end <code>']'</code>.
*/
private String contentEnd = "]";
+
/**
* The field name value separator <code>'='</code>.
*/
private String fieldNameValueSeparator = "=";
+
/**
* Whether the field separator should be added before any other fields.
*/
private boolean fieldSeparatorAtStart = false;
+
/**
* Whether the field separator should be added after any other fields.
*/
private boolean fieldSeparatorAtEnd = false;
+
/**
* The field separator <code>','</code>.
*/
private String fieldSeparator = ",";
+
/**
* The array start <code>'{'</code>.
*/
private String arrayStart = "{";
+
/**
* The array separator <code>','</code>.
*/
private String arraySeparator = ",";
+
/**
* The detail for array content.
*/
private boolean arrayContentDetail = true;
+
/**
* The array end <code>'}'</code>.
*/
private String arrayEnd = "}";
+
/**
* The value to use when fullDetail is <code>null</code>,
* the default value is <code>true</code>.
*/
private boolean defaultFullDetail = true;
+
/**
* The <code>null</code> text <code>'<null>'</code>.
*/
private String nullText = "<null>";
+
/**
* The summary size text start <code>'<size'</code>.
*/
private String sizeStartText = "<size=";
+
/**
* The summary size text start <code>'>'</code>.
*/
private String sizeEndText = ">";
+
/**
* The summary object text start <code>'<'</code>.
*/
private String summaryObjectStartText = "<";
+
/**
* The summary object text start <code>'>'</code>.
*/
@@ -2029,6 +2055,36 @@
}
//----------------------------------------------------------------------------
+
+ /**
+ * <p><code>ToStringStyle</code> that prints out the short
+ * class name and no identity hashcode.</p>
+ *
+ * <p>This is an inner class rather than using
+ * <code>StandardToStringStyle</code> to ensure its immutability.</p>
+ */
+ private static final class ShortPrefixToStringStyle extends ToStringStyle {
+
+ /**
+ * <p>Constructor.</p>
+ *
+ * <p>Use the static constant rather than instantiating.</p>
+ */
+ private ShortPrefixToStringStyle() {
+ super();
+ this.setUseShortClassName(true);
+ this.setUseIdentityHashCode(false);
+ }
+
+ /**
+ * <p>Ensure <code>Singleton</ode> after serialization.</p>
+ * @return the singleton
+ */
+ private Object readResolve() {
+ return ToStringStyle.SHORT_PREFIX_STYLE;
+ }
+
+ }
/**
* <p><code>ToStringStyle</code> that does not print out the
1.1
jakarta-commons/lang/src/test/org/apache/commons/lang/builder/ShortPrefixToStringStyleTest.java
Index: ShortPrefixToStringStyleTest.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.builder;
import java.util.ArrayList;
import java.util.HashMap;
import junit.framework.TestCase;
/**
* Unit tests [EMAIL PROTECTED]
org.apache.commons.lang.builder.ToStringStyle.SHORT_PREFIX_STYLE}.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Gary Gregory</a>
* @version $Id: ShortPrefixToStringStyleTest.java,v 1.1 2003/11/25 00:53:55
ggregory Exp $
*/
public class ShortPrefixToStringStyleTest extends TestCase {
private final Integer base = new Integer(5);
private final String baseStr = "Integer";
protected void setUp() throws Exception {
super.setUp();
ToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE);
}
protected void tearDown() throws Exception {
super.tearDown();
ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
}
//----------------------------------------------------------------
public void testBlank() {
assertEquals(baseStr + "[]", new ToStringBuilder(base).toString());
}
public void testAppendSuper() {
assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("[EMAIL
PROTECTED]").toString());
assertEquals(baseStr + "[<null>]", new
ToStringBuilder(base).appendSuper("[EMAIL PROTECTED]<null>]").toString());
assertEquals(baseStr + "[a=hello]", new
ToStringBuilder(base).appendSuper("[EMAIL PROTECTED]").append("a",
"hello").toString());
assertEquals(baseStr + "[<null>,a=hello]", new
ToStringBuilder(base).appendSuper("[EMAIL PROTECTED]<null>]").append("a",
"hello").toString());
assertEquals(baseStr + "[a=hello]", new
ToStringBuilder(base).appendSuper(null).append("a", "hello").toString());
}
public void testObject() {
Integer i3 = new Integer(3);
Integer i4 = new Integer(4);
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object)
null).toString());
assertEquals(baseStr + "[3]", new
ToStringBuilder(base).append(i3).toString());
assertEquals(baseStr + "[a=<null>]", new ToStringBuilder(base).append("a",
(Object) null).toString());
assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a",
i3).toString());
assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a",
i3).append("b", i4).toString());
assertEquals(baseStr + "[a=<Integer>]", new
ToStringBuilder(base).append("a", i3, false).toString());
assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a",
new ArrayList(), false).toString());
assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append("a", new
ArrayList(), true).toString());
assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a",
new HashMap(), false).toString());
assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a", new
HashMap(), true).toString());
assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append("a",
(Object) new String[0], false).toString());
assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append("a",
(Object) new String[0], true).toString());
}
public void testLong() {
assertEquals(baseStr + "[3]", new
ToStringBuilder(base).append(3L).toString());
assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a",
3L).toString());
assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a",
3L).append("b", 4L).toString());
}
public void testObjectArray() {
Object[] array = new Object[] {null, base, new int[] {3, 6}};
assertEquals(baseStr + "[{<null>,5,{3,6}}]", new
ToStringBuilder(base).append(array).toString());
assertEquals(baseStr + "[{<null>,5,{3,6}}]", new
ToStringBuilder(base).append((Object) array).toString());
array = null;
assertEquals(baseStr + "[<null>]", new
ToStringBuilder(base).append(array).toString());
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object)
array).toString());
}
public void testLongArray() {
long[] array = new long[] {1, 2, -3, 4};
assertEquals(baseStr + "[{1,2,-3,4}]", new
ToStringBuilder(base).append(array).toString());
assertEquals(baseStr + "[{1,2,-3,4}]", new
ToStringBuilder(base).append((Object) array).toString());
array = null;
assertEquals(baseStr + "[<null>]", new
ToStringBuilder(base).append(array).toString());
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object)
array).toString());
}
public void testLongArrayArray() {
long[][] array = new long[][] {{1, 2}, null, {5}};
assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new
ToStringBuilder(base).append(array).toString());
assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new
ToStringBuilder(base).append((Object) array).toString());
array = null;
assertEquals(baseStr + "[<null>]", new
ToStringBuilder(base).append(array).toString());
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object)
array).toString());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]