Skip to content

Commit e0c4efa

Browse files
committed
#75: Added equals, hashCode and toString to classes P's, Array, Either, List, Option, Seq, Set, Stream, Tree, TreeMap, Validation
1 parent ef3d99b commit e0c4efa

25 files changed

Lines changed: 383 additions & 107 deletions

core/src/main/java/fj/Equal.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ public static <A> Equal<Option<A>> optionEqual(final Equal<A> ea) {
238238
o1.isSome() && o2.isSome() && ea.f.f(o1.some()).f(o2.some()));
239239
}
240240

241+
public static <A> Equal<Seq<A>> seqEqual(final Equal<A> e) {
242+
return equal(s1 -> s2 -> streamEqual(e).eq(s1.toStream(), s2.toStream()));
243+
}
244+
241245
/**
242246
* An equal instance for the {@link Stream} type.
243247
*
@@ -530,4 +534,20 @@ public static <A, B> Equal<Writer<A, B>> writerEqual(Equal<A> eq1, Equal<B> eq2)
530534
return equal(w1 -> w2 -> p2Equal(eq1, eq2).eq(w1.run(), w2.run()));
531535
}
532536

537+
/**
538+
* Can the objects be checked for equality?
539+
* @return True if the objects are not null and are an instance of the provided class.
540+
*/
541+
public static boolean equalsValidationCheck(Object o1, Object o2) {
542+
java.lang.Class<?> c = o1.getClass();
543+
if (o1 == null || !c.isInstance(o1)) {
544+
return false;
545+
}
546+
if (o2 == null || !c.isInstance(o2)) {
547+
return false;
548+
}
549+
return true;
550+
551+
}
552+
533553
}

core/src/main/java/fj/Hash.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
import static fj.Function.compose;
44

5-
import fj.data.Array;
6-
import fj.data.Either;
7-
import fj.data.List;
8-
import fj.data.NonEmptyList;
9-
import fj.data.Option;
10-
import fj.data.Stream;
11-
import fj.data.Tree;
12-
import fj.data.Validation;
5+
import fj.data.*;
136
import fj.data.vector.V2;
147
import fj.data.vector.V3;
158
import fj.data.vector.V4;
@@ -203,6 +196,14 @@ public static <A> Hash<Option<A>> optionHash(final Hash<A> ha) {
203196
return hash(o -> o.isNone() ? 0 : ha.hash(o.some()));
204197
}
205198

199+
public static <A> Hash<Seq<A>> seqHash(final Hash<A> h) {
200+
return hash(s -> streamHash(h).hash(s.toStream()));
201+
}
202+
203+
public static <A> Hash<Set<A>> setHash(final Hash<A> h) {
204+
return hash(s -> streamHash(h).hash(s.toStream()));
205+
}
206+
206207
/**
207208
* A hash instance for the {@link Stream} type.
208209
*
@@ -253,6 +254,10 @@ public static <A> Hash<Tree<A>> treeHash(final Hash<A> ha) {
253254
return streamHash(ha).comap(Tree.<A>flatten_());
254255
}
255256

257+
public static <K, V> Hash<TreeMap<K, V>> treeMapHash(final Hash<K> h, final Hash<V> v) {
258+
return hash(t -> streamHash(Hash.p2Hash(h, v)).hash(t.toStream()));
259+
}
260+
256261
/**
257262
* A hash instance for a product-1.
258263
*

core/src/main/java/fj/P1.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,20 @@ public A f(final B b) {
232232
};
233233
}
234234

235+
@Override
235236
public String toString() {
236237
return Show.p1Show(Show.<A>anyShow()).showS(this);
237238
}
239+
240+
@Override
241+
public boolean equals(Object other) {
242+
return !Equal.equalsValidationCheck(this, other) ? false :
243+
Equal.p1Equal(Equal.<A>anyEqual()).eq(this, (P1<A>) other);
244+
}
245+
246+
@Override
247+
public int hashCode() {
248+
return Hash.p1Hash(Hash.<A>anyHash()).hash(this);
249+
}
250+
238251
}

core/src/main/java/fj/P2.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public abstract class P2<A, B> {
2424
*/
2525
public abstract B _2();
2626

