diff options
Diffstat (limited to 'src/ch/epfl/xblast/Lists.java')
-rw-r--r-- | src/ch/epfl/xblast/Lists.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java new file mode 100644 index 0000000..51e76b0 --- /dev/null +++ b/src/ch/epfl/xblast/Lists.java | |||
@@ -0,0 +1,47 @@ | |||
1 | package ch.epfl.xblast; | ||
2 | |||
3 | import java.util.ArrayList; | ||
4 | import java.util.Collections; | ||
5 | import java.util.List; | ||
6 | import java.util.stream.Collectors; | ||
7 | import java.util.stream.Stream; | ||
8 | |||
9 | /** | ||
10 | * Lists utility class providing common operations on lists. | ||
11 | * | ||
12 | * @author Pacien TRAN-GIRARD (261948) | ||
13 | * @author Timothée FLOURE (257420) | ||
14 | */ | ||
15 | public final class Lists { | ||
16 | |||
17 | /** | ||
18 | * Returns a reversed copy of the given list, leaving the original one unmodified. | ||
19 | * | ||
20 | * @param l the list to reverse | ||
21 | * @param <T> the type of the list's elements | ||
22 | * @return a reversed copy of the list. | ||
23 | */ | ||
24 | public static <T> List<T> reversed(List<T> l) { | ||
25 | List<T> r = new ArrayList<>(l); | ||
26 | Collections.reverse(r); | ||
27 | return r; | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * Returns a symmetric version of the list, without repeating the last element of the input list. | ||
32 | * For instance, mirrored([kay]) will return [kayak]. | ||
33 | * | ||
34 | * @param l the input list | ||
35 | * @param <T> the type of the list's elements | ||
36 | * @return the mirrored list | ||
37 | * @throws IllegalArgumentException if the given list is empty | ||
38 | */ | ||
39 | public static <T> List<T> mirrored(List<T> l) { | ||
40 | if (l == null || l.size() == 0) throw new IllegalArgumentException(); | ||
41 | |||
42 | return Stream | ||
43 | .concat(l.stream(), Lists.reversed(l).stream().skip(1)) | ||
44 | .collect(Collectors.toList()); | ||
45 | } | ||
46 | |||
47 | } | ||