Add ToStringStyle.SHORT_PREFIX_STYLE as discussed on commons-dev.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137712 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2003-11-25 00:53:55 +00:00
parent 9ebdce7348
commit 4db1390786
2 changed files with 205 additions and 2 deletions

View File

@ -86,7 +86,7 @@
* @author Gary Gregory
* @author Pete Gieser
* @since 1.0
* @version $Id: ToStringStyle.java,v 1.27 2003/08/23 00:21:49 ggregory Exp $
* @version $Id: ToStringStyle.java,v 1.28 2003/11/25 00:53:55 ggregory Exp $
*/
public abstract class ToStringStyle implements Serializable {
@ -94,14 +94,22 @@ public abstract class ToStringStyle implements Serializable {
* 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.
*/
@ -111,14 +119,17 @@ public abstract class ToStringStyle implements Serializable {
* 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 @@ public abstract class ToStringStyle implements Serializable {
* 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>'&lt;null&gt;'</code>.
*/
private String nullText = "<null>";
/**
* The summary size text start <code>'<size'</code>.
*/
private String sizeStartText = "<size=";
/**
* The summary size text start <code>'&gt;'</code>.
*/
private String sizeEndText = ">";
/**
* The summary object text start <code>'&lt;'</code>.
*/
private String summaryObjectStartText = "<";
/**
* The summary object text start <code>'&gt;'</code>.
*/
@ -2030,6 +2056,36 @@ private Object readResolve() {
//----------------------------------------------------------------------------
/**
* <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
* classname, identity hashcode, content start or field name.</p>

View File

@ -0,0 +1,147 @@
/* ====================================================================
* 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 apache@apache.org.
*
* 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 {@link org.apache.commons.lang.builder.ToStringStyle.SHORT_PREFIX_STYLE}.
*
* @author <a href="mailto:ggregory@seagullsw.com">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("Integer@8888[]").toString());
assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString());
assertEquals(baseStr + "[a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString());
assertEquals(baseStr + "[<null>,a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<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());
}
}