Class ToStringGenerator

java.lang.Object
com.helger.base.tostring.ToStringGenerator

@NotThreadSafe @UsedInGeneratedCode public final class ToStringGenerator extends Object
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 Details

  • Constructor Details

    • ToStringGenerator

      public ToStringGenerator(@Nullable Object aSrc)
      Constructor.
      Parameters:
      aSrc - The source object for which the toString representation is to be generated. May be null.
  • Method Details

    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, boolean aValue)
      Append a boolean field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The boolean value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, boolean @Nullable [] aValue)
      Append a boolean array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The boolean array value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, byte aValue)
      Append a byte field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The byte value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, byte @Nullable [] aValue)
      Append a byte array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The byte array value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, char aValue)
      Append a char field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The char value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, char @Nullable [] aValue)
      Append a char array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The char array value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, double aValue)
      Append a double field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The double value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, double @Nullable [] aValue)
      Append a double array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The double array value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, float aValue)
      Append a float field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The float value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, float @Nullable [] aValue)
      Append a float array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The float array value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, int aValue)
      Append an int field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The int value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, int @Nullable [] aValue)
      Append an int array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The int array value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, long aValue)
      Append a long field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The long value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, long @Nullable [] aValue)
      Append a long array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The long array value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, short aValue)
      Append a short field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The short value.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, short @Nullable [] aValue)
      Append a short array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The short array value. May be null.
      Returns:
      this for chaining
    • appendPassword

      public @NonNull ToStringGenerator appendPassword(@NonNull String sField)
      Append a password field. The actual value is masked with CONSTANT_PASSWORD.
      Parameters:
      sField - The name of the field. May not be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, @Nullable Enum<?> aValue)
      Append an enum field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The enum value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, @Nullable Object aValue)
      Append an object field. If the value is an array, it is handled specially.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The object value. May be null.
      Returns:
      this for chaining
    • append

      public @NonNull ToStringGenerator append(@NonNull String sField, @Nullable Object[] aValue)
      Append an object array field.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The object array value. May be null.
      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 be null.
      bValue - The boolean value.
      aFilter - The predicate to evaluate. May not be null.
      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 returns true.
      Parameters:
      sField - The name of the field. May not be null.
      bValue - The boolean value.
      aFilter - The boolean supplier to evaluate. May not be null.
      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 be null.
      cValue - The char value.
      aFilter - The predicate to evaluate. May not be null.
      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 returns true.
      Parameters:
      sField - The name of the field. May not be null.
      cValue - The char value.
      aFilter - The boolean supplier to evaluate. May not be null.
      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 be null.
      nValue - The int value.
      aFilter - The predicate to evaluate. May not be null.
      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 returns true.
      Parameters:
      sField - The name of the field. May not be null.
      nValue - The int value.
      aFilter - The boolean supplier to evaluate. May not be null.
      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 be null.
      nValue - The long value.
      aFilter - The predicate to evaluate. May not be null.
      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 returns true.
      Parameters:
      sField - The name of the field. May not be null.
      nValue - The long value.
      aFilter - The boolean supplier to evaluate. May not be null.
      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 be null.
      dValue - The double value.
      aFilter - The predicate to evaluate. May not be null.
      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 returns true.
      Parameters:
      sField - The name of the field. May not be null.
      dValue - The double value.
      aFilter - The boolean supplier to evaluate. May not be null.
      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 be null.
      aValue - The value. May be null.
      aFilter - The predicate to evaluate. May not be null.
      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 returns true.
      Type Parameters:
      T - The value type.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The value. May be null.
      aFilter - The boolean supplier to evaluate. May not be null.
      Returns:
      this for chaining
    • appendIfNotNull

      public @NonNull ToStringGenerator appendIfNotNull(@NonNull String sField, @Nullable Object aValue)
      Append an object field only if the value is not null.
      Parameters:
      sField - The name of the field. May not be null.
      aValue - The value. May be null.
      Returns:
      this for chaining
    • appendPasswordIf

      public @NonNull ToStringGenerator appendPasswordIf(@NonNull String sField, BooleanSupplier aFilter)
      Conditionally append a password field if the supplier returns true.
      Parameters:
      sField - The name of the field. May not be null.
      aFilter - The boolean supplier to evaluate. May not be null.
      Returns:
      this for chaining
    • getToString

      public @NonNull String 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(forRemoval=false) public @NonNull String toString()
      Deprecated.
      Don't call this; Use getToString() instead
      Overrides:
      toString in class Object
    • getDerived

      public static @NonNull ToStringGenerator getDerived(@NonNull String sSuperToString)
      Create a ToStringGenerator for derived classes where the base class also uses the ToStringGenerator. This avoids that the implementing class name is emitted more than once.
      Parameters:
      sSuperToString - Always pass in super.toString ()
      Returns:
      Never null