yf0994 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Array handling in Objects#toStringHelper #1125

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently there is no handling for arrays in helpers in Objects, e.g. 
toStringHelper and Object.equal.

That means, I cannot do
Objects.toStringHelper(getClass())
    .add("foo", this.foo)
    .add("anArray", this.anArray)
    .toString();

(I know I can change it to add("anArray", Arrays.toString(this.anArray)) but it 
looks bad)

Same for Objects.equal, when I am overriding equals(), I cannot do:
return Objects.equal("foo", other.foo) 
        && Objects.equals("anArray", other.anArray);

I believe adding corresponding overloads should not be difficult, and it makes 
the API much easier to use.

Original issue reported on code.google.com by adrians...@gmail.com on 29 Aug 2012 at 3:06

GoogleCodeExporter commented 9 years ago
How many different types should we be adding special-case handling for, between 
this and Optional?

(FYI, corresponding overloads would be necessary for all the primitive array 
types in addition to Object[], so that's a total of nine overloads -- and then 
should we be using deepToString just in case?)

Original comment by wasserman.louis on 29 Aug 2012 at 3:14

GoogleCodeExporter commented 9 years ago
imho, these methods in Objects aim to provide a fluent and simplified 
implementation for toString/hashCode/equals .  It is kind of losing its fluency 
if we need to switch to other APIs, which is also error-prone (like me, I have 
mistakenly use toStringHelper.add("anArray", anArray).

I used to use Apache Commons Lang's 
ToStringBuilder/HashCodeBuilder/EqualsBuilder and it is handling arrays well.  
I do hope to switch to Guava due to the ease of use and performance gain, but 
array handling is a little nuisance which make Guava less enjoyable to use in 
this aspect :)

Original comment by adrians...@gmail.com on 30 Aug 2012 at 2:06

GoogleCodeExporter commented 9 years ago
It would be too dangerous to try and change the equals() and hashCode() 
behavior for arrays, but we like this idea for toStringHelper(). We'll just 
make use of Arrays.deepToString().

Original comment by kak@google.com on 23 Oct 2012 at 4:55

GoogleCodeExporter commented 9 years ago
As Kurt says, this is mostly just a matter of changing Objects.ToStringBuilder 
to append "Arrays.deepToString(value)" instead of just "value." Then you can 
add some tests for the new behavior. It should make a decent introductory Real 
CL.

http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepToString%28ja
va.lang.Object[]%29

Original comment by cpov...@google.com on 20 May 2013 at 2:39

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08