Skip to content

Commit 57fa068

Browse files
committed
Merge pull request #95 from mperry/eq-show-hash
Review implementation of equals for standard classes
2 parents 1258988 + 5a1f465 commit 57fa068

20 files changed

Lines changed: 36 additions & 55 deletions

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -535,19 +535,19 @@ public static <A, B> Equal<Writer<A, B>> writerEqual(Equal<A> eq1, Equal<B> eq2)
535535
}
536536

537537
/**
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;
538+
* @return Returns none if no equality can be determined by checking the nullity and reference values, else the equality
539+
*/
540+
public static Option<Boolean> shallowEqualsO(Object o1, Object o2) {
541+
if (o1 == null && o2 == null) {
542+
return Option.some(true);
543+
} else if (o1 == o2) {
544+
return Option.some(true);
545+
} else if (o1 != null && o2 != null) {
546+
java.lang.Class<?> c = o1.getClass();
547+
return c.isInstance(o2) ? Option.none() : Option.some(false);
548+
} else {
549+
return Option.some(false);
548550
}
549-
return true;
550-
551551
}
552552

553553
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import fj.data.Array;
66
import fj.data.List;
77
import fj.data.Stream;
8-
import fj.data.Validation;
9-
import fj.function.Try0;
108

119
public abstract class P1<A> implements F0<A> {
1210

@@ -244,8 +242,7 @@ public String toString() {
244242

245243
@Override
246244
public boolean equals(Object other) {
247-
return !Equal.equalsValidationCheck(this, other) ? false :
248-
Equal.p1Equal(Equal.<A>anyEqual()).eq(this, (P1<A>) other);
245+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.p1Equal(Equal.<A>anyEqual()).eq(this, (P1<A>) other)));
249246
}
250247

251248
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public abstract class P2<A, B> {
2626

2727
@Override
2828
public boolean equals(Object other) {
29-
return Equal.p2Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual()).eq(this, (P2<A, B>) other);
29+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.p2Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual()).eq(this, (P2<A, B>) other)));
3030
}
3131

3232
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ public String toString() {
192192

193193
@Override
194194
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);
195+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.p3Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual()).eq(this, (P3<A, B, C>) other)));
197196
}
198197

199198
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ public String toString() {
265265

266266
@Override
267267
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);
268+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.p4Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual(), Equal.<D>anyEqual()).eq(this, (P4<A, B, C, D>) other)));
270269
}
271270

272271
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ public String toString() {
344344

345345
@Override
346346
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);
347+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> 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)));
349348
}
350349

351350
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,7 @@ public String toString() {
435435

436436
@Override
437437
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);
438+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> 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)));
440439
}
441440

442441
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,7 @@ public String toString() {
529529

530530
@Override
531531
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);
532+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> 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)));
534533
}
535534

536535
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,7 @@ public String toString() {
635635

636636
@Override
637637
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);
638+
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> 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)));
640639
}
641640

642641
@Override

core/src/main/java/fj/data/Array.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fj.data;
22

3-
import fj.Effect;
43
import fj.F;
54
import fj.F2;
65
import fj.P;
@@ -683,9 +682,7 @@ public boolean exists(final F<A, Boolean> f) {
683682

684683
@Override
685684
public boolean equals(Object o) {
686-
return !
687-
Equal.equalsValidationCheck(this, o) ? false :
688-
Equal.arrayEqual(Equal.<A>anyEqual()).eq(this, (Array<A>) o);
685+
return Equal.shallowEqualsO(this, o).orSome(P.lazy(u -> Equal.arrayEqual(Equal.<A>anyEqual()).eq(this, (Array<A>) o)));
689686
}
690687

691688
/**

0 commit comments

Comments
 (0)