|
| 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 | + * © Copyright 2002-2022 David Sporn |
| 16 | + * </p> |
| 17 | + * <hr> |
| 18 | + * |
| 19 | + * <p> |
| 20 | + * This file is part of <i>The Sporniket Core Library – lang</i>. |
| 21 | + * |
| 22 | + * <p> |
| 23 | + * <i>The Sporniket Core Library – 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 – 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 – 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 | +} |
0 commit comments