Class ToStringBuilder

java.lang.Object
com.kosdev.kos.commons.util.misc.ToStringBuilder

public class ToStringBuilder extends Object
Minimal string builder used to implement toString() when the @ToString annotation isn't used. The output matches the Lombok's output.

Example

 
  public class Person {
      String name;
      int age;
      boolean rightHanded;

      // . . .

      public String toString() {
          return new ToStringBuildr(this)
              .append("name", name)
              .append("age", age)
              .append("rightHanded", rightHanded)
              .toString();
      }
  }
 
 

This will produce a toString() of the format:

  Person[name=Stephen, age=29, rightHanded=false]
 
Since:
10
Version:
10
  • Constructor Details

    • ToStringBuilder

      public ToStringBuilder(Object object)
      Create a new builder for the specified object. The class name of the object will be used as the first token of the string.
      Parameters:
      object - the object the string is for
      Since:
      10
  • Method Details

    • append

      public ToStringBuilder append(String fieldName, Object fieldValue)
      Append a field name and value to the string.
      Parameters:
      fieldName - name of the field
      fieldValue - value of the field
      Returns:
      the builder
      Since:
      10
    • toString

      public String toString()
      Overrides:
      toString in class Object