aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-09 11:52:22 +0200
committerPacien TRAN-GIRARD2016-05-09 11:52:22 +0200
commit913812c2d7a560979aaf82e0ff7ceba0390baf96 (patch)
tree7df36be51d4a6b2ff39556220c3efd9cc5c41ed2
parentcd1909c6a4117a65feb0a2f6e62bfcd9a4aa837b (diff)
downloadxblast-913812c2d7a560979aaf82e0ff7ceba0390baf96.tar.gz
Refactor Lists utilities
-rw-r--r--src/ch/epfl/xblast/Lists.java102
1 files changed, 51 insertions, 51 deletions
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java
index 073e0cc..3794c1e 100644
--- a/src/ch/epfl/xblast/Lists.java
+++ b/src/ch/epfl/xblast/Lists.java
@@ -36,33 +36,27 @@ public final class Lists {
36 } 36 }
37 37
38 /** 38 /**
39 * Returns a symmetric version of the list, without repeating the last element of the input list. 39 * Returns a copy of the given list with element e prepended.
40 * For instance, mirrored([kay]) will return [kayak].
41 * 40 *
42 * @param l the input list 41 * @param l the list
42 * @param e the element to prepend
43 * @param <T> the type of the list's elements 43 * @param <T> the type of the list's elements
44 * @return the mirrored list 44 * @return a copy of the list with the element prepended
45 * @throws IllegalArgumentException if the given list is empty
46 */ 45 */
47 public static <T> List<T> mirrored(List<T> l) { 46 public static <T> List<T> prepended(List<T> l, T e) {
48 if (l == null || l.size() == 0) throw new IllegalArgumentException(); 47 return Lists.inserted(l, 0, e);
49
50 return Stream
51 .concat(l.stream(), Lists.reversed(l).stream().skip(1))
52 .collect(Collectors.toList());
53 } 48 }
54 49
55 /** 50 /**
56 * Returns a reversed copy of the given list, leaving the original one unmodified. 51 * Returns a copy of the given list with element e appended.
57 * 52 *
58 * @param l the list to reverse 53 * @param l the list
54 * @param e the element to append
59 * @param <T> the type of the list's elements 55 * @param <T> the type of the list's elements
60 * @return a reversed copy of the list 56 * @return a copy of the list with the element appended
61 */ 57 */
62 private static <T> List<T> reversed(List<T> l) { 58 public static <T> List<T> appended(List<T> l, T e) {
63 List<T> r = new ArrayList<>(l); 59 return Lists.inserted(l, l.size(), e);
64 Collections.reverse(r);
65 return r;
66 } 60 }
67 61
68 /** 62 /**
@@ -74,46 +68,24 @@ public final class Lists {
74 * @return a copy of the list with the element inserted at start and end 68 * @return a copy of the list with the element inserted at start and end
75 */ 69 */
76 public static <T> List<T> surrounded(List<T> l, T e) { 70 public static <T> List<T> surrounded(List<T> l, T e) {
77 return Lists.inserted(Lists.inserted(l, 0, e), l.size() + 1, e); 71 return Lists.appended(Lists.prepended(l, e), e);
78 }
79
80 /**
81 * Returns a copy of the given list with element e inserted at index i.
82 *
83 * @param l the list
84 * @param i the insertion index
85 * @param e the element to insert
86 * @param <T> the type of the list's elements
87 * @return a copy of the list with the element inserted
88 */
89 public static <T> List<T> inserted(List<T> l, int i, T e) {
90 List<T> r = new LinkedList<>(l);
91 r.add(i, e);
92 return r;
93 } 72 }
94 73
95 /** 74 /**
96 * Returns a copy of the given list with element e prepended. 75 * Returns a symmetric version of the list, without repeating the last element of the input list.
76 * For instance, mirrored([kay]) will return [kayak].
97 * 77 *
98 * @param l the list 78 * @param l the input list
99 * @param e the element to prepend
100 * @param <T> the type of the list's elements 79 * @param <T> the type of the list's elements
101 * @return a copy of the list with the element prepended 80 * @return the mirrored list
81 * @throws IllegalArgumentException if the given list is empty
102 */ 82 */
103 public static <T> List<T> prepended(List<T> l, T e) { 83 public static <T> List<T> mirrored(List<T> l) {
104 return Lists.inserted(l, 0, e); 84 if (l == null || l.size() == 0) throw new IllegalArgumentException();
105 }
106 85
107 /** 86 return Stream
108 * Returns a copy of the given list with element e appended. 87 .concat(l.stream(), Lists.reversed(l).stream().skip(1))
109 * 88 .collect(Collectors.toList());
110 * @param l the list
111 * @param e the element to append
112 * @param <T> the type of the list's elements
113 * @return a copy of the list with the element appended
114 */
115 public static <T> List<T> appended(List<T> l, T e) {
116 return Lists.inserted(l, l.size(), e);
117 } 89 }
118 90
119 /** 91 /**
@@ -167,4 +139,32 @@ public final class Lists {
167 .collect(Collectors.toList())); 139 .collect(Collectors.toList()));
168 } 140 }
169 141
142 /**
143 * Returns a reversed copy of the given list, leaving the original one unmodified.
144 *
145 * @param l the list to reverse
146 * @param <T> the type of the list's elements
147 * @return a reversed copy of the list
148 */
149 private static <T> List<T> reversed(List<T> l) {
150 List<T> r = new ArrayList<>(l);
151 Collections.reverse(r);
152 return r;
153 }
154
155 /**
156 * Returns a copy of the given list with element e inserted at index i.
157 *
158 * @param l the list
159 * @param i the insertion index
160 * @param e the element to insert
161 * @param <T> the type of the list's elements
162 * @return a copy of the list with the element inserted
163 */
164 private static <T> List<T> inserted(List<T> l, int i, T e) {
165 List<T> r = new LinkedList<>(l);
166 r.add(i, e);
167 return r;
168 }
169
170} 170}