aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/Lists.java
blob: 51e76b02a4172749a40733d525ea5cae88147380 (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
package ch.epfl.xblast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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 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.
     */
    public static <T> List<T> reversed(List<T> l) {
        List<T> r = new ArrayList<>(l);
        Collections.reverse(r);
        return r;
    }

    /**
     * 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());
    }

}