27+
@Override
28+
public boolean equals(Object other) {
29+
return Equal.p2Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual()).eq(this, (P2<A, B>) other);
30+
}
31+
32+
@Override
33+
public int hashCode() {
34+
return Hash.p2Hash(Hash.<A>anyHash(), Hash.<B>anyHash()).hash(this);
35+
}
36+
2737
/**
2838
* Swaps the elements around in this product.
2939
*
@@ -344,6 +354,7 @@ public C f(final A a, final B b) {
344354
};
345355
}
346356

357+
@Override
347358
public String toString() {
348359
return Show.p2Show(Show.<A>anyShow(), Show.<B>anyShow()).showS(this);
349360
}

core/src/main/java/fj/P3.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,20 @@ public C f(final P3<A, B, C> p) {
185185
};
186186
}
187187

188+
@Override
188189
public String toString() {
189190
return Show.p3Show(Show.<A>anyShow(), Show.<B>anyShow(), Show.<C>anyShow()).showS(this);
190191
}
191192

193+
@Override
194+
public boolean equals(Object other) {
195+
return !Equal.equalsValidationCheck(this, other) ? false :
196+
Equal.p3Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual()).eq(this, (P3<A, B, C>) other);
197+
}
198+
199+
@Override
200+
public int hashCode() {
201+
return Hash.p3Hash(Hash.<A>anyHash(), Hash.<B>anyHash(), Hash.<C>anyHash()).hash(this);
202+
}
192203

193204
}

core/src/main/java/fj/P4.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,20 @@ public D f(final P4<A, B, C, D> p) {
258258
};
259259
}
260260

261+
@Override
261262
public String toString() {
262263
return Show.p4Show(Show.<A>anyShow(), Show.<B>anyShow(), Show.<C>anyShow(), Show.<D>anyShow()).showS(this);
263264
}
264265

266+
@Override
267+
public boolean equals(Object other) {
268+
return !Equal.equalsValidationCheck(this, other) ? false :
269+
Equal.p4Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual(), Equal.<D>anyEqual()).eq(this, (P4<A, B, C, D>) other);
270+
}
271+
272+
@Override
273+
public int hashCode() {
274+
return Hash.p4Hash(Hash.<A>anyHash(), Hash.<B>anyHash(), Hash.<C>anyHash(), Hash.<D>anyHash()).hash(this);
275+
}
276+
265277
}

core/src/main/java/fj/P5.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,20 @@ public E f(final P5<A, B, C, D, E> p) {
337337
};
338338
}
339339

340+
@Override
340341
public String toString() {
341342
return Show.p5Show(Show.<A>anyShow(), Show.<B>anyShow(), Show.<C>anyShow(), Show.<D>anyShow(), Show.<E>anyShow()).showS(this);
342343
}
343344

345+
@Override
346+
public boolean equals(Object other) {
347+
return !Equal.equalsValidationCheck(this, other) ? false :
348+
Equal.p5Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual(), Equal.<D>anyEqual(), Equal.<E>anyEqual()).eq(this, (P5<A, B, C, D, E>) other);
349+
}
350+
351+
@Override
352+
public int hashCode() {
353+
return Hash.p5Hash(Hash.<A>anyHash(), Hash.<B>anyHash(), Hash.<C>anyHash(), Hash.<D>anyHash(), Hash.<E>anyHash()).hash(this);
354+
}
344355

345356
}

core/src/main/java/fj/P6.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,21 @@ public E f(final P6<A, B, C, D, E, F$> p) {
427427
};
428428
}
429429

430+
@Override
430431
public String toString() {
431432
return Show.p6Show(Show.<A>anyShow(), Show.<B>anyShow(), Show.<C>anyShow(), Show.<D>anyShow(), Show.<E>anyShow(), Show.<F>anyShow()).showS(this);
432433
}
433434

435+
436+
@Override
437+
public boolean equals(Object other) {
438+
return !Equal.equalsValidationCheck(this, other) ? false :
439+
Equal.p6Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual(), Equal.<D>anyEqual(), Equal.<E>anyEqual(), Equal.<F>anyEqual()).eq(this, (P6<A, B, C, D, E, F>) other);
440+
}
441+
442+
@Override
443+
public int hashCode() {
444+
return Hash.p6Hash(Hash.<A>anyHash(), Hash.<B>anyHash(), Hash.<C>anyHash(), Hash.<D>anyHash(), Hash.<E>anyHash(), Hash.<F>anyHash()).hash(this);
445+
}
446+
434447
}

core/src/main/java/fj/P7.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,20 @@ public G f(final P7<A, B, C, D, E, F$, G> p) {
522522
};
523523
}
524524

525+
@Override
525526
public String toString() {
526527
return Show.p7Show(Show.<A>anyShow(), Show.<B>anyShow(), Show.<C>anyShow(), Show.<D>anyShow(), Show.<E>anyShow(), Show.<F>anyShow(), Show.<G>anyShow()).showS(this);
527528
}
528529

530+
@Override
531+
public boolean equals(Object other) {
532+
return !Equal.equalsValidationCheck(this, other) ? false :
533+
Equal.p7Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual(), Equal.<D>anyEqual(), Equal.<E>anyEqual(), Equal.<F>anyEqual(), Equal.<G>anyEqual()).eq(this, (P7<A, B, C, D, E, F, G>) other);
534+
}
535+
536+
@Override
537+
public int hashCode() {
538+
return Hash.p7Hash(Hash.<A>anyHash(), Hash.<B>anyHash(), Hash.<C>anyHash(), Hash.<D>anyHash(), Hash.<E>anyHash(), Hash.<F>anyHash(), Hash.<G>anyHash()).hash(this);
539+
}
529540

530541
}

core/src/main/java/fj/P8.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,20 @@ public H f(final P8<A, B, C, D, E, F$, G, H> p) {
628628
};
629629
}
630630

631+
@Override
631632
public String toString() {
632633
return Show.p8Show(Show.<A>anyShow(), Show.<B>anyShow(), Show.<C>anyShow(), Show.<D>anyShow(), Show.<E>anyShow(), Show.<F>anyShow(), Show.<G>anyShow(), Show.<H>anyShow()).showS(this);
633634
}
634635

636+
@Override
637+
public boolean equals(Object other) {
638+
return !Equal.equalsValidationCheck(this, other) ? false :
639+
Equal.p8Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual(), Equal.<D>anyEqual(), Equal.<E>anyEqual(), Equal.<F>anyEqual(), Equal.<G>anyEqual(), Equal.<H>anyEqual()).eq(this, (P8<A, B, C, D, E, F, G, H>) other);
640+
}
641+
642+
@Override
643+
public int hashCode() {
644+
return Hash.p8Hash(Hash.<A>anyHash(), Hash.<B>anyHash(), Hash.<C>anyHash(), Hash.<D>anyHash(), Hash.<E>anyHash(), Hash.<F>anyHash(), Hash.<G>anyHash(), Hash.<H>anyHash()).hash(this);
645+
}
646+
635647
}

0 commit comments

Comments
 (0)