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/08 17:25:04 UTC
cvs commit: jakarta-commons-sandbox/lang ToStringStyle.java ToStringBuilder.java ToStringBuilderTest.java
scolebourne 2002/09/08 08:25:04
Modified: lang ToStringBuilder.java ToStringBuilderTest.java
Added: lang ToStringStyle.java
Log:
Updated ToStringBuilder for pluggable output and nulti dimensional array handling
Revision Changes Path
1.2 +198 -333 jakarta-commons-sandbox/lang/ToStringBuilder.java
Index: ToStringBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/ToStringBuilder.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ToStringBuilder.java 6 Sep 2002 22:42:59 -0000 1.1
+++ ToStringBuilder.java 8 Sep 2002 15:25:04 -0000 1.2
@@ -1,9 +1,3 @@
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Map;
-
-
-
/* ====================================================================
* The Apache Software License, Version 1.1
*
@@ -58,6 +52,9 @@
* <http://www.apache.org/>.
*/
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Map;
/**
* <code>ToString</code> generation routine.
* <p>
@@ -100,130 +97,105 @@
/**
* Current toString buffer
*/
- private final StringBuffer buffer = new StringBuffer(512);
+ private final StringBuffer buffer;
+ /**
+ * Is it full detail
+ */
+ private final ToStringStyle style;
/**
- * Is it the first parameter
+ * The object being output
*/
- private boolean first = true;
+ private final Object object;
/**
* Constructor for ToStringBuilder.
- * This constructor outputs full class names and the hashcode.
+ * This constructor outputs using a default style.
*
* @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);
+ this(object, null, null);
+ }
+
+ /**
+ * Constructor for ToStringBuilder specifying the output style.
+ *
+ * @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
+ * @throws IllegalArgumentException if the object passed in is null
+ */
+ public ToStringBuilder(Object object, ToStringStyle style) {
+ this(object, style, null);
}
/**
* 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
+ * @param style the style of the toString to create
+ * @param buffer the string buffer to populate
* @throws IllegalArgumentException if the object passed in is null
*/
- public ToStringBuilder(Object object, boolean fullClassName, boolean identityHashCode) {
+ public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) {
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 (style == null) {
+ style = ToStringStyle.DEFAULT_STYLE;
}
- if (identityHashCode) {
- buffer.append('@');
- buffer.append(Integer.toHexString(System.identityHashCode(object)));
- buffer.append('[');
+ if (buffer == null) {
+ buffer = new StringBuffer(512);
}
+ this.buffer = buffer;
+ this.style = style;
+ this.object = object;
+
+ style.appendStart(buffer, object);
}
- /**
- * 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.
+ * Append to the toString an Object value.
*
* @param value the value to add to the toString
* @return this
*/
public ToStringBuilder append(Object object) {
- return append(object, null, true);
+ style.append(buffer, null, object, null);
+ return this;
}
/**
- * Append to the toString an Object value, printing the full
- * toString of the object passed in.
+ * Append to the toString an Object value.
*
* @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);
+ public ToStringBuilder append(String fieldName, Object object) {
+ style.append(buffer, fieldName, object, null);
+ return this;
}
/**
* 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>");
- }
+ public ToStringBuilder append(String fieldName, Object object, boolean fullDetail) {
+ style.append(buffer, fieldName, object, new Boolean(fullDetail));
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString a long value.
*
@@ -231,7 +203,8 @@
* @return this
*/
public ToStringBuilder append(long value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -241,12 +214,13 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(long value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, long value) {
+ style.append(buffer, fieldName, value);
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString an int value.
*
@@ -254,7 +228,8 @@
* @return this
*/
public ToStringBuilder append(int value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -264,12 +239,13 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(int value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, int value) {
+ style.append(buffer, fieldName, value);
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString a short value.
*
@@ -277,7 +253,8 @@
* @return this
*/
public ToStringBuilder append(short value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -287,12 +264,13 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(short value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, short value) {
+ style.append(buffer, fieldName, value);
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString a char value.
*
@@ -300,7 +278,8 @@
* @return this
*/
public ToStringBuilder append(char value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -310,12 +289,13 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(char value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, char value) {
+ style.append(buffer, fieldName, value);
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString a byte value.
*
@@ -323,7 +303,8 @@
* @return this
*/
public ToStringBuilder append(byte value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -333,12 +314,13 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(byte value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, byte value) {
+ style.append(buffer, fieldName, value);
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString a double value.
*
@@ -346,7 +328,8 @@
* @return this
*/
public ToStringBuilder append(double value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -356,12 +339,13 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(double value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, double value) {
+ style.append(buffer, fieldName, value);
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString a float value.
*
@@ -369,7 +353,8 @@
* @return this
*/
public ToStringBuilder append(float value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -379,12 +364,13 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(float value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, float value) {
+ style.append(buffer, fieldName, value);
return this;
}
+ //----------------------------------------------------------------------------
+
/**
* Append to the toString a boolean value.
*
@@ -392,7 +378,8 @@
* @return this
*/
public ToStringBuilder append(boolean value) {
- return append(value, null);
+ style.append(buffer, null, value);
+ return this;
}
/**
@@ -402,522 +389,400 @@
* @param fieldName the field name
* @return this
*/
- public ToStringBuilder append(boolean value, String fieldName) {
- appendBasics(fieldName);
- buffer.append(value);
+ public ToStringBuilder append(String fieldName, boolean value) {
+ style.append(buffer, fieldName, 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);
+ style.append(buffer, null, array, null);
+ 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, String fieldName) {
- return append(array, fieldName, true);
+ public ToStringBuilder append(String fieldName, Object[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, long[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, int[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, short[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, char[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, byte[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, double[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, float[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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);
+ style.append(buffer, null, array, null);
+ return this;
}
/**
* 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);
+ public ToStringBuilder append(String fieldName, boolean[] array) {
+ style.append(buffer, fieldName, array, null);
+ return this;
}
/**
* 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.
+ * will output the array in full. Setting false will output a summary,
+ * typically 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('>');
- }
+ public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail) {
+ style.append(buffer, fieldName, array, new Boolean(fullDetail));
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;
+ style.appendEnd(buffer, object);
+ return buffer.toString();
}
}
1.2 +28 -28 jakarta-commons-sandbox/lang/ToStringBuilderTest.java
Index: ToStringBuilderTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/ToStringBuilderTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ToStringBuilderTest.java 6 Sep 2002 22:42:59 -0000 1.1
+++ ToStringBuilderTest.java 8 Sep 2002 15:25:04 -0000 1.2
@@ -52,7 +52,7 @@
public void testConstructorEx2() {
try {
- new ToStringBuilder(null, false, false);
+ new ToStringBuilder(null, null, null);
} catch (IllegalArgumentException ex) {
return;
@@ -65,64 +65,64 @@
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());
+ 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(3L, "a").toString());
- assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append(3L, "a").append(4L, "b").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 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());
+ assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (int) 3).toString());
+ assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (int) 3).append("b", (int) 4).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());
+ assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (short) 3).toString());
+ assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (short) 3).append("b", (short) 4).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());
+ assertEquals(baseStr + "[a=A]", new ToStringBuilder(base).append("a", (char) 65).toString());
+ assertEquals(baseStr + "[a=A,b=B]", new ToStringBuilder(base).append("a", (char) 65).append("b", (char) 66).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());
+ assertEquals(baseStr + "[a=3]", new ToStringBuilder(base).append("a", (byte) 3).toString());
+ assertEquals(baseStr + "[a=3,b=4]", new ToStringBuilder(base).append("a", (byte) 3).append("b", (byte) 4).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());
+ assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append("a", (double) 3.2).toString());
+ assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append("a", (double) 3.2).append("b", (double) 4.3).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());
+ assertEquals(baseStr + "[a=3.2]", new ToStringBuilder(base).append("a", (float) 3.2).toString());
+ assertEquals(baseStr + "[a=3.2,b=4.3]", new ToStringBuilder(base).append("a", (float) 3.2).append("b", (float) 4.3).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());
+ assertEquals(baseStr + "[a=true]", new ToStringBuilder(base).append("a", true).toString());
+ assertEquals(baseStr + "[a=true,b=false]", new ToStringBuilder(base).append("a", true).append("b", false).toString());
}
// public void testObjectArray() {
1.1 jakarta-commons-sandbox/lang/ToStringStyle.java
Index: ToStringStyle.java
===================================================================
/* ====================================================================
* 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/>.
*/
import java.util.Collection;
import java.util.Map;
import org.apache.commons.lang.SystemUtils;
/**
* <code>ToStringStyle</code> works with ToStringBuilder to create a
* toString.
* <p>
* These classes are intended to be used as singletons. There is no need
* to instantiate a new style each time.
* <P>
* This class can be subclassed to provide specific toString designs. The
* main public interface is via ToStringBuilder.
* <p>
* For simple changes, subclasses can change the fields of this class. For
* more complex changes, the methods can be overridden.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ToStringStyle.java,v 1.1 2002/09/08 15:25:04 scolebourne Exp $
*/
public abstract class ToStringStyle {
/**
* 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 no field names toString style.
*/
public static final ToStringStyle XML_STYLE = new XMLToStringStyle();
/**
* The content start '['
*/
protected String contentStart = "[";
/**
* The content end ']'
*/
protected String contentEnd = "]";
/**
* The field name value separator '='
*/
protected String fieldNameValueSeparator = "=";
/**
* The field separator ','
*/
protected String fieldSeparator = ",";
/**
* The array start '{'
*/
protected String arrayStart = "{";
/**
* The array separator ','
*/
protected String arraySeparator = ",";
/**
* The detail for array content
*/
protected boolean arrayContentDetail = true;
/**
* The array end '}'
*/
protected String arrayEnd = "}";
/**
* The value to use when fullDetail is null 'true'
*/
protected boolean defaultFullDetail = true;
/**
* The null text '<null>'
*/
protected String nullText = "<null>";
/**
* The summary size text start '<size'
*/
protected String sizeStartText = "<size=";
/**
* The summary size text start '>'
*/
protected String sizeEndText = ">";
//----------------------------------------------------------------------------
/**
* Constructor.
*/
public ToStringStyle() {
super();
}
//----------------------------------------------------------------------------
/**
* Append the start of data indicator.
*
* @param buffer the StringBuffer to populate
* @param object the object to build a toString for, must not be null
*/
public void appendStart(StringBuffer buffer, Object object) {
appendClassName(buffer, object);
appendIdentityHashCode(buffer, object);
appendContentStart(buffer);
}
/**
* Append the end of data indicator.
*
* @param buffer the StringBuffer to populate
* @param object the object to build a toString for, must not be null
*/
public void appendEnd(StringBuffer buffer, Object object) {
appendContentEnd(buffer);
}
//----------------------------------------------------------------------------
/**
* Append to the toString an Object value, printing the full
* toString of the object passed in.
*
* @param buffer the StringBuffer to populate
* @param fieldName the field name
* @param value the value 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, Object value, Boolean fullDetail) {
appendFieldStart(buffer, fieldName);
if (value == null) {
appendNullText(buffer, fieldName);
} else {
appendInternal(buffer, fieldName, value, isFullDetail(fullDetail));
}
appendFieldEnd(buffer, fieldName);
}
/**
* Append to the toString an Object, correctly interpretting its type.
*
* @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, not null
* @param detail output detail or not
*/
protected void appendInternal(StringBuffer buffer, String fieldName, Object value, boolean detail) {
if (value instanceof Collection) {
if (detail) {
appendDetail(buffer, fieldName, (Collection) value);
} else {
appendSummarySize(buffer, fieldName, ((Collection) value).size());
}
} else if (value instanceof Map) {
if (detail) {
appendDetail(buffer, fieldName, (Map) value);
} else {
appendSummarySize(buffer, fieldName, ((Map) value).size());
}
} else if (value instanceof long[]) {
appendDetail(buffer, fieldName, (long[]) 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.getClass().isArray()) {
if (detail) {
appendDetail(buffer, fieldName, (Object[]) value);
} else {
appendSummary(buffer, fieldName, (Object[]) value);
}
} else if (detail) {
appendDetail(buffer, fieldName, value);
} else {
appendSummary(buffer, fieldName, value);
}
}
/**
* Append to the toString an Object value, printing the full detail of the object.
*
* @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, not null
*/
protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
buffer.append(value);
}
/**
* Append to the toString a Collection.
*
* @param buffer the StringBuffer to populate
* @param fieldName the field name, typically not used as already appended
* @param coll the collection to add to the toString, not null
*/
protected void appendDetail(StringBuffer buffer, String fieldName, Collection coll) {
buffer.append(coll);
}
/**
* Append to the toString a Map.
*
* @param buffer the StringBuffer to populate
* @param fieldName the field name, typically not used as already appended
* @param map the maps to add to the toString, not null
*/
protected void appendDetail(StringBuffer buffer, String fieldName, Map map) {
buffer.append(map);
}
/**
* Append to the toString an Object value, printing a summary of the object.
*
* @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, not null
*/
protected void appendSummary(StringBuffer buffer, String fieldName, Object value) {
buffer.append('<');
buffer.append(getShortClassName(value.getClass()));
buffer.append('>');
}
//----------------------------------------------------------------------------
/**
* Append to the toString a long 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, long value) {
appendFieldStart(buffer, fieldName);
appendDetail(buffer, fieldName, value);
appendFieldEnd(buffer, fieldName);
}
/**
* Append to the toString a long 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, long value) {
buffer.append(value);
}
//----------------------------------------------------------------------------
/**
* Append to the toString an int 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, int value) {
appendFieldStart(buffer, fieldName);
appendDetail(buffer, fieldName, value);
appendFieldEnd(buffer, fieldName);
}
/**
* Append to the toString an int 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, int value) {
buffer.append(value);
}
//----------------------------------------------------------------------------
/**
* Append to the toString a char 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, char value) {
appendFieldStart(buffer, fieldName);
appendDetail(buffer, fieldName, value);
appendFieldEnd(buffer, fieldName);
}
/**
* Append to the toString a char 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, char value) {
buffer.append(value);
}
//----------------------------------------------------------------------------
/**
* Append to the toString a double 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, double value) {
appendFieldStart(buffer, fieldName);
appendDetail(buffer, fieldName, value);
appendFieldEnd(buffer, fieldName);
}
/**
* Append to the toString a double 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, double value) {
buffer.append(value);
}
//----------------------------------------------------------------------------
/**
* Append to the toString a float 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, float value) {
appendFieldStart(buffer, fieldName);
appendDetail(buffer, fieldName, value);
appendFieldEnd(buffer, fieldName);
}
/**
* Append to the toString a float 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, float value) {
buffer.append(value);
}
//----------------------------------------------------------------------------
/**
* Append to the toString a boolean 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, boolean value) {
appendFieldStart(buffer, fieldName);
appendDetail(buffer, fieldName, value);
appendFieldEnd(buffer, fieldName);
}
/**
* Append to the toString a boolean 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, boolean value) {
buffer.append(value);
}
/**
* Append to the toString an Object 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, Object[] 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 Object 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, Object[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
Object item = array[i];
if (i > 0) {
buffer.append(arraySeparator);
}
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);
}
}
buffer.append(arrayEnd);
}
/**
* Append to the toString a summary of an Object 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, Object[] array) {
appendSummarySize(buffer, fieldName, array.length);
}
//----------------------------------------------------------------------------
/**
* Append to the toString a long 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, long[] 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 long 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, long[] 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 long 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, long[] array) {
appendSummarySize(buffer, fieldName, array.length);
}
//----------------------------------------------------------------------------
/**
* Append the class name.
*
* @param object the object whose name to output
*/
protected void appendClassName(StringBuffer buffer, Object object) {
buffer.append(object.getClass().getName());
}
/**
* Append the IdentityHashCode.
*
* @param object the object whose id to output
*/
protected void appendIdentityHashCode(StringBuffer buffer, Object object) {
buffer.append('@');
buffer.append(Integer.toHexString(System.identityHashCode(object)));
}
/**
* Append the content start to the buffer.
*
* @param buffer the StringBuffer to populate
*/
protected void appendContentStart(StringBuffer buffer) {
buffer.append(contentStart);
}
/**
* Append the content end to the buffer.
*
* @param buffer the StringBuffer to populate
*/
protected void appendContentEnd(StringBuffer buffer) {
int len = buffer.length();
int sepLen = fieldSeparator.length();
if (len > 0 && sepLen > 0 && len >= sepLen && buffer.charAt(len - 1) == fieldSeparator.charAt(sepLen - 1)) {
buffer.setLength(len - sepLen);
}
buffer.append(contentEnd);
}
/**
* Append an indicator for null to the buffer.
* Default output is '<null>'.
*
* @param buffer the StringBuffer to populate
* @param fieldName the field name, typically not used as already appended
*/
protected void appendNullText(StringBuffer buffer, String fieldName) {
buffer.append(nullText);
}
/**
* Append the field separator to the buffer.
*
* @param buffer the StringBuffer to populate
*/
protected void appendFieldSeparator(StringBuffer buffer) {
buffer.append(fieldSeparator);
}
/**
* Append the field start to the buffer.
*
* @param buffer the StringBuffer to populate
* @param first is it the first field
*/
protected void appendFieldStart(StringBuffer buffer, String fieldName) {
if (fieldName != null) {
buffer.append(fieldName);
buffer.append(fieldNameValueSeparator);
}
}
/**
* Append the field end to the buffer.
*
* @param buffer the StringBuffer to populate
* @param fieldName the field name
*/
protected void appendFieldEnd(StringBuffer buffer, String fieldName) {
appendFieldSeparator(buffer);
}
/**
* Append to the toString a size summary.
*
* @param buffer the StringBuffer to populate
* @param fieldName the field name, typically not used as already appended
* @param size the size to append
*/
protected void appendSummarySize(StringBuffer buffer, String fieldName, int size) {
buffer.append(sizeStartText);
buffer.append(size);
buffer.append(sizeEndText);
}
/**
* Is this field to be output in full detail.
* <p>
* null defaults to full detail.
*
* @param fullDetail the detail requested
*/
protected boolean isFullDetail(Boolean fullDetailRequest) {
if (fullDetailRequest == null) {
return defaultFullDetail;
}
return fullDetailRequest.booleanValue();
}
/**
* Gets the short class name for a class.
*
* @param cls the class to get the short name of
* @return the short name
*/
protected String getShortClassName(Class cls) {
String name = cls.getName();
int pos = name.lastIndexOf('.');
if (pos == -1) {
return name;
}
return name.substring(pos + 1);
}
/**
* Return a toString
*
* @return debug toString
*/
public String toString() {
return "ToStringStyle";
}
//----------------------------------------------------------------------------
/**
* Default ToStringStyle
*/
public static class DefaultToStringStyle extends ToStringStyle {
}
/**
* ToStringStyle that does not print out the field names
*/
public static class NoFieldNameToStringStyle extends ToStringStyle {
/**
* @see ToStringStyle#appendFieldStart(StringBuffer, String)
*/
protected void appendFieldStart(StringBuffer buffer, String fieldName) {
// do nothing
}
}
/**
* ToStringStyle that outputs on multiple lines
*/
public static class MultiLineToStringStyle extends ToStringStyle {
/**
* @see ToStringStyle#appendContentStart(StringBuffer)
*/
protected void appendContentStart(StringBuffer buffer) {
super.appendContentStart(buffer);
appendFieldSeparator(buffer);
}
/**
* @see ToStringStyle#appendFieldSeparator(StringBuffer)
*/
protected void appendFieldSeparator(StringBuffer buffer) {
buffer.append(SystemUtils.LINE_SEPARATOR);
buffer.append(" ");
}
/**
* @see ToStringStyle#appendContentEnd(StringBuffer)
*/
protected void appendContentEnd(StringBuffer buffer) {
int len = buffer.length();
if (len > 2 && buffer.charAt(len - 1) == ' ' && buffer.charAt(len - 2) == ' ') {
buffer.setLength(len - 2);
}
buffer.append(contentEnd);
}
}
/**
* 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(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("</");
buffer.append(getShortClassName(object.getClass()));
buffer.append("\">");
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>