aboutsummaryrefslogtreecommitdiff
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
parentee00d0bfbfa5ea253559038c27a23da33c528d0e (diff)
downloadxblast-7c19be7f34b932d696d839a092f05617400589f6.tar.gz
Rewrite list mirroring function and add test
-rw-r--r--src/ch/epfl/xblast/Lists.java43
-rw-r--r--test/ch/epfl/xblast/ListsTest.java40
2 files changed, 52 insertions, 31 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}
diff --git a/test/ch/epfl/xblast/ListsTest.java b/test/ch/epfl/xblast/ListsTest.java
index 42b317f..0a2f5f9 100644
--- a/test/ch/epfl/xblast/ListsTest.java
+++ b/test/ch/epfl/xblast/ListsTest.java
@@ -1,26 +1,42 @@
1package ch.epfl.xblast; 1package ch.epfl.xblast;
2 2
3import java.util.List;
4import java.util.Arrays;
5import static org.junit.Assert.*;
6import org.junit.Test; 3import org.junit.Test;
7 4
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.List;
8import java.util.stream.Collectors;
9import java.util.stream.IntStream;
10
11import static org.junit.Assert.assertEquals;
12
8/** 13/**
9* @author Pacien TRAN-GIRARD (261948) 14 * @author Pacien TRAN-GIRARD (261948)
10* @author Timothée FLOURE (257420) 15 * @author Timothée FLOURE (257420)
11*/ 16 */
12public class ListsTest { 17public class ListsTest {
13 @Test(expected=IllegalArgumentException.class) 18
19 @Test(expected = IllegalArgumentException.class)
14 public void isEmptyListThrowingException() { 20 public void isEmptyListThrowingException() {
15 List<Integer> sampleList = Arrays.asList(); 21 List<Integer> emptyList = new ArrayList<>();
16 Lists.<Integer>mirrored(sampleList); 22 Lists.mirrored(emptyList);
17 } 23 }
18 24
19 @Test 25 @Test
20 public void isListMirrored() { 26 public void isListMirrored() {
21 List<Integer> sampleList = Arrays.asList(1,2,3,4,5); 27 List<Integer> sampleList = Arrays.asList(1, 2, 3, 4, 5);
22 List<Integer> expected = Arrays.asList(1,2,3,4,5,4,3,2,1); 28 List<Integer> expected = Arrays.asList(1, 2, 3, 4, 5, 4, 3, 2, 1);
29
30 assertEquals(expected, Lists.mirrored(sampleList));
31 }
23 32
24 assertEquals(expected, Lists.<Integer>mirrored(sampleList)); 33 @Test
34 public void mirrorElementsAreEqual() {
35 List<Integer> sampleList = IntStream.range(0, 10).boxed().collect(Collectors.toList());
36 List<Integer> mirrored = Lists.mirrored(sampleList);
37
38 for (int i = 0; i < mirrored.size() / 2; ++i)
39 assertEquals(mirrored.get(i), mirrored.get(mirrored.size() - 1 - i));
25 } 40 }
41
26} 42}