forked from palatable/lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquatableW.java
More file actions
49 lines (38 loc) · 1.39 KB
/
EquatableW.java
File metadata and controls
49 lines (38 loc) · 1.39 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
package testsupport;
import com.jnape.palatable.lambda.comonad.Comonad;
import com.jnape.palatable.lambda.functions.Fn1;
import java.util.Objects;
import static com.jnape.palatable.lambda.functions.Fn1.fn1;
public final class EquatableW<W extends Comonad<?, W>, A> implements Comonad<A, EquatableW<W, ?>> {
private final Comonad<A, W> wa;
private final Fn1<? super W, ?> equatable;
public EquatableW(Comonad<A, W> wa, Fn1<? super W, ?> equatable) {
this.wa = wa;
this.equatable = equatable;
}
@Override
public A extract() {
return wa.extract();
}
@Override
public <B> Comonad<B, EquatableW<W, ?>> extendImpl(Fn1<? super Comonad<A, EquatableW<W, ?>>, ? extends B> f) {
return new EquatableW<>(wa.extendImpl(f.contraMap(fn1(eq -> new EquatableW<>(eq, equatable)))), equatable);
}
@Override
public <B> EquatableW<W, B> fmap(Fn1<? super A, ? extends B> fn) {
return new EquatableW<>(wa.fmap(fn), equatable);
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object other) {
if (other instanceof EquatableW<?, ?>) {
EquatableW<W, ?> that = (EquatableW<W, ?>) other;
return Objects.equals(equatable.apply((W) wa), that.equatable.apply((W) that.wa));
}
return false;
}
@Override
public int hashCode() {
return super.hashCode();
}
}