aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-02-29 17:25:37 +0100
committerPacien TRAN-GIRARD2016-02-29 17:25:37 +0100
commit7c19be7f34b932d696d839a092f05617400589f6 (patch)
tree0b4f89ef40794b75c302fa808cd364176efc0fdf /src
parentee00d0bfbfa5ea253559038c27a23da33c528d0e (diff)
downloadxblast-7c19be7f34b932d696d839a092f05617400589f6.tar.gz
Rewrite list mirroring function and add test
Diffstat (limited to 'src')
-rw-r--r--src/ch/epfl/xblast/Lists.java43
1 files changed, 24 insertions, 19 deletions
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java
index f468788..51e76b0 100644
--- a/src/ch/epfl/xblast/Lists.java
+++ b/src/ch/epfl/xblast/Lists.java
@@ -3,6 +3,8 @@ package ch.epfl.xblast;
3import java.util.ArrayList; 3import java.util.ArrayList;
4import java.util.Collections; 4import java.util.Collections;
5import java.util.List; 5import java.util.List;
6import java.util.stream.Collectors;
7import java.util.stream.Stream;
6 8
7/** 9/**
8 * Lists utility class providing common operations on lists. 10 * Lists utility class providing common operations on lists.
@@ -13,30 +15,33 @@ import java.util.List;
13public final class Lists { 15public final class Lists {
14 16
15 /** 17 /**
16 * Return a symmetric version of the list. 18 * Returns a reversed copy of the given list, leaving the original one unmodified.
17 * 19 *
18 * @param l the input list 20 * @param l the list to reverse
19 * @return mirroredList the mirrored list 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
20 * @throws IllegalArgumentException if the given list is empty 37 * @throws IllegalArgumentException if the given list is empty
21 */ 38 */
22 public static <T> List<T> mirrored(List<T> l) { 39 public static <T> List<T> mirrored(List<T> l) {
23 if (l.size() == 0) { 40 if (l == null || l.size() == 0) throw new IllegalArgumentException();
24 throw new IllegalArgumentException();
25 }
26
27 List<T> mirroredList = new ArrayList<T>();
28 List<T> rightSide = new ArrayList<T>();
29
30 for (int i = 0; i < l.size() - 1; i++) {
31 rightSide.add(l.get(i));
32 }
33
34 Collections.reverse(rightSide);
35
36 mirroredList.addAll(l);
37 mirroredList.addAll(rightSide);
38 41
39 return mirroredList; 42 return Stream
43 .concat(l.stream(), Lists.reversed(l).stream().skip(1))
44 .collect(Collectors.toList());
40 } 45 }
41 46
42} 47}