aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ch/epfl/xblast/Lists.java48
-rw-r--r--test/ch/epfl/xblast/ListsTest.java47
2 files changed, 91 insertions, 4 deletions
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java
index cc6a1c8..bf0cafa 100644
--- a/src/ch/epfl/xblast/Lists.java
+++ b/src/ch/epfl/xblast/Lists.java
@@ -2,6 +2,7 @@ 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;
5import java.util.stream.Stream; 6import java.util.stream.Stream;
6 7
7/** 8/**
@@ -181,4 +182,51 @@ public final class Lists {
181 .collect(Collectors.toList())); 182 .collect(Collectors.toList()));
182 } 183 }
183 184
185 /**
186 * Maps linearly two lists.
187 *
188 * @param kl the keys list
189 * @param vl the values list
190 * @param <K> the key type
191 * @param <V> the value type
192 * @return the map
193 */
194 public static <K, V> Map<K, V> linearMap(List<K> kl, List<V> vl) {
195 if (Objects.isNull(kl) || Objects.isNull(vl) || kl.size() != vl.size())
196 throw new IllegalArgumentException();
197
198 return Collections.unmodifiableMap(IntStream
199 .range(0, kl.size()).mapToObj(i -> i)
200 .filter(i -> Objects.nonNull(vl.get(i)))
201 .collect(Collectors.toMap(kl::get, vl::get)));
202 }
203
204 /**
205 * Merges two maps Map<K1, IK> and Map<IK, V> -> Map<K1, V>.
206 *
207 * @param m1 key maps
208 * @param m2 values maps
209 * @param <K> the keys type
210 * @param <IK> the intermediate keys type
211 * @param <V> the values type
212 * @return the traversing map
213 */
214 public static <K, IK, V> Map<K, V> traverseMaps(Map<K, IK> m1, Map<IK, V> m2) {
215 return Collections.unmodifiableMap(m1.entrySet().stream()
216 .collect(Collectors.toMap(Map.Entry::getKey, e -> m2.get(e.getValue()))));
217 }
218
219 /**
220 * Returns an inverted copy of a bijective map.
221 *
222 * @param m the map to invert
223 * @param <K> the keys type
224 * @param <V> the values type
225 * @return the inverted map
226 */
227 public static <K, V> Map<K, V> invertMap(Map<V, K> m) {
228 return Collections.unmodifiableMap(m.entrySet().stream()
229 .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)));
230 }
231
184} 232}
diff --git a/test/ch/epfl/xblast/ListsTest.java b/test/ch/epfl/xblast/ListsTest.java
index b18b05e..e62eede 100644
--- a/test/ch/epfl/xblast/ListsTest.java
+++ b/test/ch/epfl/xblast/ListsTest.java
@@ -2,10 +2,7 @@ package ch.epfl.xblast;
2 2
3import org.junit.Test; 3import org.junit.Test;
4 4
5import java.util.ArrayList; 5import java.util.*;
6import java.util.Arrays;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.stream.Collectors; 6import java.util.stream.Collectors;
10import java.util.stream.IntStream; 7import java.util.stream.IntStream;
11 8
@@ -63,4 +60,46 @@ public class ListsTest {
63 assertEquals(expected, Lists.permutations(sampleList)); 60 assertEquals(expected, Lists.permutations(sampleList));
64 } 61 }
65 62
63 @Test
64 public void areListsMapped() {
65 final List<Character> keyList = Arrays.asList('a', 'b');
66 final List<Integer> valueList = Arrays.asList(0, 1);
67
68 final Map<Character, Integer> expectedMap = new HashMap<>();
69 expectedMap.put('a', 0);
70 expectedMap.put('b', 1);
71
72 assertEquals(expectedMap, Lists.linearMap(keyList, valueList));
73 }
74
75 @Test
76 public void areMapsTraversed() {
77 final Map<Character, Integer> m1 = new HashMap<>();
78 m1.put('a', 0);
79 m1.put('b', 1);
80
81 final Map<Integer, String> m2 = new HashMap<>();
82 m2.put(0, "Apple");
83 m2.put(1, "Banana");
84
85 final Map<Character, String> expectedMap = new HashMap<>();
86 expectedMap.put('a', "Apple");
87 expectedMap.put('b', "Banana");
88
89 assertEquals(expectedMap, Lists.traverseMaps(m1, m2));
90 }
91
92 @Test
93 public void isMapInverted() {
94 final Map<Character, Integer> sampleMap = new HashMap<>();
95 sampleMap.put('a', 0);
96 sampleMap.put('b', 1);
97
98 final Map<Integer, Character> expectedMap = new HashMap<>();
99 expectedMap.put(0, 'a');
100 expectedMap.put(1, 'b');
101
102 assertEquals(expectedMap, Lists.invertMap(sampleMap));
103 }
104
66} 105}