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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package org.zwobble.mammoth.internal.util;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static org.zwobble.mammoth.internal.util.Iterables.stream;
public class Lists {
public static <T> List<T> list() {
return Collections.emptyList();
}
public static <T> List<T> list(T value1) {
return Collections.singletonList(value1);
}
public static <T> List<T> list(T value1, T value2) {
return asList(value1, value2);
}
public static <T> List<T> list(T value1, T value2, T value3) {
return asList(value1, value2, value3);
}
@SafeVarargs
public static <T> List<T> list(T... values) {
return asList(values);
}
public static <T> List<T> cons(T head, Iterable<T> tail) {
return eagerConcat(list(head), tail);
}
public static <T> List<T> eagerConcat(Iterable<T> first, Iterable<T> second) {
return Stream.concat(stream(first), stream(second)).collect(Collectors.toList());
}
public static <T> List<T> eagerConcat(Collection<T> first, Collection<T> second, Collection<T> third) {
List<T> result = new ArrayList<>(first.size() + second.size() + third.size());
result.addAll(first);
result.addAll(second);
result.addAll(third);
return result;
}
public static <T> List<T> eagerFilter(Iterable<T> iterable, Predicate<T> predicate) {
return stream(iterable).filter(predicate).collect(Collectors.toList());
}
public static <T, R> List<R> eagerMap(Iterable<T> iterable, Function<T, R> function) {
return stream(iterable).map(function).collect(Collectors.toList());
}
public static <T, R> List<R> eagerMapWithIndex(Iterable<T> iterable, BiFunction<Integer, T, R> function) {
return toList(Iterables.lazyMapWithIndex(iterable, function));
}
public static <T, R> List<R> eagerFlatMap(Iterable<T> iterable, Function<T, Iterable<R>> function) {
return stream(iterable)
.flatMap(element -> stream(function.apply(element)))
.collect(Collectors.toList());
}
public static <T, R extends Comparable<R>> List<T> orderedBy(Iterable<T> iterable, Function<T, R> getKey) {
return stream(iterable)
.sorted(orderBy(getKey))
.collect(Collectors.toList());
}
private static <T, R extends Comparable<R>> Comparator<T> orderBy(Function<T, R> getKey) {
return (first, second) -> getKey.apply(first).compareTo(getKey.apply(second));
}
public static <T> List<T> toList(Iterable<T> iterable) {
return stream(iterable).collect(Collectors.toList());
}
public static <T> Optional<T> tryGetFirst(List<T> list) {
if (list.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(list.get(0));
}
}
public static <T> Optional<T> tryGetLast(List<T> list) {
if (list.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(list.get(list.size() - 1));
}
}
public static <T> List<T> skip(List<T> list, int count) {
return list.subList(Math.min(list.size(), count), list.size());
}
public static <T> Iterable<T> reversed(List<T> list) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
ListIterator<T> iterator = list.listIterator(list.size());
return new Iterator<T>() {
@Override
public boolean hasNext() {
return iterator.hasPrevious();
}
@Override
public T next() {
return iterator.previous();
}
};
}
};
}
}
|