Skip to content

Commit 88175fa

Browse files
committed
feat: add slice convinience method to maps
1 parent 6b7e4f9 commit 88175fa

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

common/src/main/java/com/mx/path/core/common/collection/MultiValueMap.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.mx.path.core.common.collection;
22

33
import java.util.Collection;
4+
import java.util.Collections;
45
import java.util.LinkedHashMap;
56
import java.util.LinkedList;
67
import java.util.List;
78
import java.util.Map;
89
import java.util.Set;
10+
import java.util.regex.Pattern;
911

1012
/**
1113
* A multi-value backed collection that acts like a multi-value map. This can be used interchangeably with other {@link MultiValueMappable} classes.
@@ -285,6 +287,32 @@ public int size() {
285287
return getRawMap().size();
286288
}
287289

290+
/**
291+
* Create a new MultiValueMap containing only keys matching the provided {@link Pattern}.
292+
*
293+
* @param pattern {@link Pattern}
294+
* @return new MultiValueMap
295+
*/
296+
public MultiValueMap<K, V> slice(Pattern pattern) {
297+
return slice(Collections.singletonList(pattern));
298+
}
299+
300+
/**
301+
* Create a new MultiValueMap containing only keys matching at least one provided {@link Pattern}.
302+
*
303+
* @param keyPatterns list of {@link Pattern}
304+
* @return new MultiValueMap
305+
*/
306+
public MultiValueMap<K, V> slice(Collection<Pattern> keyPatterns) {
307+
MultiValueMap<K, V> result = new MultiValueMap<>();
308+
309+
rawMap.keySet().stream().filter((key) -> keyPatterns.stream().anyMatch((p) -> p.asPredicate().test(key.toString()))).forEach((key) -> {
310+
result.put(key, get(key));
311+
});
312+
313+
return result;
314+
}
315+
288316
/**
289317
* Convert to a {@link SingleValueMap}
290318
* Implementation Note: It is expected that multi-values are maintained in the

common/src/main/java/com/mx/path/core/common/collection/SingleValueMap.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import java.util.ArrayList;
44
import java.util.Collection;
5+
import java.util.Collections;
56
import java.util.LinkedHashMap;
67
import java.util.List;
78
import java.util.Map;
89
import java.util.Set;
10+
import java.util.regex.Pattern;
911

1012
/**
1113
* A multi-value backed collection that acts like a single-value map. This can be used interchangeably with other {@link MultiValueMappable} classes.
@@ -246,6 +248,32 @@ public int size() {
246248
return getRawMap().size();
247249
}
248250

251+
/**
252+
* Create a new SingleValueMap containing only keys matching the provided {@link Pattern}.
253+
*
254+
* @param pattern {@link Pattern}
255+
* @return new SingleValueMap
256+
*/
257+
public SingleValueMap<K, V> slice(Pattern pattern) {
258+
return slice(Collections.singletonList(pattern));
259+
}
260+
261+
/**
262+
* Create a new SingleValueMap containing only keys matching at least one provided {@link Pattern}.
263+
*
264+
* @param keyPatterns list of {@link Pattern}
265+
* @return new SingleValueMap
266+
*/
267+
public SingleValueMap<K, V> slice(Collection<Pattern> keyPatterns) {
268+
SingleValueMap<K, V> result = new SingleValueMap<>();
269+
270+
rawMap.keySet().stream().filter((key) -> keyPatterns.stream().anyMatch((p) -> p.asPredicate().test(key.toString()))).forEach((key) -> {
271+
result.put(key, get(key));
272+
});
273+
274+
return result;
275+
}
276+
249277
/**
250278
* Convert to regular Map
251279
*

common/src/test/groovy/com/mx/path/core/common/collection/MultiValueMapTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.mx.path.core.common.collection
22

33

4+
import java.util.regex.Pattern
5+
46
import spock.lang.Specification
57

68
class MultiValueMapTest extends Specification {
@@ -131,6 +133,34 @@ class MultiValueMapTest extends Specification {
131133
empty.size() == 0
132134
}
133135

136+
def "slice"() {
137+
given:
138+
subject.add("key1", "value1")
139+
subject.add("x-key1", "value1")
140+
subject.add("key2", "value2")
141+
subject.add("x-key2", "value2")
142+
143+
when:
144+
def result = subject.slice(Pattern.compile("^x-"))
145+
146+
then:
147+
result.size() == 2
148+
result.keySet().contains("x-key1")
149+
result.keySet().contains("x-key2")
150+
151+
when:
152+
result = subject.slice([
153+
Pattern.compile("^x-"),
154+
Pattern.compile("key2")
155+
])
156+
157+
then:
158+
result.size() == 3
159+
result.keySet().contains("x-key1")
160+
result.keySet().contains("key2")
161+
result.keySet().contains("x-key2")
162+
}
163+
134164
def "isEmpty"() {
135165
when:
136166
subject.addAll("numbers", new LinkedList().tap {

common/src/test/groovy/com/mx/path/core/common/collection/SingleValueMapTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.mx.path.core.common.collection
22

33

4+
import java.util.regex.Pattern
5+
46
import spock.lang.Specification
57

68
class SingleValueMapTest extends Specification {
@@ -55,6 +57,34 @@ class SingleValueMapTest extends Specification {
5557
empty.size() == 0
5658
}
5759

60+
def "slice"() {
61+
given:
62+
subject.put("key1", "value1")
63+
subject.put("x-key1", "value1")
64+
subject.put("key2", "value2")
65+
subject.put("x-key2", "value2")
66+
67+
when:
68+
def result = subject.slice(Pattern.compile("^x-"))
69+
70+
then:
71+
result.size() == 2
72+
result.keySet().contains("x-key1")
73+
result.keySet().contains("x-key2")
74+
75+
when:
76+
result = subject.slice([
77+
Pattern.compile("^x-"),
78+
Pattern.compile("key2")
79+
])
80+
81+
then:
82+
result.size() == 3
83+
result.keySet().contains("x-key1")
84+
result.keySet().contains("key2")
85+
result.keySet().contains("x-key2")
86+
}
87+
5888
def "isEmpty"() {
5989
when:
6090
subject.clear()

0 commit comments

Comments
 (0)