|
17 | 17 | package io.spring.initializr.generator.spring.properties; |
18 | 18 |
|
19 | 19 | import java.io.PrintWriter; |
20 | | -import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
21 | 21 | import java.util.HashMap; |
22 | | -import java.util.List; |
23 | 22 | import java.util.Map; |
24 | | -import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import org.springframework.util.Assert; |
| 25 | +import org.springframework.util.StringUtils; |
25 | 26 |
|
26 | 27 | /** |
27 | 28 | * Application properties. |
@@ -70,11 +71,20 @@ public void add(String key, String value) { |
70 | 71 | add(key, (Object) value); |
71 | 72 | } |
72 | 73 |
|
| 74 | + /** |
| 75 | + * Adds a new property. |
| 76 | + * @param key the key of the property |
| 77 | + * @param value the value of the property |
| 78 | + */ |
| 79 | + public void add(String key, Collection<?> value) { |
| 80 | + add(key, (Object) value); |
| 81 | + } |
| 82 | + |
73 | 83 | void writeProperties(PrintWriter writer) { |
74 | 84 | for (Map.Entry<String, Object> entry : this.properties.entrySet()) { |
75 | | - writer.printf("%s=%s%n", entry.getKey(), (entry.getValue() instanceof List) |
76 | | - ? ((List<?>) entry.getValue()).stream().map(Object::toString).collect(Collectors.joining(", ")) |
77 | | - : entry.getValue()); |
| 85 | + Object value = (entry.getValue() instanceof Collection<?> collection) |
| 86 | + ? StringUtils.collectionToCommaDelimitedString(collection) : entry.getValue(); |
| 87 | + writer.printf("%s=%s%n", entry.getKey(), value); |
78 | 88 | } |
79 | 89 | } |
80 | 90 |
|
@@ -120,36 +130,29 @@ private static void writeEntry(Map.Entry<String, Object> entry, PrintWriter writ |
120 | 130 | writeYamlRecursive((Map<String, Object>) nestedMap, writer, indent + 1); |
121 | 131 | } |
122 | 132 | else { |
123 | | - if (value instanceof List) { |
124 | | - writer.printf("%s%s:%n", indentStr, entry.getKey()); |
125 | | - writeList((List<?>) value, writer, indent + 1); |
| 133 | + if (value instanceof Collection<?> collection) { |
| 134 | + if (collection.isEmpty()) { |
| 135 | + writer.printf("%s%s: []%n", indentStr, entry.getKey()); |
| 136 | + } |
| 137 | + else { |
| 138 | + writer.printf("%s%s:%n", indentStr, entry.getKey()); |
| 139 | + writeCollection(collection, writer, indent + 1); |
| 140 | + } |
126 | 141 | } |
127 | 142 | else { |
128 | 143 | writer.printf("%s%s: %s%n", indentStr, entry.getKey(), value); |
129 | 144 | } |
130 | 145 | } |
131 | 146 | } |
132 | 147 |
|
133 | | - private static void writeList(List<?> list, PrintWriter writer, int indent) { |
| 148 | + private static void writeCollection(Collection<?> collection, PrintWriter writer, int indent) { |
134 | 149 | String indentStr = YAML_SPACE.repeat(indent); |
135 | | - list.forEach((element) -> writer.printf("%s- %s%n", indentStr, element)); |
| 150 | + collection.forEach((element) -> writer.printf("%s- %s%n", indentStr, element)); |
136 | 151 | } |
137 | 152 |
|
138 | 153 | private void add(String key, Object value) { |
139 | | - this.properties.merge(key, value, (oldValue, newValue) -> { |
140 | | - var newValues = new ArrayList<>((oldValue instanceof List) ? ((List<?>) oldValue).size() + 1 : 2); |
141 | | - if (oldValue instanceof List<?>) { |
142 | | - newValues.addAll((List<?>) oldValue); |
143 | | - } |
144 | | - else { |
145 | | - newValues.add(oldValue); |
146 | | - } |
147 | | - if (!newValues.contains(newValue)) { |
148 | | - newValues.add(newValue); |
149 | | - return newValues; |
150 | | - } |
151 | | - return oldValue; |
152 | | - }); |
| 154 | + Assert.state(!this.properties.containsKey(key), () -> "Property '%s' already exists".formatted(key)); |
| 155 | + this.properties.put(key, value); |
153 | 156 | } |
154 | 157 |
|
155 | 158 | } |
0 commit comments