Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions core/src/main/java/fj/Equal.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,19 +535,19 @@ public static <A, B> Equal<Writer<A, B>> writerEqual(Equal<A> eq1, Equal<B> eq2)
}

/**
* Can the objects be checked for equality?
* @return True if the objects are not null and are an instance of the provided class.
*/
public static boolean equalsValidationCheck(Object o1, Object o2) {
java.lang.Class<?> c = o1.getClass();
if (o1 == null || !c.isInstance(o1)) {
return false;
}
if (o2 == null || !c.isInstance(o2)) {
return false;
* @return Returns none if no equality can be determined by checking the nullity and reference values, else the equality
*/
public static Option<Boolean> shallowEqualsO(Object o1, Object o2) {
if (o1 == null && o2 == null) {
return Option.some(true);
} else if (o1 == o2) {
return Option.some(true);
} else if (o1 != null && o2 != null) {
java.lang.Class<?> c = o1.getClass();
return c.isInstance(o2) ? Option.none() : Option.some(false);
} else {
return Option.some(false);
}
return true;

}

}
5 changes: 1 addition & 4 deletions core/src/main/java/fj/P1.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import fj.data.Array;
import fj.data.List;
import fj.data.Stream;
import fj.data.Validation;
import fj.function.Try0;

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

Expand Down Expand Up @@ -244,8 +242,7 @@ public String toString() {

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

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/fj/P2.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class P2<A, B> {

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

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/P3.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ public String toString() {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.p3Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual()).eq(this, (P3<A, B, C>) other);
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)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/P4.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ public String toString() {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.p4Equal(Equal.<A>anyEqual(), Equal.<B>anyEqual(), Equal.<C>anyEqual(), Equal.<D>anyEqual()).eq(this, (P4<A, B, C, D>) other);
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)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/P5.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ public String toString() {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
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);
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)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/P6.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,7 @@ public String toString() {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
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);
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)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/P7.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,7 @@ public String toString() {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
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);
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)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/P8.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,7 @@ public String toString() {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
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);
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)));
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/fj/data/Array.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fj.data;

import fj.Effect;
import fj.F;
import fj.F2;
import fj.P;
Expand Down Expand Up @@ -683,9 +682,7 @@ public boolean exists(final F<A, Boolean> f) {

@Override
public boolean equals(Object o) {
return !
Equal.equalsValidationCheck(this, o) ? false :
Equal.arrayEqual(Equal.<A>anyEqual()).eq(this, (Array<A>) o);
return Equal.shallowEqualsO(this, o).orSome(P.lazy(u -> Equal.arrayEqual(Equal.<A>anyEqual()).eq(this, (Array<A>) o)));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/data/Either.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public final <X> X either(final F<A, X> left, final F<B, X> right) {
@Override
public boolean equals(Object other) {

return !Equal.equalsValidationCheck(this, other) ? false :
Equal.eitherEqual(Equal.<A>anyEqual(), Equal.<B>anyEqual()).eq(this, (Either<A, B>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.eitherEqual(Equal.<A>anyEqual(), Equal.<B>anyEqual()).eq(this, (Either<A, B>) other)));
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/fj/data/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import fj.Equal;
import fj.F;
import fj.F2;
import fj.F3;
import fj.Function;
import fj.Hash;
import fj.Monoid;
Expand Down Expand Up @@ -1818,8 +1817,7 @@ private void copy() {
//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.equalsValidationCheck(this, obj) ? false :
Equal.listEqual(Equal.<A>anyEqual()).eq(this, (List<A>) obj);
return Equal.shallowEqualsO(this, obj).orSome(P.lazy(u -> Equal.listEqual(Equal.<A>anyEqual()).eq(this, (List<A>) obj)));
}

/**
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/fj/data/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static fj.Bottom.error;

import fj.Effect;
import fj.F;
import fj.F2;
import fj.P;
Expand Down Expand Up @@ -33,7 +32,6 @@
import static fj.data.Validation.parseLong;
import static fj.data.Validation.parseShort;
import static fj.Show.optionShow;
import static fj.Show.anyShow;

import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -628,8 +626,7 @@ public final boolean exists(final F<A, Boolean> f) {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.optionEqual(Equal.<A>anyEqual()).eq(this, (Option<A>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.optionEqual(Equal.<A>anyEqual()).eq(this, (Option<A>) other)));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/data/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public static <A> Seq<A> empty() {
@Override
public boolean equals(Object other) {

return !Equal.equalsValidationCheck(this, other) ? false :
Equal.seqEqual(Equal.<A>anyEqual()).eq(this, (Seq<A>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.seqEqual(Equal.<A>anyEqual()).eq(this, (Seq<A>) other)));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/fj/data/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static <A> Set<A> empty(final Ord<A> ord) {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false : Equal.setEqual(Equal.<A>anyEqual()).eq(this, (Set<A>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.setEqual(Equal.<A>anyEqual()).eq(this, (Set<A>) other)));
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/fj/data/Stream.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package fj.data;

import fj.Effect;
import fj.Equal;
import fj.Hash;
import fj.Show;
import fj.F;
import fj.F2;
import fj.F3;
import fj.Function;
import fj.Monoid;
import fj.Ord;
Expand Down Expand Up @@ -1239,8 +1237,7 @@ public final boolean forall(final F<A, Boolean> f) {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.streamEqual(Equal.<A>anyEqual()).eq(this, (Stream<A>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.streamEqual(Equal.<A>anyEqual()).eq(this, (Stream<A>) other)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/data/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ private Stream<String> drawTree(final Show<A> s) {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.treeEqual(Equal.<A>anyEqual()).eq(this, (Tree<A>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.treeEqual(Equal.<A>anyEqual()).eq(this, (Tree<A>) other)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/data/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public static <K, V> TreeMap<K, V> empty(final Ord<K> keyOrd) {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.treeMapEqual(Equal.<K>anyEqual(), Equal.<V>anyEqual()).eq(this, (TreeMap<K, V>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.treeMapEqual(Equal.<K>anyEqual(), Equal.<V>anyEqual()).eq(this, (TreeMap<K, V>) other)));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/data/Validation.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ public boolean exists(final F<T, Boolean> f) {

@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.validationEqual(Equal.<E>anyEqual(), Equal.<T>anyEqual()).eq(this, (Validation<E, T>) other);
return Equal.shallowEqualsO(this, other).orSome(P.lazy(u -> Equal.validationEqual(Equal.<E>anyEqual(), Equal.<T>anyEqual()).eq(this, (Validation<E, T>) other)));
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/java/fj/data/ListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ public void objectMethods() {

int max = 5;
List<Integer> list = List.range(1, max);

assertTrue(list.equals(list));
assertTrue(list.equals(List.range(1, max)));

assertFalse(list.equals(List.single(1)));
assertFalse(list.equals(true));
assertFalse(list.equals(null));



assertTrue(List.list(1, 2).toString().equals("List(1,2)"));

Expand Down