Skip to content

Commit f9f9b68

Browse files
committed
Tools to create simple maps
1 parent 25600c8 commit f9f9b68

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
*
3+
*/
4+
package com.sporniket.libre.lang ;
5+
6+
import static java.util.Arrays.asList ;
7+
8+
import java.util.Map ;
9+
import java.util.stream.Collectors ;
10+
11+
/**
12+
* Macros for maps.
13+
*
14+
* <p>
15+
* &copy; Copyright 2002-2022 David Sporn
16+
* </p>
17+
* <hr>
18+
*
19+
* <p>
20+
* This file is part of <i>The Sporniket Core Library &#8211; lang</i>.
21+
*
22+
* <p>
23+
* <i>The Sporniket Core Library &#8211; lang</i> is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published
24+
* by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
25+
*
26+
* <p>
27+
* <i>The Sporniket Core Library &#8211; lang</i> is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
29+
*
30+
* <p>
31+
* You should have received a copy of the GNU Lesser General Public License along with <i>The Sporniket Core Library &#8211; lang</i>. If not, see
32+
* <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>. 2
33+
*
34+
* <hr>
35+
*
36+
* @author David SPORN
37+
* @version 22.09.00
38+
* @since 22.09.01
39+
*/
40+
public class MapTools {
41+
42+
/**
43+
* Update the given map using a list of {@link String}s, each string encoding a pair of key-value.
44+
*
45+
* @param target the map to modify.
46+
* @param separator
47+
* @param items each item follows the pattern : <code>key + separator + value</code>
48+
* @return the updated map.
49+
*/
50+
public static final Map<String, String> applyToMap(Map<String, String> target, char separator, String... items) {
51+
String sep = "[" + separator + "]" ;
52+
asList(items).stream().map(i -> i.split(sep, 2)).forEach(i -> target.put(i[0], i[1])) ;
53+
return target ;
54+
}
55+
56+
/**
57+
* Update the given map using a list of {@link String}s, each string encoding a pair of key-value.
58+
*
59+
* @param target the map to modify.
60+
* @param items each item follows the pattern : <code>key + ':' + value</code>
61+
* @return the updated map.
62+
*/
63+
public static final Map<String, String> applyToMap(Map<String, String> target, String... items) {
64+
return applyToMap(target, ':', items) ;
65+
}
66+
67+
/**
68+
* Create a {@link Map} using a list of {@link String}s, each string encoding a pair of key-value.
69+
*
70+
* @param separator the separator char to use.
71+
* @param items each item follows the pattern : <code>key + separator + value</code>
72+
* @return the map.
73+
*/
74+
public static final Map<String, String> asMap(char separator, String... items) {
75+
String sep = "[" + separator + "]" ;
76+
return asList(items).stream().map(a -> a.split(sep, 2))//
77+
.collect(Collectors.toMap(a -> a[0], a -> a[1])) ;
78+
}
79+
80+
/**
81+
* Create a {@link Map} using a list of {@link String}s, each string encoding a pair of key-value.
82+
*
83+
* @param items each item follows the pattern : <code>key + ':' + value</code>
84+
* @return the map.
85+
*/
86+
public static final Map<String, String> asMap(String... items) {
87+
return asMap(':', items) ;
88+
}
89+
90+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package test.sporniket.libre.lang ;
2+
3+
import static com.sporniket.libre.lang.MapTools.applyToMap ;
4+
import static com.sporniket.libre.lang.MapTools.asMap ;
5+
import static org.assertj.core.api.BDDAssertions.then ;
6+
7+
import java.util.Map ;
8+
9+
import org.junit.jupiter.api.Test ;
10+
11+
import com.sporniket.libre.lang.MapTools ;
12+
13+
/**
14+
* Test suite for {@link MapTools}.
15+
*
16+
* <p>
17+
* &copy; Copyright 2002-2022 David Sporn
18+
* </p>
19+
* <hr>
20+
*
21+
* <p>
22+
* This file is part of <i>The Sporniket Core Library &#8211; lang</i>.
23+
*
24+
* <p>
25+
* <i>The Sporniket Core Library &#8211; lang</i> is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published
26+
* by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
27+
*
28+
* <p>
29+
* <i>The Sporniket Core Library &#8211; lang</i> is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
31+
*
32+
* <p>
33+
* You should have received a copy of the GNU Lesser General Public License along with <i>The Sporniket Core Library &#8211; lang</i>. If not, see
34+
* <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>. 2
35+
*
36+
* <hr>
37+
*
38+
* @author David SPORN
39+
*
40+
* @version 22.09.00
41+
* @since 22.09.01
42+
*/
43+
public class TestMapTools {
44+
45+
@Test
46+
public void applyMapWithoutSeparatorShouldModifyMap() {
47+
Map<String, String> target = asMap('|', "a|item a", "b|item b") ;
48+
then(applyToMap(target, "a:apply a", "c:apply c"))//
49+
.hasSize(3)//
50+
.containsEntry("a", "apply a")//
51+
.containsEntry("b", "item b")//
52+
.containsEntry("c", "apply c") ;
53+
}
54+
55+
@Test
56+
public void applyMapWithSeparatorShouldModifyMap() {
57+
Map<String, String> target = asMap("a:item a", "b:item b") ;
58+
then(applyToMap(target, '|', "a|apply a", "c|apply c"))//
59+
.hasSize(3)//
60+
.containsEntry("a", "apply a")//
61+
.containsEntry("b", "item b")//
62+
.containsEntry("c", "apply c") ;
63+
}
64+
65+
@Test
66+
public void asMapWithoutSeparatorShouldCreateMap() {
67+
then(asMap("a:item a", "b:item b"))//
68+
.hasSize(2)//
69+
.containsEntry("a", "item a")//
70+
.containsEntry("b", "item b") ;
71+
}
72+
73+
@Test
74+
public void asMapWithSeparatorShouldCreateMap() {
75+
then(asMap('|', "a|item a", "b|item b"))//
76+
.hasSize(2)//
77+
.containsEntry("a", "item a")//
78+
.containsEntry("b", "item b") ;
79+
}
80+
}

0 commit comments

Comments
 (0)