|
7 | 7 | import fj.F2;
|
8 | 8 | import fj.F3;
|
9 | 9 | import fj.Function;
|
| 10 | +import fj.Hash; |
10 | 11 | import fj.Monoid;
|
11 | 12 | import fj.Ord;
|
12 | 13 | import fj.P;
|
13 | 14 | import fj.P1;
|
14 | 15 | import fj.P2;
|
| 16 | +import fj.Show; |
15 | 17 | import fj.Unit;
|
16 | 18 | import static fj.Function.curry;
|
17 | 19 | import static fj.Function.constant;
|
@@ -1828,4 +1830,45 @@ private void copy() {
|
1828 | 1830 | snoc(t.head());
|
1829 | 1831 | }
|
1830 | 1832 | }
|
| 1833 | + |
| 1834 | + /** |
| 1835 | + * Perform an equality test on this list which delegates to the .equals() method of the member instances. |
| 1836 | + * This is implemented with Equal.listEqual using the anyEqual rule. |
| 1837 | + * |
| 1838 | + * @param obj the other object to check for equality against. |
| 1839 | + * @return true if this list is equal to the provided argument |
| 1840 | + */ |
| 1841 | + //Suppress the warning for cast to <code>List<A></code> because the type is checked in the previous line. |
| 1842 | + @SuppressWarnings({ "unchecked" }) |
| 1843 | + @Override public boolean equals( final Object obj ) { |
| 1844 | + if ( obj == null || !( obj instanceof List ) ) { return false; } |
| 1845 | + |
| 1846 | + //Casting to List<A> here does not cause a runtime exception even if the type arguments don't match. |
| 1847 | + //The cast is done to avoid the compiler warning "raw use of parameterized class 'List'" |
| 1848 | + return Equal.listEqual( Equal.<A>anyEqual() ).eq( this, (List<A>) obj ); |
| 1849 | + } |
| 1850 | + |
| 1851 | + /** |
| 1852 | + * Compute the hash code from this list as a function of the hash codes of its members. |
| 1853 | + * Delegates to Hash.listHash, using the anyHash() rule, which uses the hash codes of the contents. |
| 1854 | + * |
| 1855 | + * @return the hash code for this list. |
| 1856 | + */ |
| 1857 | + @Override public int hashCode() { |
| 1858 | + return Hash.listHash( Hash.<A>anyHash() ).hash( this ); |
| 1859 | + } |
| 1860 | + |
| 1861 | + /** |
| 1862 | + * Obtain a string representation of this list using the toString implementations of the members. Uses Show.listShow with F2 argument and may |
| 1863 | + * not be very performant. |
| 1864 | + * |
| 1865 | + * @return a String representation of the list |
| 1866 | + */ |
| 1867 | + @Override public String toString() { |
| 1868 | + return Show.listShow( Show.<A>anyShow() ).show( this ).foldLeft( new F2<String, Character, String>() { |
| 1869 | + @Override public String f( final String s, final Character c ) { |
| 1870 | + return s + c; |
| 1871 | + } |
| 1872 | + }, "" ); |
| 1873 | + } |
1831 | 1874 | }
|
0 commit comments