com.groupbyinc.common.apache.commons.lang3.builder.ToStringStyle Maven / Gradle / Ivy
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.lang3.builder; import java.io.Serializable; import java.lang.reflect.Array; import java.util.Collection; import java.util.Map; import java.util.WeakHashMap; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.SystemUtils; /** *. */ private String sizeEndText = ">"; /** * The summary object text startControls
* *Stringformatting for {@link ToStringBuilder}. * The main public interface is always viaToStringBuilder.These classes are intended to be used as
* *Singletons. * There is no need to instantiate a new style each time. A program * will generally use one of the predefined constants on this class. * Alternatively, the {@link StandardToStringStyle} class can be used * to set the individual settings. Thus most styles can be achieved * without subclassing.If required, a subclass can override as many or as few of the * methods as it requires. Each object type (from
boolean* tolongtoObjecttoint[]) has * its own methods to output it. Most have two versions, detail and summary. * *For example, the detail version of the array based methods will * output the whole array, whereas the summary method will just output * the array length.
* *If you want to format the output of certain objects, such as dates, you * must create a subclass and override a method. *
** public class MyStyle extends ToStringStyle { * protected void appendDetail(StringBuffer buffer, String fieldName, Object value) { * if (value instanceof Date) { * value = new SimpleDateFormat("yyyy-MM-dd").format(value); * } * buffer.append(value); * } * } ** * @since 1.0 * @version $Id: ToStringStyle.java 1654143 2015-01-23 08:45:21Z britter $ */ public abstract class ToStringStyle implements Serializable { /** * Serialization version ID. */ private static final long serialVersionUID = -2587890625525655916L; /** * The default toString style. Using thePerson* example from {@link ToStringBuilder}, the output would look like this: * ** Person@182f0db[name=John Doe,age=33,smoker=false] **/ public static final ToStringStyle DEFAULT_STYLE = new DefaultToStringStyle(); /** * The multi line toString style. Using thePerson* example from {@link ToStringBuilder}, the output would look like this: * ** Person@182f0db[ * name=John Doe * age=33 * smoker=false * ] **/ public static final ToStringStyle MULTI_LINE_STYLE = new MultiLineToStringStyle(); /** * The no field names toString style. Using the *Personexample from {@link ToStringBuilder}, the output * would look like this: * ** Person@182f0db[John Doe,33,false] **/ public static final ToStringStyle NO_FIELD_NAMES_STYLE = new NoFieldNameToStringStyle(); /** * The short prefix toString style. Using thePersonexample * from {@link ToStringBuilder}, the output would look like this: * ** Person[name=John Doe,age=33,smoker=false] ** * @since 2.1 */ public static final ToStringStyle SHORT_PREFIX_STYLE = new ShortPrefixToStringStyle(); /** * The simple toString style. Using thePerson* example from {@link ToStringBuilder}, the output would look like this: * ** John Doe,33,false **/ public static final ToStringStyle SIMPLE_STYLE = new SimpleToStringStyle(); /** * The no class name toString style. Using thePerson* example from {@link ToStringBuilder}, the output would look like this: * ** [name=John Doe,age=33,smoker=false] ** * @since 3.4 */ public static final ToStringStyle NO_CLASS_NAME_STYLE = new NoClassNameToStringStyle(); /** * The JSON toString style. Using thePersonexample from * {@link ToStringBuilder}, the output would look like this: * ** {"name": "John Doe", "age": 33, "smoker": true} ** * Note: Since field names are mandatory in JSON, this * ToStringStyle will throw an {@link UnsupportedOperationException} if no * field name is passed in while appending. Furthermore This ToStringStyle * will only generate valid JSON if referenced objects also produce JSON * when calling {@code toString()} on them. * * @since 3.4 * @see json.org */ public static final ToStringStyle JSON_STYLE = new JsonToStringStyle(); /** ** A registry of objects used by
*/ private static final ThreadLocalreflectionToStringmethods * to detect cyclical object references and avoid infinite loops. *> REGISTRY = new ThreadLocal >(); /* * Note that objects of this class are generally shared between threads, so * an instance variable would not be suitable here. * * In normal use the registry should always be left empty, because the caller * should call toString() which will clean up. * * See LANG-792 */ /** * * Returns the registry of objects being traversed by the
* * @return Set the registry of objects being traversed */ static MapreflectionToString* methods in the current thread. *'<'. */ private String summaryObjectStartText = "<"; /** * The summary object text start'>'. */ private String summaryObjectEndText = ">"; //---------------------------------------------------------------------------- /** *Constructor.
*/ protected ToStringStyle() { super(); } //---------------------------------------------------------------------------- /** *Append to the
*toStringthe superclass toString.NOTE: It assumes that the toString has been created from the same ToStringStyle.
* *A
* * @param buffer thenullsuperToStringis ignored.StringBufferto populate * @param superToString thesuper.toString()* @since 2.0 */ public void appendSuper(final StringBuffer buffer, final String superToString) { appendToString(buffer, superToString); } /** *Append to the
*toStringanother toString.NOTE: It assumes that the toString has been created from the same ToStringStyle.
* *A
* * @param buffer thenulltoStringis ignored.StringBufferto populate * @param toString the additionaltoString* @since 2.0 */ public void appendToString(final StringBuffer buffer, final String toString) { if (toString != null) { final int pos1 = toString.indexOf(contentStart) + contentStart.length(); final int pos2 = toString.lastIndexOf(contentEnd); if (pos1 != pos2 && pos1 >= 0 && pos2 >= 0) { final String data = toString.substring(pos1, pos2); if (fieldSeparatorAtStart) { removeLastFieldSeparator(buffer); } buffer.append(data); appendFieldSeparator(buffer); } } } /** *Append to the
* * @param buffer thetoStringthe start of data indicator.StringBufferto populate * @param object theObjectto build atoStringfor */ public void appendStart(final StringBuffer buffer, final Object object) { if (object != null) { appendClassName(buffer, object); appendIdentityHashCode(buffer, object); appendContentStart(buffer); if (fieldSeparatorAtStart) { appendFieldSeparator(buffer); } } } /** *Append to the
* * @param buffer thetoStringthe end of data indicator.StringBufferto populate * @param object theObjectto build a *toStringfor. */ public void appendEnd(final StringBuffer buffer, final Object object) { if (this.fieldSeparatorAtEnd == false) { removeLastFieldSeparator(buffer); } appendContentEnd(buffer); unregister(object); } /** *Remove the last field separator from the buffer.
* * @param buffer theStringBufferto populate * @since 2.0 */ protected void removeLastFieldSeparator(final StringBuffer buffer) { final int len = buffer.length(); final int sepLen = fieldSeparator.length(); if (len > 0 && sepLen > 0 && len >= sepLen) { boolean match = true; for (int i = 0; i < sepLen; i++) { if (buffer.charAt(len - 1 - i) != fieldSeparator.charAt(sepLen - 1 - i)) { match = false; break; } } if (match) { buffer.setLength(len - sepLen); } } } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringanObject* value, printing the fulltoStringof the *Objectpassed in.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString* @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final Object value, final Boolean fullDetail) { appendFieldStart(buffer, fieldName); if (value == null) { appendNullText(buffer, fieldName); } else { appendInternal(buffer, fieldName, value, isFullDetail(fullDetail)); } appendFieldEnd(buffer, fieldName); } /** *Append to the
* *toStringanObject, * correctly interpreting its type.This method performs the main lookup by Class type to correctly * route arrays,
* *Collections,Mapsand *Objectsto the appropriate method.Either detail or summary views can be specified.
* *If a cycle is detected, an object will be appended with the *
* * @param buffer theObject.toString()format.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString, * notnull* @param detail output detail or not */ protected void appendInternal(final StringBuffer buffer, final String fieldName, final Object value, final boolean detail) { if (isRegistered(value) && !(value instanceof Number || value instanceof Boolean || value instanceof Character)) { appendCyclicObject(buffer, fieldName, value); return; } register(value); try { 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[]) { 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 (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); } else { appendSummary(buffer, fieldName, (Object[]) value); } } else { if (detail) { appendDetail(buffer, fieldName, value); } else { appendSummary(buffer, fieldName, value); } } } finally { unregister(value); } } /** *Append to the
* * @param buffer thetoStringanObject* value that has been detected to participate in a cycle. This * implementation will print the standard string value of the value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString, * notnull* * @since 2.2 */ protected void appendCyclicObject(final StringBuffer buffer, final String fieldName, final Object value) { ObjectUtils.identityToString(buffer, value); } /** *Append to the
* * @param buffer thetoStringanObject* value, printing the full detail of theObject.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) { buffer.append(value); } /** *Append to the
* * @param buffer thetoStringaCollection.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param coll theCollectionto add to the *toString, notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection> coll) { buffer.append(coll); } /** *Append to the
* * @param buffer thetoStringaMap.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param map theMapto add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final Map, ?> map) { buffer.append(map); } /** *Append to the
* * @param buffer thetoStringanObject* value, printing a summary of theObject.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final Object value) { buffer.append(summaryObjectStartText); buffer.append(getShortClassName(value.getClass())); buffer.append(summaryObjectEndText); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringalong* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final long value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringalong* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final long value) { buffer.append(value); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringanint* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final int value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringanint* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final int value) { buffer.append(value); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringashort* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final short value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringashort* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final short value) { buffer.append(value); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringabyte* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final byte value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringabyte* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final byte value) { buffer.append(value); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringachar* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final char value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringachar* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final char value) { buffer.append(value); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringadouble* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final double value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringadouble* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final double value) { buffer.append(value); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringafloat* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final float value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringafloat* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final float value) { buffer.append(value); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringaboolean* value.StringBufferto populate * @param fieldName the field name * @param value the value to add to thetoString*/ public void append(final StringBuffer buffer, final String fieldName, final boolean value) { appendFieldStart(buffer, fieldName); appendDetail(buffer, fieldName, value); appendFieldEnd(buffer, fieldName); } /** *Append to the
* * @param buffer thetoStringaboolean* value.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param value the value to add to thetoString*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final boolean value) { buffer.append(value); } /** *Append to the
* * @param buffer thetoStringanObject* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to the toString * @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final Object[] array, final 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
* * @param buffer thetoStringthe detail of an *Objectarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final Object[] array) { buffer.append(arrayStart); for (int i = 0; i < array.length; i++) { final Object item = array[i]; if (i > 0) { buffer.append(arraySeparator); } if (item == null) { appendNullText(buffer, fieldName); } else { appendInternal(buffer, fieldName, item, arrayContentDetail); } } buffer.append(arrayEnd); } /** *Append to the
* * @param buffer thetoStringthe detail of an array type.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull* @since 2.0 */ protected void reflectionAppendArrayDetail(final StringBuffer buffer, final String fieldName, final Object array) { buffer.append(arrayStart); final int length = Array.getLength(array); for (int i = 0; i < length; i++) { final Object item = Array.get(array, i); if (i > 0) { buffer.append(arraySeparator); } if (item == null) { appendNullText(buffer, fieldName); } else { appendInternal(buffer, fieldName, item, arrayContentDetail); } } buffer.append(arrayEnd); } /** *Append to the
* * @param buffer thetoStringa summary of an *Objectarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final Object[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringalong* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to thetoString* @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final long[] array, final 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
* * @param buffer thetoStringthe detail of a *longarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of a *longarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final long[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringanint* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to thetoString* @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final int[] array, final 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
* * @param buffer thetoStringthe detail of an *intarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of an *intarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final int[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringashort* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to thetoString* @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final short[] array, final 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
* * @param buffer thetoStringthe detail of a *shortarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of a *shortarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final short[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringabyte* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to thetoString* @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final byte[] array, final 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
* * @param buffer thetoStringthe detail of a *bytearray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of a *bytearray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final byte[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringachar* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to thetoString* @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final char[] array, final 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
* * @param buffer thetoStringthe detail of a *chararray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of a *chararray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final char[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringadouble* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to the toString * @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final double[] array, final 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
* * @param buffer thetoStringthe detail of a *doublearray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of a *doublearray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final double[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringafloat* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to the toString * @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final float[] array, final 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
* * @param buffer thetoStringthe detail of a *floatarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of a *floatarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final float[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringaboolean* array.StringBufferto populate * @param fieldName the field name * @param array the array to add to the toString * @param fullDetailtruefor detail,false* for summary info,nullfor style decides */ public void append(final StringBuffer buffer, final String fieldName, final boolean[] array, final 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
* * @param buffer thetoStringthe detail of a *booleanarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendDetail(final StringBuffer buffer, final String fieldName, final 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
* * @param buffer thetoStringa summary of a *booleanarray.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param array the array to add to thetoString, * notnull*/ protected void appendSummary(final StringBuffer buffer, final String fieldName, final boolean[] array) { appendSummarySize(buffer, fieldName, array.length); } //---------------------------------------------------------------------------- /** *Append to the
* * @param buffer thetoStringthe class name.StringBufferto populate * @param object theObjectwhose name to output */ protected void appendClassName(final StringBuffer buffer, final Object object) { if (useClassName && object != null) { register(object); if (useShortClassName) { buffer.append(getShortClassName(object.getClass())); } else { buffer.append(object.getClass().getName()); } } } /** *Append the {@link System#identityHashCode(java.lang.Object)}.
* * @param buffer theStringBufferto populate * @param object theObjectwhose id to output */ protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) { if (this.isUseIdentityHashCode() && object!=null) { register(object); buffer.append('@'); buffer.append(Integer.toHexString(System.identityHashCode(object))); } } /** *Append to the
* * @param buffer thetoStringthe content start.StringBufferto populate */ protected void appendContentStart(final StringBuffer buffer) { buffer.append(contentStart); } /** *Append to the
* * @param buffer thetoStringthe content end.StringBufferto populate */ protected void appendContentEnd(final StringBuffer buffer) { buffer.append(contentEnd); } /** *Append to the
* *toStringan indicator fornull.The default indicator is
* * @param buffer the'<null>'.StringBufferto populate * @param fieldName the field name, typically not used as already appended */ protected void appendNullText(final StringBuffer buffer, final String fieldName) { buffer.append(nullText); } /** *Append to the
* * @param buffer thetoStringthe field separator.StringBufferto populate */ protected void appendFieldSeparator(final StringBuffer buffer) { buffer.append(fieldSeparator); } /** *Append to the
* * @param buffer thetoStringthe field start.StringBufferto populate * @param fieldName the field name */ protected void appendFieldStart(final StringBuffer buffer, final String fieldName) { if (useFieldNames && fieldName != null) { buffer.append(fieldName); buffer.append(fieldNameValueSeparator); } } /** *Append to the
* * @param buffer thetoStringthe field end.StringBufferto populate * @param fieldName the field name, typically not used as already appended */ protected void appendFieldEnd(final StringBuffer buffer, final String fieldName) { appendFieldSeparator(buffer); } /** *Append to the
* *toStringa size summary.The size summary is used to summarize the contents of *
* *Collections,Mapsand arrays.The output consists of a prefix, the passed in size * and a suffix.
* *The default format is
* * @param buffer the'<size=n>'.StringBufferto populate * @param fieldName the field name, typically not used as already appended * @param size the size to append */ protected void appendSummarySize(final StringBuffer buffer, final String fieldName, final int size) { buffer.append(sizeStartText); buffer.append(size); buffer.append(sizeEndText); } /** *Is this field to be output in full detail.
* *This method converts a detail request into a detail level. * The calling code may request full detail (
* * @param fullDetailRequest the detail level requested * @return whether full detail is to be shown */ protected boolean isFullDetail(final Boolean fullDetailRequest) { if (fullDetailRequest == null) { return defaultFullDetail; } return fullDetailRequest.booleanValue(); } /** *true), * but a subclass might ignore that and always return *false. The calling code may pass in *nullindicating that it doesn't care about * the detail level. In this case the default detail level is * used.Gets the short class name for a class.
* *The short class name is the classname excluding * the package name.
* * @param cls theClassto get the short name of * @return the short name */ protected String getShortClassName(final Class> cls) { return ClassUtils.getShortClassName(cls); } // Setters and getters for the customizable parts of the style // These methods are not expected to be overridden, except to make public // (They are not public so that immutable subclasses can be written) //--------------------------------------------------------------------- /** *Gets whether to use the class name.
* * @return the current useClassName flag */ protected boolean isUseClassName() { return useClassName; } /** *Sets whether to use the class name.
* * @param useClassName the new useClassName flag */ protected void setUseClassName(final boolean useClassName) { this.useClassName = useClassName; } //--------------------------------------------------------------------- /** *Gets whether to output short or long class names.
* * @return the current useShortClassName flag * @since 2.0 */ protected boolean isUseShortClassName() { return useShortClassName; } /** *Sets whether to output short or long class names.
* * @param useShortClassName the new useShortClassName flag * @since 2.0 */ protected void setUseShortClassName(final boolean useShortClassName) { this.useShortClassName = useShortClassName; } //--------------------------------------------------------------------- /** *Gets whether to use the identity hash code.
* * @return the current useIdentityHashCode flag */ protected boolean isUseIdentityHashCode() { return useIdentityHashCode; } /** *Sets whether to use the identity hash code.
* * @param useIdentityHashCode the new useIdentityHashCode flag */ protected void setUseIdentityHashCode(final boolean useIdentityHashCode) { this.useIdentityHashCode = useIdentityHashCode; } //--------------------------------------------------------------------- /** *Gets whether to use the field names passed in.
* * @return the current useFieldNames flag */ protected boolean isUseFieldNames() { return useFieldNames; } /** *Sets whether to use the field names passed in.
* * @param useFieldNames the new useFieldNames flag */ protected void setUseFieldNames(final boolean useFieldNames) { this.useFieldNames = useFieldNames; } //--------------------------------------------------------------------- /** *Gets whether to use full detail when the caller doesn't * specify.
* * @return the current defaultFullDetail flag */ protected boolean isDefaultFullDetail() { return defaultFullDetail; } /** *Sets whether to use full detail when the caller doesn't * specify.
* * @param defaultFullDetail the new defaultFullDetail flag */ protected void setDefaultFullDetail(final boolean defaultFullDetail) { this.defaultFullDetail = defaultFullDetail; } //--------------------------------------------------------------------- /** *Gets whether to output array content detail.
* * @return the current array content detail setting */ protected boolean isArrayContentDetail() { return arrayContentDetail; } /** *Sets whether to output array content detail.
* * @param arrayContentDetail the new arrayContentDetail flag */ protected void setArrayContentDetail(final boolean arrayContentDetail) { this.arrayContentDetail = arrayContentDetail; } //--------------------------------------------------------------------- /** *Gets the array start text.
* * @return the current array start text */ protected String getArrayStart() { return arrayStart; } /** *Sets the array start text.
* ** * @param arrayStart the new array start text */ protected void setArrayStart(String arrayStart) { if (arrayStart == null) { arrayStart = ""; } this.arrayStart = arrayStart; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the array end text.
* * @return the current array end text */ protected String getArrayEnd() { return arrayEnd; } /** *Sets the array end text.
* ** * @param arrayEnd the new array end text */ protected void setArrayEnd(String arrayEnd) { if (arrayEnd == null) { arrayEnd = ""; } this.arrayEnd = arrayEnd; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the array separator text.
* * @return the current array separator text */ protected String getArraySeparator() { return arraySeparator; } /** *Sets the array separator text.
* ** * @param arraySeparator the new array separator text */ protected void setArraySeparator(String arraySeparator) { if (arraySeparator == null) { arraySeparator = ""; } this.arraySeparator = arraySeparator; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the content start text.
* * @return the current content start text */ protected String getContentStart() { return contentStart; } /** *Sets the content start text.
* ** * @param contentStart the new content start text */ protected void setContentStart(String contentStart) { if (contentStart == null) { contentStart = ""; } this.contentStart = contentStart; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the content end text.
* * @return the current content end text */ protected String getContentEnd() { return contentEnd; } /** *Sets the content end text.
* ** * @param contentEnd the new content end text */ protected void setContentEnd(String contentEnd) { if (contentEnd == null) { contentEnd = ""; } this.contentEnd = contentEnd; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the field name value separator text.
* * @return the current field name value separator text */ protected String getFieldNameValueSeparator() { return fieldNameValueSeparator; } /** *Sets the field name value separator text.
* ** * @param fieldNameValueSeparator the new field name value separator text */ protected void setFieldNameValueSeparator(String fieldNameValueSeparator) { if (fieldNameValueSeparator == null) { fieldNameValueSeparator = ""; } this.fieldNameValueSeparator = fieldNameValueSeparator; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the field separator text.
* * @return the current field separator text */ protected String getFieldSeparator() { return fieldSeparator; } /** *Sets the field separator text.
* ** * @param fieldSeparator the new field separator text */ protected void setFieldSeparator(String fieldSeparator) { if (fieldSeparator == null) { fieldSeparator = ""; } this.fieldSeparator = fieldSeparator; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets whether the field separator should be added at the start * of each buffer.
* * @return the fieldSeparatorAtStart flag * @since 2.0 */ protected boolean isFieldSeparatorAtStart() { return fieldSeparatorAtStart; } /** *Sets whether the field separator should be added at the start * of each buffer.
* * @param fieldSeparatorAtStart the fieldSeparatorAtStart flag * @since 2.0 */ protected void setFieldSeparatorAtStart(final boolean fieldSeparatorAtStart) { this.fieldSeparatorAtStart = fieldSeparatorAtStart; } //--------------------------------------------------------------------- /** *Gets whether the field separator should be added at the end * of each buffer.
* * @return fieldSeparatorAtEnd flag * @since 2.0 */ protected boolean isFieldSeparatorAtEnd() { return fieldSeparatorAtEnd; } /** *Sets whether the field separator should be added at the end * of each buffer.
* * @param fieldSeparatorAtEnd the fieldSeparatorAtEnd flag * @since 2.0 */ protected void setFieldSeparatorAtEnd(final boolean fieldSeparatorAtEnd) { this.fieldSeparatorAtEnd = fieldSeparatorAtEnd; } //--------------------------------------------------------------------- /** *Gets the text to output when
* * @return the current text to output when null found */ protected String getNullText() { return nullText; } /** *nullfound.Sets the text to output when
* *nullfound.* * @param nullText the new text to output when null found */ protected void setNullText(String nullText) { if (nullText == null) { nullText = ""; } this.nullText = nullText; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the start text to output when a
* *Collection, *Mapor array size is output.This is output before the size value.
* * @return the current start of size text */ protected String getSizeStartText() { return sizeStartText; } /** *Sets the start text to output when a
* *Collection, *Mapor array size is output.This is output before the size value.
* ** * @param sizeStartText the new start of size text */ protected void setSizeStartText(String sizeStartText) { if (sizeStartText == null) { sizeStartText = ""; } this.sizeStartText = sizeStartText; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the end text to output when a
* *Collection, *Mapor array size is output.This is output after the size value.
* * @return the current end of size text */ protected String getSizeEndText() { return sizeEndText; } /** *Sets the end text to output when a
* *Collection, *Mapor array size is output.This is output after the size value.
* ** * @param sizeEndText the new end of size text */ protected void setSizeEndText(String sizeEndText) { if (sizeEndText == null) { sizeEndText = ""; } this.sizeEndText = sizeEndText; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the start text to output when an
* *Objectis * output in summary mode.This is output before the size value.
* * @return the current start of summary text */ protected String getSummaryObjectStartText() { return summaryObjectStartText; } /** *Sets the start text to output when an
* *Objectis * output in summary mode.This is output before the size value.
* ** * @param summaryObjectStartText the new start of summary text */ protected void setSummaryObjectStartText(String summaryObjectStartText) { if (summaryObjectStartText == null) { summaryObjectStartText = ""; } this.summaryObjectStartText = summaryObjectStartText; } //--------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Gets the end text to output when an
* *Objectis * output in summary mode.This is output after the size value.
* * @return the current end of summary text */ protected String getSummaryObjectEndText() { return summaryObjectEndText; } /** *Sets the end text to output when an
* *Objectis * output in summary mode.This is output after the size value.
* ** * @param summaryObjectEndText the new end of summary text */ protected void setSummaryObjectEndText(String summaryObjectEndText) { if (summaryObjectEndText == null) { summaryObjectEndText = ""; } this.summaryObjectEndText = summaryObjectEndText; } //---------------------------------------------------------------------------- /** *
nullis accepted, but will be converted to * an empty String.Default
* *ToStringStyle.This is an inner class rather than using *
*/ private static final class DefaultToStringStyle extends ToStringStyle { /** * Required for serialization support. * * @see java.io.Serializable */ private static final long serialVersionUID = 1L; /** *StandardToStringStyleto ensure its immutability.Constructor.
* *Use the static constant rather than instantiating.
*/ DefaultToStringStyle() { super(); } /** *Ensure
* * @return the singleton */ private Object readResolve() { return ToStringStyle.DEFAULT_STYLE; } } //---------------------------------------------------------------------------- /** *Singletonafter serialization.* *
ToStringStylethat does not print out * the field names.This is an inner class rather than using *
StandardToStringStyleto ensure its immutability. */ private static final class NoFieldNameToStringStyle extends ToStringStyle { private static final long serialVersionUID = 1L; /** *Constructor.
* *Use the static constant rather than instantiating.
*/ NoFieldNameToStringStyle() { super(); this.setUseFieldNames(false); } /** *Ensure
* * @return the singleton */ private Object readResolve() { return ToStringStyle.NO_FIELD_NAMES_STYLE; } } //---------------------------------------------------------------------------- /** *Singletonafter serialization.* *
ToStringStylethat prints out the short * class name and no identity hashcode.This is an inner class rather than using *
*/ private static final class ShortPrefixToStringStyle extends ToStringStyle { private static final long serialVersionUID = 1L; /** *StandardToStringStyleto ensure its immutability.Constructor.
* *Use the static constant rather than instantiating.
*/ ShortPrefixToStringStyle() { super(); this.setUseShortClassName(true); this.setUseIdentityHashCode(false); } /** *Ensure
* @return the singleton */ private Object readResolve() { return ToStringStyle.SHORT_PREFIX_STYLE; } } //---------------------------------------------------------------------------- /** *Singleton after serialization.* *
ToStringStylethat does not print out the * classname, identity hashcode, content start or field name.This is an inner class rather than using *
*/ private static final class SimpleToStringStyle extends ToStringStyle { private static final long serialVersionUID = 1L; /** *StandardToStringStyleto ensure its immutability.Constructor.
* *Use the static constant rather than instantiating.
*/ SimpleToStringStyle() { super(); this.setUseClassName(false); this.setUseIdentityHashCode(false); this.setUseFieldNames(false); this.setContentStart(""); this.setContentEnd(""); } /** *Ensure
* @return the singleton */ private Object readResolve() { return ToStringStyle.SIMPLE_STYLE; } } //---------------------------------------------------------------------------- /** *Singleton after serialization.* *
ToStringStylethat outputs on multiple lines.This is an inner class rather than using *
*/ private static final class MultiLineToStringStyle extends ToStringStyle { private static final long serialVersionUID = 1L; /** *StandardToStringStyleto ensure its immutability.Constructor.
* *Use the static constant rather than instantiating.
*/ MultiLineToStringStyle() { super(); this.setContentStart("["); this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + " "); this.setFieldSeparatorAtStart(true); this.setContentEnd(SystemUtils.LINE_SEPARATOR + "]"); } /** *Ensure
* * @return the singleton */ private Object readResolve() { return ToStringStyle.MULTI_LINE_STYLE; } } //---------------------------------------------------------------------------- /** *Singletonafter serialization.* *
ToStringStylethat does not print out the classname * and identity hashcode but prints content start and field names.This is an inner class rather than using *
*/ private static final class NoClassNameToStringStyle extends ToStringStyle { private static final long serialVersionUID = 1L; /** *StandardToStringStyleto ensure its immutability.Constructor.
* *Use the static constant rather than instantiating.
*/ NoClassNameToStringStyle() { super(); this.setUseClassName(false); this.setUseIdentityHashCode(false); } /** *Ensure
* * @return the singleton */ private Object readResolve() { return ToStringStyle.NO_CLASS_NAME_STYLE; } } // ---------------------------------------------------------------------------- /** *Singletonafter serialization.*
* *ToStringStylethat outputs with JSON format. ** This is an inner class rather than using *
*/ private static final class JsonToStringStyle extends ToStringStyle { private static final long serialVersionUID = 1L; /** * The summary size text startStandardToStringStyleto ensure its immutability. *'>'. */ private String FIELD_NAME_PREFIX = "\""; /** ** Constructor. *
* ** Use the static constant rather than instantiating. *
*/ JsonToStringStyle() { super(); this.setUseClassName(false); this.setUseIdentityHashCode(false); this.setContentStart("{"); this.setContentEnd("}"); this.setArrayStart("["); this.setArrayEnd("]"); this.setFieldSeparator(","); this.setFieldNameValueSeparator(":"); this.setNullText("null"); this.setSummaryObjectStartText("\"<"); this.setSummaryObjectEndText(">\""); this.setSizeStartText("\"\""); } @Override public void append(StringBuffer buffer, String fieldName, Object[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, long[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, int[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, short[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, byte[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, char[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, double[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, float[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, boolean[] array, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, array, fullDetail); } @Override public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } if (!isFullDetail(fullDetail)){ throw new UnsupportedOperationException( "FullDetail must be true when using JsonToStringStyle"); } super.append(buffer, fieldName, value, fullDetail); } @Override protected void appendDetail(StringBuffer buffer, String fieldName, Object value) { if (value == null) { appendNullText(buffer, fieldName); return; } if (value.getClass() == String.class) { appendValueAsString(buffer, (String)value); return; } buffer.append(value); } /** * Appends the given String in parenthesis to the given StringBuffer. * * @param buffer the StringBuffer to append the value to. * @param value the value to append. */ private void appendValueAsString(StringBuffer buffer, String value) { buffer.append("\"" + value + "\""); } @Override protected void appendFieldStart(StringBuffer buffer, String fieldName) { if (fieldName == null) { throw new UnsupportedOperationException( "Field names are mandatory when using JsonToStringStyle"); } super.appendFieldStart(buffer, FIELD_NAME_PREFIX + fieldName + FIELD_NAME_PREFIX); } /** * * Ensure
* * @return the singleton */ private Object readResolve() { return ToStringStyle.JSON_STYLE; } } }Singletonafter serialization. *