Package com.helger.base.tostring
Class ToStringGenerator
java.lang.Object
com.helger.base.tostring.ToStringGenerator
This is a utility class for easier
toString method creations. It assumes that the
toString method is only used for the representation of the internal state and not
for creating human readable formats.
A real world example for a final class derived from Object or a base class looks like
this:
@Override
public String toString ()
{
return new ToStringGenerator (this).append ("member1", member1).append ("member2", member2).toString ();
}
For a derived class, the typical code looks like this, assuming that the base class also used the
ToStringGenerator:
@Override
public String toString ()
{
return ToStringGenerator.getDerived (super.toString ())
.append ("member3", member3)
.append ("member4", member4)
.toString ();
}
- Author:
- Philip Helger
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@NonNull ToStringGeneratorAppend a boolean field.@NonNull ToStringGeneratorAppend a boolean array field.@NonNull ToStringGeneratorAppend a byte field.@NonNull ToStringGeneratorAppend a byte array field.@NonNull ToStringGeneratorAppend a char field.@NonNull ToStringGeneratorAppend a char array field.@NonNull ToStringGeneratorAppend a double field.@NonNull ToStringGeneratorAppend a double array field.@NonNull ToStringGeneratorAppend a float field.@NonNull ToStringGeneratorAppend a float array field.@NonNull ToStringGeneratorAppend an int field.@NonNull ToStringGeneratorAppend an int array field.@NonNull ToStringGeneratorAppend a long field.@NonNull ToStringGeneratorAppend a long array field.@NonNull ToStringGeneratorAppend a short field.@NonNull ToStringGeneratorAppend a short array field.@NonNull ToStringGeneratorAppend an enum field.@NonNull ToStringGeneratorAppend an object field.@NonNull ToStringGeneratorAppend an object array field.@NonNull ToStringGeneratorappendIf(@NonNull String sField, boolean bValue, @NonNull IBooleanPredicate aFilter) Conditionally append a boolean field if the filter matches the value.@NonNull ToStringGeneratorappendIf(@NonNull String sField, boolean bValue, @NonNull BooleanSupplier aFilter) Conditionally append a boolean field if the supplier returnstrue.@NonNull ToStringGeneratorappendIf(@NonNull String sField, char cValue, @NonNull ICharPredicate aFilter) Conditionally append a char field if the filter matches the value.@NonNull ToStringGeneratorappendIf(@NonNull String sField, char cValue, @NonNull BooleanSupplier aFilter) Conditionally append a char field if the supplier returnstrue.@NonNull ToStringGeneratorappendIf(@NonNull String sField, double dValue, @NonNull BooleanSupplier aFilter) Conditionally append a double field if the supplier returnstrue.@NonNull ToStringGeneratorappendIf(@NonNull String sField, double dValue, @NonNull DoublePredicate aFilter) Conditionally append a double field if the filter matches the value.@NonNull ToStringGeneratorappendIf(@NonNull String sField, int nValue, @NonNull BooleanSupplier aFilter) Conditionally append an int field if the supplier returnstrue.@NonNull ToStringGeneratorappendIf(@NonNull String sField, int nValue, @NonNull IntPredicate aFilter) Conditionally append an int field if the filter matches the value.@NonNull ToStringGeneratorappendIf(@NonNull String sField, long nValue, @NonNull BooleanSupplier aFilter) Conditionally append a long field if the supplier returnstrue.@NonNull ToStringGeneratorappendIf(@NonNull String sField, long nValue, @NonNull LongPredicate aFilter) Conditionally append a long field if the filter matches the value.<T> @NonNull ToStringGeneratorappendIf(@NonNull String sField, @Nullable T aValue, @NonNull BooleanSupplier aFilter) Conditionally append an object field if the supplier returnstrue.<T> @NonNull ToStringGeneratorConditionally append an object field if the filter matches the value.@NonNull ToStringGeneratorappendIfNotNull(@NonNull String sField, @Nullable Object aValue) Append an object field only if the value is notnull.@NonNull ToStringGeneratorappendPassword(@NonNull String sField) Append a password field.@NonNull ToStringGeneratorappendPasswordIf(@NonNull String sField, BooleanSupplier aFilter) Conditionally append a password field if the supplier returnstrue.static @NonNull ToStringGeneratorgetDerived(@NonNull String sSuperToString) Create aToStringGeneratorfor derived classes where the base class also uses theToStringGenerator.@NonNull StringRetrieve the final string representation.@NonNull StringtoString()Deprecated.
-
Field Details
-
CONSTANT_NULL
String to be emitted fornullvalues- See Also:
-
CONSTANT_THIS
String to be emitted forthisvalues- See Also:
-
CONSTANT_PASSWORD
String to be emitted for password values- See Also:
-
-
Constructor Details
-
ToStringGenerator
Constructor.- Parameters:
aSrc- The source object for which the toString representation is to be generated. May benull.
-
-
Method Details
-
append
Append a boolean field.- Parameters:
sField- The name of the field. May not benull.aValue- The boolean value.- Returns:
- this for chaining
-
append
Append a boolean array field.- Parameters:
sField- The name of the field. May not benull.aValue- The boolean array value. May benull.- Returns:
- this for chaining
-
append
Append a byte field.- Parameters:
sField- The name of the field. May not benull.aValue- The byte value.- Returns:
- this for chaining
-
append
Append a byte array field.- Parameters:
sField- The name of the field. May not benull.aValue- The byte array value. May benull.- Returns:
- this for chaining
-
append
Append a char field.- Parameters:
sField- The name of the field. May not benull.aValue- The char value.- Returns:
- this for chaining
-
append
Append a char array field.- Parameters:
sField- The name of the field. May not benull.aValue- The char array value. May benull.- Returns:
- this for chaining
-
append
Append a double field.- Parameters:
sField- The name of the field. May not benull.aValue- The double value.- Returns:
- this for chaining
-
append
Append a double array field.- Parameters:
sField- The name of the field. May not benull.aValue- The double array value. May benull.- Returns:
- this for chaining
-
append
Append a float field.- Parameters:
sField- The name of the field. May not benull.aValue- The float value.- Returns:
- this for chaining
-
append
Append a float array field.- Parameters:
sField- The name of the field. May not benull.aValue- The float array value. May benull.- Returns:
- this for chaining
-
append
Append an int field.- Parameters:
sField- The name of the field. May not benull.aValue- The int value.- Returns:
- this for chaining
-
append
Append an int array field.- Parameters:
sField- The name of the field. May not benull.aValue- The int array value. May benull.- Returns:
- this for chaining
-
append
Append a long field.- Parameters:
sField- The name of the field. May not benull.aValue- The long value.- Returns:
- this for chaining
-
append
Append a long array field.- Parameters:
sField- The name of the field. May not benull.aValue- The long array value. May benull.- Returns:
- this for chaining
-
append
Append a short field.- Parameters:
sField- The name of the field. May not benull.aValue- The short value.- Returns:
- this for chaining
-
append
Append a short array field.- Parameters:
sField- The name of the field. May not benull.aValue- The short array value. May benull.- Returns:
- this for chaining
-
appendPassword
Append a password field. The actual value is masked withCONSTANT_PASSWORD.- Parameters:
sField- The name of the field. May not benull.- Returns:
- this for chaining
-
append
Append an enum field.- Parameters:
sField- The name of the field. May not benull.aValue- The enum value. May benull.- Returns:
- this for chaining
-
append
Append an object field. If the value is an array, it is handled specially.- Parameters:
sField- The name of the field. May not benull.aValue- The object value. May benull.- Returns:
- this for chaining
-
append
Append an object array field.- Parameters:
sField- The name of the field. May not benull.aValue- The object array value. May benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, boolean bValue, @NonNull IBooleanPredicate aFilter) Conditionally append a boolean field if the filter matches the value.- Parameters:
sField- The name of the field. May not benull.bValue- The boolean value.aFilter- The predicate to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, boolean bValue, @NonNull BooleanSupplier aFilter) Conditionally append a boolean field if the supplier returnstrue.- Parameters:
sField- The name of the field. May not benull.bValue- The boolean value.aFilter- The boolean supplier to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, char cValue, @NonNull ICharPredicate aFilter) Conditionally append a char field if the filter matches the value.- Parameters:
sField- The name of the field. May not benull.cValue- The char value.aFilter- The predicate to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, char cValue, @NonNull BooleanSupplier aFilter) Conditionally append a char field if the supplier returnstrue.- Parameters:
sField- The name of the field. May not benull.cValue- The char value.aFilter- The boolean supplier to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, int nValue, @NonNull IntPredicate aFilter) Conditionally append an int field if the filter matches the value.- Parameters:
sField- The name of the field. May not benull.nValue- The int value.aFilter- The predicate to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, int nValue, @NonNull BooleanSupplier aFilter) Conditionally append an int field if the supplier returnstrue.- Parameters:
sField- The name of the field. May not benull.nValue- The int value.aFilter- The boolean supplier to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, long nValue, @NonNull LongPredicate aFilter) Conditionally append a long field if the filter matches the value.- Parameters:
sField- The name of the field. May not benull.nValue- The long value.aFilter- The predicate to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, long nValue, @NonNull BooleanSupplier aFilter) Conditionally append a long field if the supplier returnstrue.- Parameters:
sField- The name of the field. May not benull.nValue- The long value.aFilter- The boolean supplier to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, double dValue, @NonNull DoublePredicate aFilter) Conditionally append a double field if the filter matches the value.- Parameters:
sField- The name of the field. May not benull.dValue- The double value.aFilter- The predicate to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public @NonNull ToStringGenerator appendIf(@NonNull String sField, double dValue, @NonNull BooleanSupplier aFilter) Conditionally append a double field if the supplier returnstrue.- Parameters:
sField- The name of the field. May not benull.dValue- The double value.aFilter- The boolean supplier to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public <T> @NonNull ToStringGenerator appendIf(@NonNull String sField, @Nullable T aValue, @NonNull Predicate<? super T> aFilter) Conditionally append an object field if the filter matches the value.- Type Parameters:
T- The value type.- Parameters:
sField- The name of the field. May not benull.aValue- The value. May benull.aFilter- The predicate to evaluate. May not benull.- Returns:
- this for chaining
-
appendIf
public <T> @NonNull ToStringGenerator appendIf(@NonNull String sField, @Nullable T aValue, @NonNull BooleanSupplier aFilter) Conditionally append an object field if the supplier returnstrue.- Type Parameters:
T- The value type.- Parameters:
sField- The name of the field. May not benull.aValue- The value. May benull.aFilter- The boolean supplier to evaluate. May not benull.- Returns:
- this for chaining
-
appendIfNotNull
Append an object field only if the value is notnull.- Parameters:
sField- The name of the field. May not benull.aValue- The value. May benull.- Returns:
- this for chaining
-
appendPasswordIf
Conditionally append a password field if the supplier returnstrue.- Parameters:
sField- The name of the field. May not benull.aFilter- The boolean supplier to evaluate. May not benull.- Returns:
- this for chaining
-
getToString
Retrieve the final string representation. Once this method has been called, the closing bracket is appended and subsequent calls return the same result.- Returns:
- The generated toString value. Never
null.
-
toString
Deprecated.Don't call this; UsegetToString()instead -
getDerived
Create aToStringGeneratorfor derived classes where the base class also uses theToStringGenerator. This avoids that the implementing class name is emitted more than once.- Parameters:
sSuperToString- Always pass insuper.toString ()- Returns:
- Never
null
-
getToString()instead