-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTuple2.java
More file actions
104 lines (83 loc) · 2.29 KB
/
Copy pathTuple2.java
File metadata and controls
104 lines (83 loc) · 2.29 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package jaskell.util;
import java.util.Objects;
/**
* @param <T>
* @param <U>
*/
public class Tuple2<T, U> implements Tuple<T, U, U, T> {
final T item0;
final U item1;
public Tuple2(T item0, U item1) {
this.item0 = item0;
this.item1 = item1;
}
public T getItem0() {
return item0;
}
public U getItem1() {
return item1;
}
public <V> Tuple2<V, U> item0(V item) {
return new Tuple2<>(item, getItem1());
}
public <V> Tuple2<T, V> item1(V item) {
return new Tuple2<>(getItem0(), item);
}
public <V> Tuple3<T, U, V> add(V item) {
return new Tuple3<>(getItem0(), getItem1(), item);
}
public <V> Try<Tuple3<T, U, V>> tryAdd(Try<V> tryItem) {
return tryItem.map(item -> new Tuple3<>(getItem0(), getItem1(), item));
}
public <R> R uncurry(BiFunction<T, U, R> functor) throws Exception {
return functor.apply(getItem0(), getItem1());
}
public <R> Try<R> tryIt(BiFunction<T, U, R> functor) throws Exception {
return functor.collect(getItem0(), getItem1());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Tuple2)) return false;
Tuple2<?, ?> tuple2 = (Tuple2<?, ?>) o;
return Objects.equals(getItem0(), tuple2.getItem0()) && Objects.equals(getItem1(), tuple2.getItem1());
}
@Override
public int hashCode() {
return Objects.hash(getItem0(), getItem1());
}
public static <T, U> Try<Tuple2<T, U>> liftA(Try<T> t0, Try<U> t1) {
return Try.joinMap(t0, t1, Tuple2::new);
}
@Override
public int size() {
return 2;
}
@Override
public Object get(int pos) throws IndexOutOfBoundsException {
switch (pos) {
case 0:
return getItem0();
case 1:
return getItem1();
default:
throw new IndexOutOfBoundsException("tuple2 only accept 0 or 1 pos");
}
}
@Override
public T head() {
return getItem0();
}
@Override
public U tail() {
return getItem1();
}
@Override
public U last() {
return getItem1();
}
@Override
public T butLast() {
return getItem0();
}
}