From 547b47d801d226ec10a67d13fac25435f370da50 Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Mon, 5 Mar 2012 16:07:49 -0700 Subject: [PATCH 1/2] added equals, hashCode and toString implementations to fj.List which delegate to the corresponding Equal, Hash and Show classes which use the member values, see #14 --- core/src/main/java/fj/data/List.java | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/core/src/main/java/fj/data/List.java b/core/src/main/java/fj/data/List.java index b27d5b96..e349cf2a 100644 --- a/core/src/main/java/fj/data/List.java +++ b/core/src/main/java/fj/data/List.java @@ -7,11 +7,13 @@ import fj.F2; import fj.F3; import fj.Function; +import fj.Hash; import fj.Monoid; import fj.Ord; import fj.P; import fj.P1; import fj.P2; +import fj.Show; import fj.Unit; import static fj.Function.curry; import static fj.Function.constant; @@ -1828,4 +1830,45 @@ private void copy() { snoc(t.head()); } } + + /** + * Perform an equality test on this list which delegates to the .equals() method of the member instances. + * This is implemented with Equal.listEqual using the anyEqual rule. + * + * @param obj the other object to check for equality against. + * @return true if this list is equal to the provided argument + */ + //Suppress the warning for cast to List because the type is checked in the previous line. + @SuppressWarnings({ "unchecked" }) + @Override public boolean equals( final Object obj ) { + if ( obj == null || !( obj instanceof List ) ) { return false; } + + //Casting to List here does not cause a runtime exception even if the type arguments don't match. + //The cast is done to avoid the compiler warning "raw use of parameterized class 'List'" + return Equal.listEqual( Equal.anyEqual() ).eq( this, (List) obj ); + } + + /** + * Compute the hash code from this list as a function of the hash codes of its members. + * Delegates to Hash.listHash, using the anyHash() rule, which uses the hash codes of the contents. + * + * @return the hash code for this list. + */ + @Override public int hashCode() { + return Hash.listHash( Hash.anyHash() ).hash( this ); + } + + /** + * Obtain a string representation of this list using the toString implementations of the members. Uses Show.listShow with F2 argument and may + * not be very performant. + * + * @return a String representation of the list + */ + @Override public String toString() { + return Show.listShow( Show.anyShow() ).show( this ).foldLeft( new F2() { + @Override public String f( final String s, final Character c ) { + return s + c; + } + }, "" ); + } } From d0a686bccc367a6792cc9f666075185d8ead5e3f Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Mon, 5 Mar 2012 16:08:56 -0700 Subject: [PATCH 2/2] added Sam Reid to contributor list --- etc/CONTRIBUTORS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/etc/CONTRIBUTORS b/etc/CONTRIBUTORS index affd214b..4c4085b1 100644 --- a/etc/CONTRIBUTORS +++ b/etc/CONTRIBUTORS @@ -131,3 +131,6 @@ jason@functionaljava.org Dylan Just dylan@techtangents.com +Sam Reid +reids@colorado.edu +