|
| 1 | +package me.hsgamer.bettergui.util; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.concurrent.ConcurrentMap; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.Stream; |
| 7 | + |
| 8 | +public class MapTemplate { |
| 9 | + public static final String START_VARIABLE = "{"; |
| 10 | + public static final String END_VARIABLE = "}"; |
| 11 | + |
| 12 | + private static Object getVariableValue(String string, Map<String, Object> variableMap) { |
| 13 | + int startVariableIndex = string.indexOf(START_VARIABLE); |
| 14 | + int endVariableIndex = string.lastIndexOf(END_VARIABLE); |
| 15 | + if (startVariableIndex != 0 || endVariableIndex != string.length() - END_VARIABLE.length()) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + String variable = string.substring(startVariableIndex + START_VARIABLE.length(), endVariableIndex); |
| 19 | + Object variableValue = variableMap.get(variable); |
| 20 | + if (variableValue == null) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + return apply(variableValue, variableMap); |
| 24 | + } |
| 25 | + |
| 26 | + public static Collection<?> apply(Collection<?> collection, Map<String, Object> variableMap) { |
| 27 | + Stream<Object> stream = collection.stream() |
| 28 | + .flatMap(obj -> { |
| 29 | + if (!(obj instanceof String)) { |
| 30 | + return Stream.of(obj); |
| 31 | + } |
| 32 | + String string = (String) obj; |
| 33 | + Object variableValue = getVariableValue(string, variableMap); |
| 34 | + if (variableValue == null) { |
| 35 | + return Stream.of(obj); |
| 36 | + } |
| 37 | + if (variableValue instanceof Collection) { |
| 38 | + return ((Collection<?>) variableValue).stream(); |
| 39 | + } |
| 40 | + return Stream.of(variableValue); |
| 41 | + }); |
| 42 | + if (collection instanceof List) { |
| 43 | + return stream.collect(Collectors.toList()); |
| 44 | + } else if (collection instanceof Set) { |
| 45 | + return stream.collect(Collectors.toSet()); |
| 46 | + } else { |
| 47 | + return stream.collect(Collectors.toCollection(ArrayList::new)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static Map<?, ?> apply(Map<?, ?> map, Map<String, Object> variableMap) { |
| 52 | + Stream<Map.Entry<?, ?>> stream = map.entrySet().stream() |
| 53 | + .flatMap(entry -> { |
| 54 | + Object key = entry.getKey(); |
| 55 | + if (!(key instanceof String)) { |
| 56 | + return Stream.of(new AbstractMap.SimpleEntry<>(key, apply(entry.getValue(), variableMap))); |
| 57 | + } |
| 58 | + String string = (String) key; |
| 59 | + Object variableValue = getVariableValue(string, variableMap); |
| 60 | + if (variableValue == null) { |
| 61 | + return Stream.of(new AbstractMap.SimpleEntry<>(key, apply(entry.getValue(), variableMap))); |
| 62 | + } |
| 63 | + if (variableValue instanceof Map) { |
| 64 | + return ((Map<?, ?>) variableValue).entrySet().stream(); |
| 65 | + } |
| 66 | + return Stream.empty(); |
| 67 | + }); |
| 68 | + if (map instanceof ConcurrentMap) { |
| 69 | + return stream.collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 70 | + } else if (map instanceof LinkedHashMap) { |
| 71 | + return stream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new)); |
| 72 | + } else { |
| 73 | + return stream.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static Object apply(Object obj, Map<String, Object> variableMap) { |
| 78 | + if (variableMap == null || variableMap.isEmpty()) { |
| 79 | + return obj; |
| 80 | + } |
| 81 | + if (obj instanceof Collection) { |
| 82 | + return apply((Collection<?>) obj, variableMap); |
| 83 | + } |
| 84 | + if (obj instanceof Map) { |
| 85 | + return apply((Map<?, ?>) obj, variableMap); |
| 86 | + } |
| 87 | + if (obj instanceof String) { |
| 88 | + String result = (String) obj; |
| 89 | + StringBuilder sb = new StringBuilder(result); |
| 90 | + for (Map.Entry<String, Object> entry : variableMap.entrySet()) { |
| 91 | + String placeholder = START_VARIABLE + entry.getKey() + END_VARIABLE; |
| 92 | + String replacement = Objects.toString(entry.getValue()); |
| 93 | + int index; |
| 94 | + while ((index = sb.indexOf(placeholder)) != -1) { |
| 95 | + sb.replace(index, index + placeholder.length(), replacement); |
| 96 | + } |
| 97 | + } |
| 98 | + return sb.toString(); |
| 99 | + } |
| 100 | + return obj; |
| 101 | + } |
| 102 | +} |
0 commit comments