Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 25088 invoked from network); 15 Sep 2002 19:26:58 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 15 Sep 2002 19:26:58 -0000 Received: (qmail 14455 invoked by uid 97); 15 Sep 2002 19:27:32 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 14396 invoked by uid 97); 15 Sep 2002 19:27:31 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 14385 invoked by uid 97); 15 Sep 2002 19:27:30 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 15 Sep 2002 19:26:45 -0000 Message-ID: <20020915192645.79871.qmail@icarus.apache.org> From: scolebourne@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/lang ToStringBuilder.java ToStringBuilderTest.java ToStringStyle.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N scolebourne 2002/09/15 12:26:45 Modified: lang ToStringBuilder.java ToStringBuilderTest.java ToStringStyle.java Log: Added code and tests in preparation for moving to commons proper Revision Changes Path 1.3 +143 -9 jakarta-commons-sandbox/lang/ToStringBuilder.java Index: ToStringBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/lang/ToStringBuilder.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ToStringBuilder.java 8 Sep 2002 15:25:04 -0000 1.2 +++ ToStringBuilder.java 15 Sep 2002 19:26:45 -0000 1.3 @@ -52,6 +52,8 @@ * . */ +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collection; import java.util.Map; @@ -95,11 +97,15 @@ public class ToStringBuilder { /** + * The default style of output to use + */ + private static ToStringStyle defaultStyle; + /** * Current toString buffer */ private final StringBuffer buffer; /** - * Is it full detail + * The style of output to use */ private final ToStringStyle style; /** @@ -109,21 +115,23 @@ /** * Constructor for ToStringBuilder. - * This constructor outputs using a default style. + * This constructor outputs using the default style set with + * setDefaultStyle. * * @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, null, null); + this(object, getDefaultStyle(), null); } /** * Constructor for ToStringBuilder specifying the output style. + *

+ * If the style is null, the default style is used. * * @param object the object to build a toString for, must not be null - * @param style the style of the toString to create - * @param buffer the string buffer to populate + * @param style the style of the toString to create, may be null * @throws IllegalArgumentException if the object passed in is null */ public ToStringBuilder(Object object, ToStringStyle style) { @@ -132,10 +140,13 @@ /** * Constructor for ToStringBuilder. + *

+ * If the style is null, the default style is used. + * If the buffer is null, a new one is created. * * @param object the object to build a toString for, must not be null - * @param style the style of the toString to create - * @param buffer the string buffer to populate + * @param style the style of the toString to create, may be null + * @param buffer the string buffer to populate, may be null * @throws IllegalArgumentException if the object passed in is null */ public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) { @@ -159,6 +170,120 @@ //---------------------------------------------------------------------------- /** + * Gets the default style to use. + *

+ * This could allow the toString style to be controlled for an entire + * application with one call. This might be used to have a verbose toString + * during development and a compact toString in production. + * + * @return the default toString style + */ + public static ToStringStyle getDefaultStyle() { + return defaultStyle; + } + + /** + * Sets the default style to use. + * + * @param style the default toString style + * @throws IllegalArgumentException if the style is null + */ + public static void setDefaultStyle(ToStringStyle style) { + if (style == null) { + throw new IllegalArgumentException("The style must not be null"); + } + defaultStyle = style; + } + + //------------------------------------------------------------------------- + + /** + * This method uses reflection to build a suitable toString using the default style. + *

+ * It uses Field.setAccessible to gain access to private fields. This means + * that it will throw a security exception if run under a security manger, if + * the permissions are not set up. + * It is also not as efficient as testing explicitly. + * Transient members will be not be included, as they are likely derived + * fields, and not part of the value of the object. + * + * @param object the object to be output + * @return the String result + * @throws IllegalArgumentException if the object is null + */ + public static String reflectionToString(Object object) { + return reflectionToString(object, null, false); + } + + /** + * This method uses reflection to build a suitable toString. + *

+ * It uses Field.setAccessible to gain access to private fields. This means + * that it will throw a security exception if run under a security manger, if + * the permissions are not set up. + * It is also not as efficient as testing explicitly. + * Transient members will be not be included, as they are likely derived + * fields, and not part of the value of the object. + *

+ * If the style is null, the default style is used. + * + * @param object the object to be output + * @param style the style of the toString to create, may be null + * @return the String result + * @throws IllegalArgumentException if the object or style is null + */ + public static String reflectionToString(Object object, ToStringStyle style) { + return reflectionToString(object, style, false); + } + + /** + * This method uses reflection to build a suitable toString. + *

+ * It uses Field.setAccessible to gain access to private fields. This means + * that it will throw a security exception if run under a security manger, if + * the permissions are not set up. + * It is also not as efficient as testing explicitly. + * If the outputTransients parameter is set to true, transient members will be + * output, otherwise they are ignored, as they are likely derived fields, and + * not part of the value of the object. + *

+ * If the style is null, the default style is used. + * + * @param object the object to be output + * @param style the style of the toString to create, may be null + * @param outputTransients whether to include transient fields + * @return the String result + * @throws IllegalArgumentException if the object or style is null + */ + public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) { + if (object == null) { + throw new IllegalArgumentException("The object must not be null"); + } + if (style == null) { + style = getDefaultStyle(); + } + Field[] fields = object.getClass().getDeclaredFields(); + Field.setAccessible(fields, true); + ToStringBuilder builder = new ToStringBuilder(object, style); + for (int i = 0; i < fields.length; ++i) { + Field f = fields[i]; + if (outputTransients || !Modifier.isTransient(f.getModifiers())) { + try { + builder.append(f.getName(), f.get(object)); + + } catch (IllegalAccessException ex) { + //this can't happen. Would get a Security exception instead + //throw a runtime exception in case the impossible happens. + throw new InternalError("Unexpected IllegalAccessException"); + } + } + } + return builder.toString(); + } + + //---------------------------------------------------------------------------- + + /** * Append to the toString an Object value. * * @param value the value to add to the toString @@ -776,7 +901,16 @@ //---------------------------------------------------------------------------- /** - * Return the built toString + * Gets the buffer being populated + * + * @return the StringBuffer being populated + */ + public StringBuffer getStringBuffer() { + return buffer; + } + + /** + * Returns the built toString * * @return the String toString */ 1.3 +199 -99 jakarta-commons-sandbox/lang/ToStringBuilderTest.java Index: ToStringBuilderTest.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/lang/ToStringBuilderTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ToStringBuilderTest.java 8 Sep 2002 15:25:04 -0000 1.2 +++ ToStringBuilderTest.java 15 Sep 2002 19:26:45 -0000 1.3 @@ -52,9 +52,52 @@ public void testConstructorEx2() { try { + new ToStringBuilder(null, null); + + } catch (IllegalArgumentException ex) { + try { + new ToStringBuilder(base, null); + + } catch (Exception ex2) { + fail(); + } + return; + } + fail(); + } + + public void testConstructorEx3() { + try { new ToStringBuilder(null, null, null); } catch (IllegalArgumentException ex) { + try { + new ToStringBuilder(base, null, null); + new ToStringBuilder(base, ToStringStyle.DEFAULT_STYLE, null); + + } catch (Exception ex2) { + fail(); + } + return; + } + fail(); + } + + public void testGetSetDefault() { + try { + ToStringBuilder.setDefaultStyle(ToStringStyle.NO_FIELD_NAMES_STYLE); + assertSame(ToStringStyle.NO_FIELD_NAMES_STYLE, ToStringBuilder.getDefaultStyle()); + } finally { + // reset for other tests + ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE); + } + } + + public void testSetDefaultEx() { + try { + ToStringBuilder.setDefaultStyle(null); + + } catch (IllegalArgumentException ex) { return; } fail(); @@ -125,102 +168,159 @@ assertEquals(baseStr + "[a=true,b=false]", new ToStringBuilder(base).append("a", true).append("b", false).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()); -// } -// + + public void testObjectArray() { + Object[] array = new Object[] {null, base, new int[] {3, 6}}; + assertEquals(baseStr + "[{,5,{3,6}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{,5,{3,6}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", 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 + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testIntArray() { + int[] array = new int[] {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 + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testShortArray() { + short[] array = new short[] {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 + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testByteArray() { + byte[] array = new byte[] {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 + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testCharArray() { + char[] array = new char[] {'A', '2', '_', 'D'}; + assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{A,2,_,D}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testDoubleArray() { + double[] array = new double[] {1.0, 2.9876, -3.00001, 4.3}; + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testFloatArray() { + float[] array = new float[] {1.0f, 2.9876f, -3.00001f, 4.3f}; + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{1.0,2.9876,-3.00001,4.3}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testBooleanArray() { + boolean[] array = new boolean[] {true, false, false}; + assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{true,false,false}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + + public void testLongArrayArray() { + long[][] array = new long[][] {{1, 2}, null, {5}}; + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testIntArrayArray() { + int[][] array = new int[][] {{1, 2}, null, {5}}; + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testShortArrayArray() { + short[][] array = new short[][] {{1, 2}, null, {5}}; + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testByteArrayArray() { + byte[][] array = new byte[][] {{1, 2}, null, {5}}; + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{1,2},,{5}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testCharArrayArray() { + char[][] array = new char[][] {{'A', 'B'}, null, {'p'}}; + assertEquals(baseStr + "[{{A,B},,{p}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{A,B},,{p}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testDoubleArrayArray() { + double[][] array = new double[][] {{1.0, 2.29686}, null, {Double.NaN}}; + assertEquals(baseStr + "[{{1.0,2.29686},,{NaN}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{1.0,2.29686},,{NaN}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testFloatArrayArray() { + float[][] array = new float[][] {{1.0f, 2.29686f}, null, {Float.NaN}}; + assertEquals(baseStr + "[{{1.0,2.29686},,{NaN}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{1.0,2.29686},,{NaN}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + + public void testBooleanArrayArray() { + boolean[][] array = new boolean[][] {{true, false}, null, {false}}; + assertEquals(baseStr + "[{{true,false},,{false}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[{{true,false},,{false}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals(baseStr + "[]", new ToStringBuilder(base).append(array).toString()); + assertEquals(baseStr + "[]", new ToStringBuilder(base).append((Object) array).toString()); + } + } 1.2 +597 -143 jakarta-commons-sandbox/lang/ToStringStyle.java Index: ToStringStyle.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/lang/ToStringStyle.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ToStringStyle.java 8 Sep 2002 15:25:04 -0000 1.1 +++ ToStringStyle.java 15 Sep 2002 19:26:45 -0000 1.2 @@ -89,7 +89,7 @@ /** * The no field names toString style. */ - public static final ToStringStyle XML_STYLE = new XMLToStringStyle(); +// public static final ToStringStyle XML_STYLE = new XMLToStringStyle(); /** * The content start '[' @@ -221,29 +221,61 @@ } } else if (value instanceof long[]) { - appendDetail(buffer, fieldName, (long[]) value); + if (detail) { + appendDetail(buffer, fieldName, (long[]) value); + } else { + appendSummary(buffer, fieldName, (long[]) value); + } + + } else if (value instanceof int[]) { + if (detail) { + appendDetail(buffer, fieldName, (int[]) value); + } else { + appendSummary(buffer, fieldName, (int[]) value); + } + + } else if (value instanceof short[]) { + if (detail) { + appendDetail(buffer, fieldName, (short[]) value); + } else { + appendSummary(buffer, fieldName, (short[]) value); + } -// } else if (item instanceof int[]) { -// appendDetail(buffer, fieldName, (int[]) item); -// -// } else if (item instanceof short[]) { -// appendDetail(buffer, fieldName, (short[]) item); -// -// } else if (item instanceof byte[]) { -// appendDetail(buffer, fieldName, (byte[]) item); -// -// } else if (item instanceof char[]) { -// appendDetail(buffer, fieldName, (char[]) item); -// -// } else if (item instanceof double[]) { -// appendDetail(buffer, fieldName, (double[]) item); -// -// } else if (item instanceof float[]) { -// appendDetail(buffer, fieldName, (float[]) item); -// -// } else if (item instanceof boolean[]) { -// appendDetail(buffer, fieldName, (boolean[]) item); + } else if (value instanceof byte[]) { + if (detail) { + appendDetail(buffer, fieldName, (byte[]) value); + } else { + appendSummary(buffer, fieldName, (byte[]) value); + } + } else if (value instanceof char[]) { + if (detail) { + appendDetail(buffer, fieldName, (char[]) value); + } else { + appendSummary(buffer, fieldName, (char[]) value); + } + + } else if (value instanceof double[]) { + if (detail) { + appendDetail(buffer, fieldName, (double[]) value); + } else { + appendSummary(buffer, fieldName, (double[]) value); + } + + } else if (value instanceof float[]) { + if (detail) { + appendDetail(buffer, fieldName, (float[]) value); + } else { + appendSummary(buffer, fieldName, (float[]) value); + } + + } else if (value instanceof boolean[]) { + if (detail) { + appendDetail(buffer, fieldName, (boolean[]) value); + } else { + appendSummary(buffer, fieldName, (boolean[]) value); + } + } else if (value.getClass().isArray()) { if (detail) { appendDetail(buffer, fieldName, (Object[]) value); @@ -251,11 +283,12 @@ appendSummary(buffer, fieldName, (Object[]) value); } - } else if (detail) { - appendDetail(buffer, fieldName, value); - } else { - appendSummary(buffer, fieldName, value); + if (detail) { + appendDetail(buffer, fieldName, (Object) value); + } else { + appendSummary(buffer, fieldName, (Object) value); + } } } @@ -360,6 +393,58 @@ //---------------------------------------------------------------------------- /** + * Append to the toString a short value. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param value the value to add to the toString + */ + public void append(StringBuffer buffer, String fieldName, short value) { + appendFieldStart(buffer, fieldName); + appendDetail(buffer, fieldName, value); + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString a short value. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param value the value to add to the toString + */ + protected void appendDetail(StringBuffer buffer, String fieldName, short value) { + buffer.append(value); + } + + //---------------------------------------------------------------------------- + + /** + * Append to the toString a byte value. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param value the value to add to the toString + */ + public void append(StringBuffer buffer, String fieldName, byte value) { + appendFieldStart(buffer, fieldName); + appendDetail(buffer, fieldName, value); + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString a byte value. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param value the value to add to the toString + */ + protected void appendDetail(StringBuffer buffer, String fieldName, byte value) { + buffer.append(value); + } + + //---------------------------------------------------------------------------- + + /** * Append to the toString a char value. * * @param buffer the StringBuffer to populate @@ -504,42 +589,8 @@ if (item == null) { appendNullText(buffer, fieldName); - } else if (item instanceof long[]) { - appendDetail(buffer, fieldName, (long[]) item); - -// } else if (item instanceof int[]) { -// appendDetail(buffer, fieldName, (int[]) item); -// -// } else if (item instanceof short[]) { -// appendDetail(buffer, fieldName, (short[]) item); -// -// } else if (item instanceof byte[]) { -// appendDetail(buffer, fieldName, (byte[]) item); -// -// } else if (item instanceof char[]) { -// appendDetail(buffer, fieldName, (char[]) item); -// -// } else if (item instanceof double[]) { -// appendDetail(buffer, fieldName, (double[]) item); -// -// } else if (item instanceof float[]) { -// appendDetail(buffer, fieldName, (float[]) item); -// -// } else if (item instanceof boolean[]) { -// appendDetail(buffer, fieldName, (boolean[]) item); - - } else if (item.getClass().isArray()) { - if (arrayContentDetail) { - appendDetail(buffer, fieldName, (Object[]) item); - } else { - appendSummary(buffer, fieldName, (Object[]) item); - } - - } else if (arrayContentDetail) { - appendDetail(buffer, fieldName, item); - } else { - appendSummary(buffer, fieldName, item); + appendInternal(buffer, fieldName, item, arrayContentDetail); } } buffer.append(arrayEnd); @@ -614,6 +665,391 @@ //---------------------------------------------------------------------------- /** + * Append to the toString an int array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param array the array to add to the toString + * @param fullDetail true for detail, false for summary info, null for style decides + */ + public void append(StringBuffer buffer, String fieldName, int[] array, Boolean fullDetail) { + appendFieldStart(buffer, fieldName); + + if (array == null) { + appendNullText(buffer, fieldName); + + } else if (isFullDetail(fullDetail)) { + appendDetail(buffer, fieldName, array); + + } else { + appendSummary(buffer, fieldName, array); + } + + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString the detail of an int array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendDetail(StringBuffer buffer, String fieldName, int[] array) { + buffer.append(arrayStart); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(arraySeparator); + } + appendDetail(buffer, fieldName, array[i]); + } + buffer.append(arrayEnd); + } + + /** + * Append to the toString a summary of an int array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendSummary(StringBuffer buffer, String fieldName, int[] array) { + appendSummarySize(buffer, fieldName, array.length); + } + + //---------------------------------------------------------------------------- + + /** + * Append to the toString a short array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param array the array to add to the toString + * @param fullDetail true for detail, false for summary info, null for style decides + */ + public void append(StringBuffer buffer, String fieldName, short[] array, Boolean fullDetail) { + appendFieldStart(buffer, fieldName); + + if (array == null) { + appendNullText(buffer, fieldName); + + } else if (isFullDetail(fullDetail)) { + appendDetail(buffer, fieldName, array); + + } else { + appendSummary(buffer, fieldName, array); + } + + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString the detail of a short array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendDetail(StringBuffer buffer, String fieldName, short[] array) { + buffer.append(arrayStart); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(arraySeparator); + } + appendDetail(buffer, fieldName, array[i]); + } + buffer.append(arrayEnd); + } + + /** + * Append to the toString a summary of a short array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendSummary(StringBuffer buffer, String fieldName, short[] array) { + appendSummarySize(buffer, fieldName, array.length); + } + + //---------------------------------------------------------------------------- + + /** + * Append to the toString a byte array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param array the array to add to the toString + * @param fullDetail true for detail, false for summary info, null for style decides + */ + public void append(StringBuffer buffer, String fieldName, byte[] array, Boolean fullDetail) { + appendFieldStart(buffer, fieldName); + + if (array == null) { + appendNullText(buffer, fieldName); + + } else if (isFullDetail(fullDetail)) { + appendDetail(buffer, fieldName, array); + + } else { + appendSummary(buffer, fieldName, array); + } + + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString the detail of a byte array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendDetail(StringBuffer buffer, String fieldName, byte[] array) { + buffer.append(arrayStart); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(arraySeparator); + } + appendDetail(buffer, fieldName, array[i]); + } + buffer.append(arrayEnd); + } + + /** + * Append to the toString a summary of a byte array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendSummary(StringBuffer buffer, String fieldName, byte[] array) { + appendSummarySize(buffer, fieldName, array.length); + } + + //---------------------------------------------------------------------------- + + /** + * Append to the toString a char array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param array the array to add to the toString + * @param fullDetail true for detail, false for summary info, null for style decides + */ + public void append(StringBuffer buffer, String fieldName, char[] array, Boolean fullDetail) { + appendFieldStart(buffer, fieldName); + + if (array == null) { + appendNullText(buffer, fieldName); + + } else if (isFullDetail(fullDetail)) { + appendDetail(buffer, fieldName, array); + + } else { + appendSummary(buffer, fieldName, array); + } + + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString the detail of a char array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendDetail(StringBuffer buffer, String fieldName, char[] array) { + buffer.append(arrayStart); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(arraySeparator); + } + appendDetail(buffer, fieldName, array[i]); + } + buffer.append(arrayEnd); + } + + /** + * Append to the toString a summary of a char array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendSummary(StringBuffer buffer, String fieldName, char[] array) { + appendSummarySize(buffer, fieldName, array.length); + } + + //---------------------------------------------------------------------------- + + /** + * Append to the toString a double array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param array the array to add to the toString + * @param fullDetail true for detail, false for summary info, null for style decides + */ + public void append(StringBuffer buffer, String fieldName, double[] array, Boolean fullDetail) { + appendFieldStart(buffer, fieldName); + + if (array == null) { + appendNullText(buffer, fieldName); + + } else if (isFullDetail(fullDetail)) { + appendDetail(buffer, fieldName, array); + + } else { + appendSummary(buffer, fieldName, array); + } + + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString the detail of a double array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendDetail(StringBuffer buffer, String fieldName, double[] array) { + buffer.append(arrayStart); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(arraySeparator); + } + appendDetail(buffer, fieldName, array[i]); + } + buffer.append(arrayEnd); + } + + /** + * Append to the toString a summary of a double array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendSummary(StringBuffer buffer, String fieldName, double[] array) { + appendSummarySize(buffer, fieldName, array.length); + } + + //---------------------------------------------------------------------------- + + /** + * Append to the toString a float array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param array the array to add to the toString + * @param fullDetail true for detail, false for summary info, null for style decides + */ + public void append(StringBuffer buffer, String fieldName, float[] array, Boolean fullDetail) { + appendFieldStart(buffer, fieldName); + + if (array == null) { + appendNullText(buffer, fieldName); + + } else if (isFullDetail(fullDetail)) { + appendDetail(buffer, fieldName, array); + + } else { + appendSummary(buffer, fieldName, array); + } + + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString the detail of a float array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendDetail(StringBuffer buffer, String fieldName, float[] array) { + buffer.append(arrayStart); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(arraySeparator); + } + appendDetail(buffer, fieldName, array[i]); + } + buffer.append(arrayEnd); + } + + /** + * Append to the toString a summary of a float array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendSummary(StringBuffer buffer, String fieldName, float[] array) { + appendSummarySize(buffer, fieldName, array.length); + } + + //---------------------------------------------------------------------------- + + /** + * Append to the toString a boolean array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name + * @param array the array to add to the toString + * @param fullDetail true for detail, false for summary info, null for style decides + */ + public void append(StringBuffer buffer, String fieldName, boolean[] array, Boolean fullDetail) { + appendFieldStart(buffer, fieldName); + + if (array == null) { + appendNullText(buffer, fieldName); + + } else if (isFullDetail(fullDetail)) { + appendDetail(buffer, fieldName, array); + + } else { + appendSummary(buffer, fieldName, array); + } + + appendFieldEnd(buffer, fieldName); + } + + /** + * Append to the toString the detail of a boolean array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendDetail(StringBuffer buffer, String fieldName, boolean[] array) { + buffer.append(arrayStart); + for (int i = 0; i < array.length; i++) { + if (i > 0) { + buffer.append(arraySeparator); + } + appendDetail(buffer, fieldName, array[i]); + } + buffer.append(arrayEnd); + } + + /** + * Append to the toString a summary of a boolean array. + * + * @param buffer the StringBuffer to populate + * @param fieldName the field name, typically not used as already appended + * @param array the array to add to the toString, not null + */ + protected void appendSummary(StringBuffer buffer, String fieldName, boolean[] array) { + appendSummarySize(buffer, fieldName, array.length); + } + + //---------------------------------------------------------------------------- + + /** * Append the class name. * * @param object the object whose name to output @@ -679,7 +1115,7 @@ * Append the field start to the buffer. * * @param buffer the StringBuffer to populate - * @param first is it the first field + * @param fieldName the field name */ protected void appendFieldStart(StringBuffer buffer, String fieldName) { if (fieldName != null) { @@ -692,7 +1128,7 @@ * Append the field end to the buffer. * * @param buffer the StringBuffer to populate - * @param fieldName the field name + * @param fieldName the field name, typically not used as already appended */ protected void appendFieldEnd(StringBuffer buffer, String fieldName) { appendFieldSeparator(buffer); @@ -713,10 +1149,9 @@ /** * Is this field to be output in full detail. - *

- * null defaults to full detail. * - * @param fullDetail the detail requested + * @param fullDetail the detail level requested + * @return whether full detail is to be shown */ protected boolean isFullDetail(Boolean fullDetailRequest) { if (fullDetailRequest == null) { @@ -740,27 +1175,35 @@ return name.substring(pos + 1); } - /** - * Return a toString - * - * @return debug toString - */ - public String toString() { - return "ToStringStyle"; - } - //---------------------------------------------------------------------------- /** * Default ToStringStyle */ - public static class DefaultToStringStyle extends ToStringStyle { + private static class DefaultToStringStyle extends ToStringStyle { + + /** + * Constructor - use the static constant rather than instantiating. + */ + private DefaultToStringStyle() { + super(); + } + } + //---------------------------------------------------------------------------- + /** * ToStringStyle that does not print out the field names */ - public static class NoFieldNameToStringStyle extends ToStringStyle { + private static class NoFieldNameToStringStyle extends ToStringStyle { + + /** + * Constructor - use the static constant rather than instantiating. + */ + private NoFieldNameToStringStyle() { + super(); + } /** * @see ToStringStyle#appendFieldStart(StringBuffer, String) @@ -771,12 +1214,21 @@ } + //---------------------------------------------------------------------------- + /** * ToStringStyle that outputs on multiple lines */ - public static class MultiLineToStringStyle extends ToStringStyle { + private static class MultiLineToStringStyle extends ToStringStyle { /** + * Constructor - use the static constant rather than instantiating. + */ + private MultiLineToStringStyle() { + super(); + } + + /** * @see ToStringStyle#appendContentStart(StringBuffer) */ protected void appendContentStart(StringBuffer buffer) { @@ -805,69 +1257,71 @@ } - /** - * ToStringStyle that outputs in XML style - */ - public static class XMLToStringStyle extends ToStringStyle { - - /** - * Constructor - */ - public XMLToStringStyle() { - super(); - nullText = "null"; - sizeStartText = "size="; - sizeEndText = ""; - } - - /** - * @see ToStringStyle#appendStart(StringBuffer, Object) - */ - public void appendStart(StringBuffer buffer, Object object) { - buffer.append('<'); - buffer.append(getShortClassName(object.getClass())); - buffer.append(" class=\""); - appendClassName(buffer, object); - buffer.append("\" hashCode=\""); - appendIdentityHashCode(buffer, object); - buffer.append("\">"); - buffer.append(SystemUtils.LINE_SEPARATOR); - buffer.append(" "); - } - - /** - * @see ToStringStyle#appendFieldStart(StringBuffer, String) - */ - protected void appendFieldStart(StringBuffer buffer, String fieldName) { - buffer.append('<'); - buffer.append(fieldName); - buffer.append('>'); - } - - /** - * @see ToStringStyle#appendFieldEnd(StringBuffer, String) - */ - protected void appendFieldEnd(StringBuffer buffer, String fieldName) { - buffer.append("'); - buffer.append(SystemUtils.LINE_SEPARATOR); - buffer.append(" "); - } - - /** - * @see ToStringStyle#appendEnd(StringBuffer, Object) - */ - public void appendEnd(StringBuffer buffer, Object object) { - int len = buffer.length(); - if (len > 2 && buffer.charAt(len - 1) == ' ' && buffer.charAt(len - 2) == ' ') { - buffer.setLength(len - 2); - } - buffer.append(""); - } - - } + //---------------------------------------------------------------------------- + +// /** +// * ToStringStyle that outputs in XML style +// */ +// private static class XMLToStringStyle extends ToStringStyle { +// +// /** +// * Constructor - use the static constant rather than instantiating. +// */ +// private XMLToStringStyle() { +// super(); +// nullText = "null"; +// sizeStartText = "size="; +// sizeEndText = ""; +// } +// +// /** +// * @see ToStringStyle#appendStart(StringBuffer, Object) +// */ +// public void appendStart(StringBuffer buffer, Object object) { +// buffer.append('<'); +// buffer.append(getShortClassName(object.getClass())); +// buffer.append(" class=\""); +// appendClassName(buffer, object); +// buffer.append("\" hashCode=\""); +// appendIdentityHashCode(buffer, object); +// buffer.append("\">"); +// buffer.append(SystemUtils.LINE_SEPARATOR); +// buffer.append(" "); +// } +// +// /** +// * @see ToStringStyle#appendFieldStart(StringBuffer, String) +// */ +// protected void appendFieldStart(StringBuffer buffer, String fieldName) { +// buffer.append('<'); +// buffer.append(fieldName); +// buffer.append('>'); +// } +// +// /** +// * @see ToStringStyle#appendFieldEnd(StringBuffer, String) +// */ +// protected void appendFieldEnd(StringBuffer buffer, String fieldName) { +// buffer.append("'); +// buffer.append(SystemUtils.LINE_SEPARATOR); +// buffer.append(" "); +// } +// +// /** +// * @see ToStringStyle#appendEnd(StringBuffer, Object) +// */ +// public void appendEnd(StringBuffer buffer, Object object) { +// int len = buffer.length(); +// if (len > 2 && buffer.charAt(len - 1) == ' ' && buffer.charAt(len - 2) == ' ') { +// buffer.setLength(len - 2); +// } +// buffer.append(""); +// } +// +// } } -- To unsubscribe, e-mail: For additional commands, e-mail: