-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTuple.java
More file actions
64 lines (50 loc) · 1.87 KB
/
Copy pathTuple.java
File metadata and controls
64 lines (50 loc) · 1.87 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
package jaskell.util;
import java.util.*;
public interface Tuple<Head, Tail, Last, ButLast> {
int size();
Head head();
Tail tail();
Last last();
ButLast butLast();
Object get(int pos) throws IndexOutOfBoundsException;
default List<Object> toList() {
List<Object> result = new ArrayList<>();
for (int i = 0; i < size(); i++) {
result.add(get(i));
}
return result;
}
default Map<String, Object> toMap(String... keys) throws IllegalArgumentException {
if (keys.length != size()) {
throw new IllegalArgumentException(
String.format("tuple%d has %d fields but %d keys given %s",
size(), size(), keys.length, Arrays.asList(keys)));
}
Map<String, Object> result = new HashMap<>();
for (int pos = 0; pos < keys.length; pos++) {
result.put(keys[pos], get(pos));
}
return result;
}
static <T, U> Tuple2<T, U> tuple(T t, U u) {
return new Tuple2<>(t, u);
}
static <T, U, V> Tuple3<T, U, V> tuple(T t, U u, V v) {
return new Tuple3<>(t, u, v);
}
static <T, U, V, W> Tuple4<T, U, V, W> tuple(T t, U u, V v, W w) {
return new Tuple4<>(t, u, v, w);
}
static <T, U, V, W, X> Tuple5<T, U, V, W, X> tuple(T t, U u, V v, W w, X x) {
return new Tuple5<>(t, u, v, w, x);
}
static <T, U, V, W, X, Y> Tuple6<T, U, V, W, X, Y> tuple(T t, U u, V v, W w, X x, Y y){
return new Tuple6<>(t, u, v, w, x, y);
}
static <T, U, V, W, X, Y, Z> Tuple7<T, U, V, W, X, Y, Z> tuple(T t, U u, V v, W w, X x, Y y, Z z){
return new Tuple7<>(t, u, v, w, x, y, z);
}
static <S, T, U, V, W, X, Y, Z> Tuple8<S, T, U, V, W, X, Y, Z> tuple(S s, T t, U u, V v, W w, X x, Y y, Z z){
return new Tuple8<>(s, t, u, v, w, x, y, z);
}
}