aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/Lists.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/xblast/Lists.java')
-rw-r--r--src/ch/epfl/xblast/Lists.java39
1 files changed, 34 insertions, 5 deletions
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java
index 777fab2..9bd527b 100644
--- a/src/ch/epfl/xblast/Lists.java
+++ b/src/ch/epfl/xblast/Lists.java
@@ -2,7 +2,6 @@ package ch.epfl.xblast;
2 2
3import java.util.*; 3import java.util.*;
4import java.util.stream.Collectors; 4import java.util.stream.Collectors;
5import java.util.stream.IntStream;
6import java.util.stream.Stream; 5import java.util.stream.Stream;
7 6
8/** 7/**
@@ -207,10 +206,36 @@ public final class Lists {
207 if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size()) 206 if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size())
208 throw new IllegalArgumentException(); 207 throw new IllegalArgumentException();
209 208
210 return Collections.unmodifiableMap(IntStream 209 Map<K, V> m = new HashMap<>();
211 .range(0, kl.size()).mapToObj(i -> i) 210 Iterator<K> ki = kl.iterator();
212 .filter(i -> Objects.nonNull(vl.get(i))) 211 Iterator<V> vi = vl.iterator();
213 .collect(Collectors.toMap(kl::get, vl::get))); 212
213 while (ki.hasNext() && vi.hasNext())
214 m.put(ki.next(), vi.next());
215
216 return Collections.unmodifiableMap(m);
217 }
218
219 /**
220 * Maps linearly two lists, adjusting their respective size by truncating the key list or completing the value list
221 * with null values.
222 *
223 * @param kl the keys list
224 * @param vl the values list
225 * @param <K> the key type
226 * @param <V> the value type
227 * @return the map
228 */
229 public static <K, V> Map<K, V> linearAdjustedMap(List<K> kl, List<V> vl) {
230 if (Objects.isNull(kl) || Objects.isNull(vl))
231 throw new IllegalArgumentException();
232
233 if (kl.size() <= vl.size())
234 return Lists.linearMap(kl, vl.subList(0, kl.size()));
235 else
236 return Lists.linearMap(
237 kl,
238 concatenated(Arrays.asList(vl, Collections.nCopies(kl.size() - vl.size(), null))));
214 } 239 }
215 240
216 /** 241 /**
@@ -225,6 +250,8 @@ public final class Lists {
225 */ 250 */
226 public static <K, IK, V> Map<K, V> traverseMaps(Map<K, IK> m1, Map<IK, V> m2) { 251 public static <K, IK, V> Map<K, V> traverseMaps(Map<K, IK> m1, Map<IK, V> m2) {
227 return Collections.unmodifiableMap(m1.entrySet().stream() 252 return Collections.unmodifiableMap(m1.entrySet().stream()
253 .filter(e -> Objects.nonNull(e.getValue()))
254 .filter(e -> Objects.nonNull(m2.get(e.getValue())))
228 .collect(Collectors.toMap(Map.Entry::getKey, e -> m2.get(e.getValue())))); 255 .collect(Collectors.toMap(Map.Entry::getKey, e -> m2.get(e.getValue()))));
229 } 256 }
230 257
@@ -238,6 +265,8 @@ public final class Lists {
238 */ 265 */
239 public static <K, V> Map<K, V> invertMap(Map<V, K> m) { 266 public static <K, V> Map<K, V> invertMap(Map<V, K> m) {
240 return Collections.unmodifiableMap(m.entrySet().stream() 267 return Collections.unmodifiableMap(m.entrySet().stream()
268 .filter(e -> Objects.nonNull(e.getKey()))
269 .filter(e -> Objects.nonNull(e.getValue()))
241 .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); 270 .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)));
242 } 271 }
243 272