Skip to content

Commit

Permalink
Merge pull request functionaljava#15 from samreid/master
Browse files Browse the repository at this point in the history
Provide equals/hashCode/toString for fj.List<A>
  • Loading branch information
runarorama committed Mar 6, 2012
2 parents b8fb621 + d0a686b commit c127183
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
43 changes: 43 additions & 0 deletions core/src/main/java/fj/data/List.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>List<A></code> 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<A> 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.<A>anyEqual() ).eq( this, (List<A>) 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.<A>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.<A>anyShow() ).show( this ).foldLeft( new F2<String, Character, String>() {
@Override public String f( final String s, final Character c ) {
return s + c;
}
}, "" );
}
}
3 changes: 3 additions & 0 deletions etc/CONTRIBUTORS
Expand Up @@ -131,3 +131,6 @@ jason@functionaljava.org
Dylan Just
dylan@techtangents.com

Sam Reid
reids@colorado.edu

0 comments on commit c127183

Please sign in to comment.