question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MoreObjects.ToStringHelper allow user to specify Appendable

See original GitHub issue

Currently 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:closed
  • Created 6 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kluevercommented, Jul 11, 2017

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 their toString()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.

1reaction
kluevercommented, Jul 6, 2017

Are you noticing actual performance problems from this API?

ToStringHelper is designed to make writing toString()'s easy. If you require a super performant implementation of toString(), you’re probably better off rolling your own.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found