forked from palatable/lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualityAwareIso.java
More file actions
72 lines (60 loc) · 2.54 KB
/
EqualityAwareIso.java
File metadata and controls
72 lines (60 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package testsupport;
import com.jnape.palatable.lambda.functor.Applicative;
import com.jnape.palatable.lambda.functor.Functor;
import com.jnape.palatable.lambda.functor.Profunctor;
import com.jnape.palatable.lambda.lens.Iso;
import com.jnape.palatable.lambda.lens.LensLike;
import com.jnape.palatable.lambda.monad.Monad;
import java.util.Objects;
import java.util.function.Function;
import static com.jnape.palatable.lambda.functions.builtin.fn2.Both.both;
import static com.jnape.palatable.lambda.lens.functions.View.view;
public final class EqualityAwareIso<S, T, A, B> implements Iso<S, T, A, B> {
private final S s;
private final B b;
private final Iso<S, T, A, B> iso;
public EqualityAwareIso(S s, B b, Iso<S, T, A, B> iso) {
this.s = s;
this.b = b;
this.iso = iso;
}
@Override
public <P extends Profunctor, F extends Functor, FB extends Functor<B, F>, FT extends Functor<T, F>, PAFB extends Profunctor<A, FB, P>, PSFT extends Profunctor<S, FT, P>> PSFT apply(
PAFB pafb) {
return iso.<P, F, FB, FT, PAFB, PSFT>apply(pafb);
}
@Override
public <F extends Functor, FT extends Functor<T, F>, FB extends Functor<B, F>> FT apply(
Function<? super A, ? extends FB> fn, S s) {
return iso.apply(fn, s);
}
@Override
public <U> EqualityAwareIso<S, U, A, B> fmap(Function<? super T, ? extends U> fn) {
return new EqualityAwareIso<>(s, b, iso.fmap(fn));
}
@Override
public <U> EqualityAwareIso<S, U, A, B> pure(U u) {
return new EqualityAwareIso<>(s, b, iso.pure(u));
}
@Override
public <U> EqualityAwareIso<S, U, A, B> zip(
Applicative<Function<? super T, ? extends U>, LensLike<S, ?, A, B, Iso>> appFn) {
return new EqualityAwareIso<>(s, b, iso.zip(appFn));
}
@Override
public <U> EqualityAwareIso<S, U, A, B> flatMap(
Function<? super T, ? extends Monad<U, LensLike<S, ?, A, B, Iso>>> fn) {
return new EqualityAwareIso<>(s, b, iso.flatMap(fn));
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object other) {
if (other instanceof EqualityAwareIso) {
Iso<S, T, A, B> that = (EqualityAwareIso<S, T, A, B>) other;
Boolean sameForward = both(view(this), view(that)).apply(s).into(Objects::equals);
Boolean sameReverse = both(view(this.mirror()), view(that.mirror())).apply(b).into(Objects::equals);
return sameForward && sameReverse;
}
return false;
}
}