You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/09/07 00:42:59 UTC
cvs commit: jakarta-commons-sandbox/lang ToStringBuilder.java ToStringBuilderTest.java
scolebourne 2002/09/06 15:42:59
Added: lang ToStringBuilder.java ToStringBuilderTest.java
Log:
Store in lang sandbox for the moment
Revision Changes Path
1.1 jakarta-commons-sandbox/lang/ToStringBuilder.java
Index: ToStringBuilder.java
===================================================================
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements 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/>.
*/
/**
* <code>ToString</code> generation routine.
* <p>
* This class enables a good toString to be built for any class. This class aims
* to simplify the process by:
* <ul>
* <li>allowing field names
* <li>handling all types consistently
* <li>handling nulls consistently
* <li>outputting arrays in the same way as Lists
* <li>enabling the detail level to be controlled for objects and collections
* </ul>
* <p>
* To use this class write code as follows:
* <code>
* public class Person {
* String name;
* int age;
* boolean isSmoker;
*
* ...
*
* public String toString() {
* return new ToStringBuilder(this).
* append(name, "name").
* append(age, "age").
* append(smoker, "smoker").
* toString();
* }
* }
* </code>
* This will produce a toString of the format:
* <code>Person@7f54[name=Stephen,age=29,smoker=false]</code>
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ToStringBuilder.java,v 1.1 2002/09/06 22:42:59 scolebourne Exp $
*/
public class ToStringBuilder {
/**
* Current toString buffer
*/
private final StringBuffer buffer = new StringBuffer(512);
/**
* Is it the first parameter
*/
private boolean first = true;
/**
* Constructor for ToStringBuilder.
* This constructor outputs full class names and the hashcode.
*
* @param object the object to build a toString for, must not be null
* @throws IllegalArgumentException if the object passed in is null
*/
public ToStringBuilder(Object object) {
this(object, true, true);
}
/**
* Constructor for ToStringBuilder.
*
* @param object the object to build a toString for, must not be null
* @param fullClassName whether to output the full, or a short class name
* @param identityHashCode whether to output the identity hashcode as in the default toString
* @throws IllegalArgumentException if the object passed in is null
*/
public ToStringBuilder(Object object, boolean fullClassName, boolean identityHashCode) {
super();
if (object == null) {
throw new IllegalArgumentException("The object to create a toString for must not be null");
}
if (fullClassName) {
buffer.append(object.getClass().getName());
} else {
String name = object.getClass().getName();
int pos = name.lastIndexOf('.');
if (pos >= 0) {
name = name.substring(pos + 1);
}
buffer.append(name);
}
if (identityHashCode) {
buffer.append('@');
buffer.append(Integer.toHexString(System.identityHashCode(object)));
buffer.append('[');
}
}
/**
* Append the basic info to the buffer.
*
* @param fieldName the field name
*/
private void appendBasics(String fieldName) {
if (first) {
first = false;
} else {
buffer.append(',');
}
if (fieldName != null) {
buffer.append(fieldName);
buffer.append('=');
}
}
/**
* Append to the toString an Object value, printing the full
* toString of the object passed in.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(Object object) {
return append(object, null, true);
}
/**
* Append to the toString an Object value, printing the full
* toString of the object passed in.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(Object object, String fieldName) {
return append(object, fieldName, true);
}
/**
* Append to the toString an Object value.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of the object. Setting false will output
* a <linked> indicator if non null, except for Collection, Map and
* Arrays where the size is output.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(Object object, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (object == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append(object);
} else if (object instanceof Collection) {
buffer.append("<size=");
buffer.append(((Collection) object).size());
buffer.append('>');
} else if (object instanceof Map) {
buffer.append("<size=");
buffer.append(((Map) object).size());
buffer.append('>');
} else if (object instanceof Object[]) {
buffer.append("<size=");
buffer.append(((Object[]) object).length);
buffer.append('>');
} else {
buffer.append("<linked>");
}
return this;
}
/**
* Append to the toString a long value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(long value) {
return append(value, null);
}
/**
* Append to the toString a long value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(long value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString an int value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(int value) {
return append(value, null);
}
/**
* Append to the toString an int value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(int value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString a short value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(short value) {
return append(value, null);
}
/**
* Append to the toString a short value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(short value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString a char value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(char value) {
return append(value, null);
}
/**
* Append to the toString a char value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(char value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString a byte value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(byte value) {
return append(value, null);
}
/**
* Append to the toString a byte value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(byte value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString a double value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(double value) {
return append(value, null);
}
/**
* Append to the toString a double value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(double value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString a float value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(float value) {
return append(value, null);
}
/**
* Append to the toString a float value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(float value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString a boolean value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(boolean value) {
return append(value, null);
}
/**
* Append to the toString a boolean value.
*
* @param value the value to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(boolean value, String fieldName) {
appendBasics(fieldName);
buffer.append(value);
return this;
}
/**
* Append to the toString an Object array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(Object[] array) {
return append(array, null, true);
}
/**
* Append to the toString an Object array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(Object[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString an Object array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(Object[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
buffer.append("<null>");
} else {
buffer.append(array[i]);
}
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a long array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(long[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for a long array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(long[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString a long array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(long[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a int array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(int[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for an int array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(int[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString an int array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(int[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a short array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(short[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for a short array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(short[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString a short array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(short[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a char array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(char[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for a char array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(char[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString a char array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(char[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a byte array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(byte[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for a byte array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(byte[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString a byte array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(byte[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a double array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(double[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for a double array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(double[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString a double array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(double[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a float array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(float[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for a float array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(float[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString a float array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(float[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Append to the toString a boolean array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @return this
*/
public ToStringBuilder append(boolean[] array) {
return append(array, null, true);
}
/**
* Append a hashCode for a boolean array.
* <p>
* The array will be output in full.
*
* @param array the array to add to the hashCode
* @return this
*/
public ToStringBuilder append(boolean[] array, String fieldName) {
return append(array, fieldName, true);
}
/**
* Append to the toString a boolean array.
* <p>
* A boolean parameter controls the level of detail to show. Setting true
* will output the full toString of each array element. Setting false will
* output the size of the array.
*
* @param array the array to add to the toString
* @param fieldName the field name
* @param fullDetail true for detail, false for summary info
* @return this
*/
public ToStringBuilder append(boolean[] array, String fieldName, boolean fullDetail) {
appendBasics(fieldName);
if (array == null) {
buffer.append("<null>");
} else if (fullDetail) {
buffer.append('[');
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
buffer.append(']');
} else {
buffer.append("<size=");
buffer.append(array.length);
buffer.append('>');
}
return this;
}
/**
* Return the built toString
*
* @return the String toString
*/
public String toString() {
buffer.append(']');
String result = buffer.toString();
buffer.setLength(buffer.length() - 1);
return result;
}
}
1.1 jakarta-commons-sandbox/lang/ToStringBuilderTest.java
Index: ToStringBuilderTest.java
===================================================================
import java.util.ArrayList;
import java.util.HashMap;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Unit tests {@link org.apache.commons.lang.ToStringBuilder}.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ToStringBuilderTest.java,v 1.1 2002/09/06 22:42:59 scolebourne Exp $
*/
public class ToStringBuilderTest extends TestCase {
private final Integer base = new Integer(5);
private final String baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base));
public ToStringBuilderTest(String name) {
super(name);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite(ToStringBuilderTest.class);
suite.setName("ToStringBuilder Tests");
return suite;
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
//-----------------------------------------------------------------------
public void testConstructorEx1() {
try {
new ToStringBuilder(null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testConstructorEx2() {
try {
new ToStringBuilder(null, false, false);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
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((Object) null, "a").toString());
assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append(i3, "a").toString());
assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append(i3, "a").append(i4, "b").toString());
assertEquals(baseStr + "[a=<linked>]", new ToStringBuilder(base).append(i3, "a", false).toString());
assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append(new ArrayList(), "a", false).toString());
assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append(new ArrayList(), "a", true).toString());
assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append(new HashMap(), "a", false).toString());
assertEquals(baseStr + "[a={}]", new ToStringBuilder(base).append(new HashMap(), "a", true).toString());
assertEquals(baseStr + "[a=<size=0>]", new ToStringBuilder(base).append(new String[0], "a", false).toString());
assertEquals(baseStr + "[a=[]]", new ToStringBuilder(base).append(new String[0], "a", true).toString());
}
public void testLong() {
assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString());
assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append(3L, "a").toString());
assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append(3L, "a").append(4L, "b").toString());
}
public void testInt() {
assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((int) 3).toString());
assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append((int) 3, "a").toString());
assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append((int) 3, "a").append((int) 4, "b").toString());
}
public void testShort() {
assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((short) 3).toString());
assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append((short) 3, "a").toString());
assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append((short) 3, "a").append((short) 4, "b").toString());
}
public void testChar() {
assertEquals(baseStr + "[A]", new ToStringBuilder(base).append((char) 65).toString());
assertEquals(baseStr + "[a=A]", new ToStringBuilder(base).append((char) 65, "a").toString());
assertEquals(baseStr + "[a=A,b=B]", new ToStringBuilder(base).append((char) 65, "a").append((char) 66, "b").toString());
}
public void testByte() {
assertEquals(baseStr + "[3]", new ToStringBuilder(base).append((byte) 3).toString());
assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append((byte) 3, "a").toString());
assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append((byte) 3, "a").append((byte) 4, "b").toString());
}
public void testDouble() {
assertEquals(baseStr + "[3.2]", new ToStringBuilder(base).append((double) 3.2).toString());
assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append((double) 3.2, "a").toString());
assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append((double) 3.2, "a").append((double) 4.3, "b").toString());
}
public void testFloat() {
assertEquals(baseStr + "[3.2]", new ToStringBuilder(base).append((float) 3.2).toString());
assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append((float) 3.2, "a").toString());
assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append((float) 3.2, "a").append((float) 4.3, "b").toString());
}
public void testBoolean() {
assertEquals(baseStr + "[true]", new ToStringBuilder(base).append(true).toString());
assertEquals(baseStr + "[a=true]", new ToStringBuilder(base).append(true, "a").toString());
assertEquals(baseStr + "[a=true,b=false]", new ToStringBuilder(base).append(true, "a").append(false, "b").toString());
}
// public void testObjectArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((Object[]) null).toHashCode());
// Object[] obj = new Object[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = new Object();
// assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = new Object();
// assertEquals( (17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testLongArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long[]) null).toHashCode());
// long[] obj = new long[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = 5L;
// int h1 = (int) (5L ^ (5L >> 32));
// assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = 6L;
// int h2 = (int) (6L ^ (6L >> 32));
// assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testIntArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int[]) null).toHashCode());
// int[] obj = new int[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = 5;
// assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = 6;
// assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testShortArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short[]) null).toHashCode());
// short[] obj = new short[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = (short) 5;
// assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = (short) 6;
// assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testCharArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((char[]) null).toHashCode());
// char[] obj = new char[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = (char) 5;
// assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = (char) 6;
// assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testByteArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte[]) null).toHashCode());
// byte[] obj = new byte[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = (byte) 5;
// assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = (byte) 6;
// assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testDoubleArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((double[]) null).toHashCode());
// double[] obj = new double[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = 5.4d;
// long l1 = Double.doubleToLongBits(5.4d);
// int h1 = (int) (l1 ^ (l1 >> 32));
// assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = 6.3d;
// long l2 = Double.doubleToLongBits(6.3d);
// int h2 = (int) (l2 ^ (l2 >> 32));
// assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testFloatArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float[]) null).toHashCode());
// float[] obj = new float[2];
// assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = 5.4f;
// int h1 = Float.floatToIntBits(5.4f);
// assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = 6.3f;
// int h2 = Float.floatToIntBits(6.3f);
// assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
// public void testBooleanArray() {
// assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((boolean[]) null).toHashCode());
// boolean[] obj = new boolean[2];
// assertEquals((17 * 37 + 1) * 37 + 1 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[0] = true;
// assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// obj[1] = false;
// assertEquals( (17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
// }
//
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>