public class ToString extends Object
Object.toString() of a class.
The aim of this class, is to avoid the boilerplate code needed to implement
the Object.toString() method.
Even if most of the IDEs provide tools to generate implementations of Object.toString(),
the generated code is ugly and hard to understand.
By using this utility class the resulting Object.toString() method will be small,
clean and easy to read.
You may have seen a lot of times implementation of the method Object.toString()
in this form:
public String toString()
{
return "SomeType{" +
id=" + id +
", string='" + string + '\'' +
", intArray=" + Arrays.toString(intArray) +
", stringMatrix=" + Arrays.toString(stringMatrix) +
'}';
}
With this utility you can get the same result with some more concise
and elegant code:
public String toString()
{
return ToString.of( this )
.print( "id", id )
.print( "string", string )
.print( "intArray", intArray )
.print( "stringMatrix", stringMatrix )
.likeIntellij();
}
In addition, to change your objects layout the only one thing you need to do is to change the last method of the functional invocation chain.
| Modifier and Type | Class and Description |
|---|---|
static interface |
ToString.Configuration
Contains the information needed by the
ToString.Printer
to know how to build the String representation of an object. |
static interface |
ToString.Configurator
Implementation of the
Pattern Builder with the
purpose to properly create a ToString.Configuration. |
static interface |
ToString.Printer
Represents a printer able to build the
String
representation of an object using the given
ToString.Configuration. |
| Modifier and Type | Method and Description |
|---|---|
static ToString.Configurator |
of(Object object)
Returns an instance of
ToString.Configurator
in order to instruct the ToString facility
on how to build the text representation of the
given object. |
public static ToString.Configurator of(Object object)
ToString.Configurator
in order to instruct the ToString facility
on how to build the text representation of the
given object.object - an object to be converted to String.ToString.Configurator.Copyright © 2011–2020 Nerd4j. All rights reserved.