MoreObjects.ToStringHelper allow user to specify Appendable
See original GitHub issueCurrently the ToStringHelper
class constructs a StringBuilder with an initial capacity of 32 in the toString
method
@Override
public String toString() {
// create a copy to keep it consistent in case value changes
boolean omitNullValuesSnapshot = omitNullValues;
String nextSeparator = "";
StringBuilder builder = new StringBuilder(32).append(className).append('{');
I think it would be useful to add an instance field to ToStringHelper that holds a Appendable instance that can be given to it, or defaulted to the new StringBuilder(32)
. Also additional factory methods would be added toMoreObjects
to allow the user to specify said instance, e.g. public static ToStringHelper toStringHelper(Appendable buffer, Object self)
. This would allow reuse of StringBuilders as opposed to creating them every time.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Class MoreObjects.ToStringHelper - Guava
Adds an unnamed value to the formatted output. It is strongly encouraged to use add(String, Object) instead and give value a readable name....
Read more >Alternative to MoreObjects in Java 8 - Stack Overflow
You can use StringJoiner from java.util package. Example: @Override public String toString() { return new StringJoiner(", ", ClassName.class.
Read more >guava/src/com/google/common/base/MoreObjects.java
That approach also allows for ... Use {@link MoreObjects#toStringHelper(Object)} to create an instance. ... Returns a string in the format specified by.
Read more >Guava 18: What's New? - Baeldung
We can use the MoreObjects.toStringHelper(Object self) method to do this easily. public class User { private long id; private String name; ...
Read more >com.google.common.base.MoreObjects.java Source code
That approach also allows for * lazy evaluation of the fallback instance, ... Use {@link MoreObjects#toStringHelper(Object)} to create an instance.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I still think providing more flexibility here isn’t a great idea.
ToStringHelper
already builds up a linked list of wrapped entries, so it’s not designed to be high performance. I worry that by providing the overload, we’re going to have users trying to count the number of characters in theirtoString()
s by hand, and that seems wasteful.StringBuilder
expands by doubling it’s internal buffer…the string would have to be pretty enormous for this to happen more than a few times.Are you noticing actual performance problems from this API?
ToStringHelper
is designed to make writingtoString()'s
easy. If you require a super performant implementation oftoString()
, you’re probably better off rolling your own.