aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/Lists.java
blob: cc6a1c8a812f0be2e1e5cee0d3b5b9cdc5349b69 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ch.epfl.xblast;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Lists utility class providing common operations on lists.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public final class Lists {

    /**
     * Returns an immutable copy of a list.
     *
     * @param l   a list
     * @param <T> the element type
     * @return the immutable list
     */
    public static <T> List<T> immutableList(List<T> l) {
        return Collections.unmodifiableList(new ArrayList<>(l));
    }

    /**
     * Returns an immutable copy of a map.
     *
     * @param m   a mab
     * @param <K> the key type
     * @param <V> the value type
     * @return the immutable copy
     */
    public static <K, V> Map<K, V> immutableMap(Map<K, V> m) {
        return Collections.unmodifiableMap(new HashMap<>(m));
    }

    /**
     * Returns a symmetric version of the list, without repeating the last element of the input list.
     * For instance, mirrored([kay]) will return [kayak].
     *
     * @param l   the input list
     * @param <T> the type of the list's elements
     * @return the mirrored list
     * @throws IllegalArgumentException if the given list is empty
     */
    public static <T> List<T> mirrored(List<T> l) {
        if (l == null || l.size() == 0) throw new IllegalArgumentException();

        return Stream
                .concat(l.stream(), Lists.reversed(l).stream().skip(1))
                .collect(Collectors.toList());
    }

    /**
     * Returns a reversed copy of the given list, leaving the original one unmodified.
     *
     * @param l   the list to reverse
     * @param <T> the type of the list's elements
     * @return a reversed copy of the list
     */
    private static <T> List<T> reversed(List<T> l) {
        List<T> r = new ArrayList<>(l);
        Collections.reverse(r);
        return r;
    }

    /**
     * Returns an immutable copy of the given list, sorted using the supplied comparator.
     *
     * @param l   the list to sort
     * @param cmp the Comparator to use
     * @param <T> the type of the list's elements
     * @return the sorted list
     */
    public static <T> List<T> sorted(List<T> l, Comparator<T> cmp) {
        List<T> r = new ArrayList<>(l);
        Collections.sort(r, cmp);
        return Collections.unmodifiableList(r);
    }

    /**
     * Returns a copy of the given list surrounded with element e.
     *
     * @param l   the list
     * @param e   the element to insert
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element inserted at start and end
     */
    public static <T> List<T> surrounded(List<T> l, T e) {
        return Lists.inserted(Lists.inserted(l, 0, e), l.size() + 1, e);
    }

    /**
     * Returns a copy of the given list with element e inserted at index i.
     *
     * @param l   the list
     * @param i   the insertion index
     * @param e   the element to insert
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element inserted
     */
    public static <T> List<T> inserted(List<T> l, int i, T e) {
        List<T> r = new LinkedList<>(l);
        r.add(i, e);
        return r;
    }

    /**
     * Returns a copy of the given list with element e prepended.
     *
     * @param l   the list
     * @param e   the element to prepend
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element prepended
     */
    public static <T> List<T> prepended(List<T> l, T e) {
        return Lists.inserted(l, 0, e);
    }

    /**
     * Returns a copy of the given list with element e appended.
     *
     * @param l   the list
     * @param e   the element to append
     * @param <T> the type of the list's elements
     * @return a copy of the list with the element appended
     */
    public static <T> List<T> appended(List<T> l, T e) {
        return Lists.inserted(l, l.size(), e);
    }

    /**
     * Returns all the permutations of the elements of the given list.
     *
     * @param l   given list
     * @param <T> the type of the list's elements
     * @return a list of all the permutations of the list
     * @throws IllegalArgumentException if the given list is null
     */
    public static <T> List<List<T>> permutations(List<T> l) {
        if (l == null) throw new IllegalArgumentException();
        if (l.size() <= 1) return Collections.singletonList(l);
        if (l.size() == 2) return Arrays.asList(l, Lists.reversed(l));

        List<List<T>> p = new LinkedList<>();
        for (List<T> sp : Lists.permutations(l.subList(1, l.size())))
            for (int i = 0; i <= sp.size(); ++i)
                p.add(Lists.inserted(sp, i, l.get(0)));

        return p;
    }

    /**
     * Returns a copy of the given list with the first n items dropped.
     *
     * @param l   given list
     * @param n   number of items to drop
     * @param <T> the type of the list's elements
     * @return a copy of the list with the n first element dropped
     */
    public static <T> List<T> firstDropped(List<T> l, int n) {
        if (Objects.isNull(l))
            return Collections.emptyList();

        return new ArrayList<>(l.subList(n, l.size()));
    }

    /**
     * Returns an immutable copy of the given lists, concatenated.
     *
     * @param lists the lists to concatenate
     * @param <T>   the type of the list's elements
     * @return the list result of the concatenation
     */
    public static <T> List<T> concatenated(List<T>... lists) {
        return Collections.unmodifiableList(
                Stream.of(lists)
                        .map(List::stream)
                        .reduce(Stream::concat).orElse(Stream.empty())
                        .collect(Collectors.toList()));
    }

}