aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/util/ExtraLists.java
blob: 3c14a4562b50abd83b2fdf16a3fa23aeb1ed1210 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.zwobble.couscous.util;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import java.util.Iterator;
import java.util.List;
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 java.util.stream.StreamSupport;

public class ExtraLists {
    public static <T> List<T> list() {
        return ImmutableList.of();
    }

    public static <T> List<T> list(T value1) {
        return ImmutableList.of(value1);
    }

    public static <T> List<T> list(T value1, T value2) {
        return ImmutableList.of(value1, value2);
    }

    public static <T> List<T> list(T value1, T value2, T value3) {
        return ImmutableList.of(value1, value2, value3);
    }

    public static <T> List<T> list(T... values) {
        return ImmutableList.copyOf(values);
    }

    public static <T> List<T> cons(T value, Iterable<T> list) {
        return concat(list(value), list);
    }

    public static <T> List<T> append(Iterable<? extends T> iterable, T value) {
        return Stream.concat(ExtraIterables.stream(iterable), Stream.of(value)).collect(Collectors.toList());
    }

    public static <T> List<T> concat(Iterable<? extends T> first, Iterable<? extends T> second) {
        return ImmutableList.copyOf(Iterables.concat(first, second));
    }

    public static <T> List<T> concat(Iterable<? extends T> first, Iterable<? extends T> second, Iterable<? extends T> third) {
        return ImmutableList.copyOf(Iterables.concat(first, second, third));
    }

    @SuppressWarnings("unchecked")
    public static <T, R extends T> List<R> ofType(List<T> list, Class<R> clazz) {
        return (List<R>)list.stream()
            .filter(clazz::isInstance)
            .collect(Collectors.toList());
    }

    @SuppressWarnings("unchecked")
    public static <T, R, E extends Exception> List<R> flatMap(
            Stream<T> stream,
            CheckedFunction<T, Iterable<R>, E> function) throws E {
        try {
            return stream
                .flatMap(value -> {
                    try {
                        return StreamSupport.stream(function.apply(value).spliterator(), false);
                    } catch (Exception exception) {
                        throw new StashedException(exception);
                    }
                })
                .collect(Collectors.toList());
        } catch (StashedException exception) {
            throw (E)exception.exception;
        }
    }

    public static <T> List<T> eagerFilter(Iterable<T> iterable, Predicate<T> predicate) {
        return StreamSupport.stream(iterable.spliterator(), false)
            .filter(predicate)
            .collect(Collectors.toList());
    }

    public static <T, R> List<R> eagerMap(
            Iterable<T> iterable,
            Function<T, R> function) {
        return StreamSupport.stream(iterable.spliterator(), false)
            .map(function)
            .collect(Collectors.toList());
    }

    public static <T, R> List<R> eagerMapWithIndex(
        Iterable<T> iterable,
        FunctionWithIndex<T, R> function)
    {
        ImmutableList.Builder<R> builder = ImmutableList.builder();
        int index = 0;
        Iterator<T> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            builder.add(function.apply(iterator.next(), index));
            index++;
        }
        return builder.build();
    }

    public interface FunctionWithIndex<T, R> {
        R apply(T value, int index);
    }

    public static <T1, T2, R> List<R> eagerMap(
        Iterable<T1> iterable1,
        Iterable<T2> iterable2,
        BiFunction<T1, T2, R> function)
    {
        Iterator<T1> iterator1 = iterable1.iterator();
        Iterator<T2> iterator2 = iterable2.iterator();

        ImmutableList.Builder<R> result = ImmutableList.builder();

        while (iterator1.hasNext() && iterator2.hasNext()) {
            result.add(function.apply(iterator1.next(), iterator2.next()));
        }

        if (iterator1.hasNext()) {
            throw new RuntimeException("iterable1 longer than iterable2");
        }

        if (iterator2.hasNext()) {
            throw new RuntimeException("iterable2 longer than iterable1");
        }

        return result.build();
    }

    public static <T, R> List<R> eagerFlatMap(
        Iterable<T> iterable,
        Function<T, Iterable<R>> function) {
        return StreamSupport.stream(iterable.spliterator(), false)
            .flatMap(element -> StreamSupport.stream(function.apply(element).spliterator(), false))
            .collect(Collectors.toList());
    }
    
    @FunctionalInterface
    public interface CheckedFunction<T, R, E extends Exception> {
        R apply(T value) throws E;
    }
    
    private static class StashedException extends RuntimeException {
        private static final long serialVersionUID = 1L;
        
        private Exception exception;
        
        private StashedException(Exception exception) {
            this.exception = exception;
        }
    }

    public static <T> List<T> copyOf(Iterable<T> iterable) {
        return ImmutableList.copyOf(iterable);
    }

    public static <T> T getLast(T[] values) {
        return values[values.length - 1];
    }

    public static <T> T getLast(List<T> values) {
        return values.get(values.size() - 1);
    }
}