Lets face it writing nice toString messages is a boring messy chore that needs to be done as it can really be helpful for insepection in a debugger or logging.
What features do you like or wish should be in such a helper...
dumping properties should come w/ labels.
name=mP country=Australia ...
values that are some default should optionally be skipped.
the seperator between label and value should be updatable and it should auto be inserted between labels and values when they are added.
it should also auto insert the separator of your choice.
If you want commas spaces whatever between values when including an array so be it.
it should auto quote string values...because its important to know exactly where a string starts and ends.
*name=mP state="New South Wales"
when a list, map or set is added the rules about quoting strings, using the set separator etc should be respected. Please dont just dump Collection.toString().
I have a few others in someting i am improving can you list your own ideas, observations etc.
new ToStringBuilder()
.setLabelValueSeparator('=')
.label("name")
.value(Country.AUSTRALIA) // -> returns "Australia" without the quotes.
.label("day of death")
.value(null) //
.label("numbers")
.valueSeparator(",");
.value(Arrays.asList( 1, 2, 3 )
.build();
will of course result in "name="Australia" numbers=1, 2, 3;
The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler. Else, the user implemented or overridden toString() method is called.
Java - toString() MethodThe method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.
toString() converts the object to a string. The string representation is the string "class" or "interface", followed by a space, and then by the fully qualified name of the class in the format returned by getName.
ToStringBuilder is a utility class provided by apache commons lang library. ToStringBuilder is a utility class provided by apache commons lang library. It provides a consistent and better control over what and how much data, an object should expose using toString() method and in which format.
Apache ToStringBuilder has a decent implenentation out of the box:
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
I'm actually looking right now on how to get its output to be a bit prettier. ReflectionStringBuilder seems to offer some more customization. Specifically I like this:
@Override
public String toString() {
StandardToStringStyle style = new StandardToStringStyle();
style.setFieldSeparator(", ");
style.setUseClassName(false);
style.setUseIdentityHashCode(false);
return new ReflectionToStringBuilder(this, style).toString();
}
The output looks like this:
[[email protected], age=16, createdDate=<null>, favoriteColor=blue, id=2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With