/
Lists.java
84 lines (67 loc) · 2.19 KB
/
Lists.java
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
package aQute.bnd.unmodifiable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collector;
@SuppressWarnings("unchecked")
public class Lists {
private Lists() {}
public static <E> List<E> of() {
return (List<E>) ImmutableList.EMPTY;
}
public static <E> List<E> of(E e1) {
return new ImmutableList<>(e1);
}
public static <E> List<E> of(E e1, E e2) {
return new ImmutableList<>(e1, e2);
}
public static <E> List<E> of(E e1, E e2, E e3) {
return new ImmutableList<>(e1, e2, e3);
}
public static <E> List<E> of(E e1, E e2, E e3, E e4) {
return new ImmutableList<>(e1, e2, e3, e4);
}
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
return new ImmutableList<>(e1, e2, e3, e4, e5);
}
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
return new ImmutableList<>(e1, e2, e3, e4, e5, e6);
}
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
return new ImmutableList<>(e1, e2, e3, e4, e5, e6, e7);
}
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
return new ImmutableList<>(e1, e2, e3, e4, e5, e6, e7, e8);
}
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
return new ImmutableList<>(e1, e2, e3, e4, e5, e6, e7, e8, e9);
}
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
return new ImmutableList<>(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
}
@SafeVarargs
public static <E> List<E> of(E... elements) {
int length = elements.length;
if (length == 0) {
return of();
}
return new ImmutableList<E>(Arrays.copyOf(elements, length, Object[].class));
}
public static <E> List<E> copyOf(Collection<? extends E> collection) {
if (collection instanceof ImmutableList) {
return (List<E>) collection;
}
if (collection.isEmpty()) {
return of();
}
return new ImmutableList<E>(collection.toArray());
}
public static <E> Collector<E, ?, List<E>> toList() {
return Collector.of((Supplier<List<E>>) ArrayList::new, List::add, (l, r) -> {
l.addAll(r);
return l;
}, Lists::copyOf);
}
}