Skip to content

Commit 7f801b8

Browse files
committed
feat: added yaml configuration support
1 parent 355af59 commit 7f801b8

File tree

13 files changed

+1059
-4
lines changed

13 files changed

+1059
-4
lines changed

deltaspike/checkstyle-rules/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,3 @@
5353
<name>Apache DeltaSpike CheckStyle-rules</name>
5454

5555
</project>
56-
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.deltaspike.core.util;
20+
21+
import jakarta.enterprise.inject.Typed;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import java.util.StringJoiner;
25+
26+
/**
27+
* Utility to flatten nested Maps into a single level key:value pair set of properties.
28+
*
29+
* @since 2.0.1
30+
*/
31+
@Typed
32+
public abstract class MapUtils
33+
{
34+
/**
35+
* Don't construct this class. Only use the <code>static</code> methods.
36+
*/
37+
private MapUtils()
38+
{
39+
// Do nothing
40+
}
41+
42+
/**
43+
* Calls {@link #flattenMapProperties(Map, boolean)} with <code>indexed=false</code>.
44+
*
45+
* @param input Map of properties that may contain nested Maps.
46+
* @param <V> Type of values the {@link Map} contains.
47+
* @return Map of all properties indexed by their fully qualified names.
48+
* @see #flattenMapProperties(Map, boolean)
49+
*/
50+
public static <V> Map<String, String> flattenMapProperties(final Map<String, V> input)
51+
{
52+
return flattenMapProperties(input, false);
53+
}
54+
55+
/**
56+
* Converts a {@link Map} of objects to a flattened {@link Map} of {@link String} values.
57+
*
58+
* <p>For example, with the given input:</p>
59+
*
60+
* <pre><code>
61+
* Map&lt;String, Object&gt; application = Map.of(
62+
* "name", "My App",
63+
* "prefixes", List.of("&gt;", "$")
64+
* );
65+
*
66+
* Map&lt;String, Object&gt; map = Map.of("application", application);
67+
* Map&lt;String, String&gt; result = MapUtils.flattenMapProperties(map);
68+
* </code></pre>
69+
*
70+
* Will result in the following properties, assuming <code>indexed</code> is <code>false</code>:
71+
*
72+
* <pre><code>
73+
* application.name=My App
74+
* application.prefixes=&gt;,$
75+
* </code></pre>
76+
*
77+
* If <code>indexed</code> is <code>true</code>, the result would be:
78+
*
79+
* <pre><code>
80+
* application.name=My App
81+
* application.prefixes[0]=&gt;
82+
* application.prefixes[1]=$
83+
* </code></pre>
84+
*
85+
*
86+
* @param input Map of properties that may contain nested Maps.
87+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
88+
* @param <V> Type of values the {@link Map} contains.
89+
* @return Map of all properties indexed by their fully qualified names.
90+
*/
91+
public static <V> Map<String, String> flattenMapProperties(final Map<String, V> input, final boolean indexed)
92+
{
93+
final Map<String, String> result = new HashMap<>();
94+
flattenMapProperties(input, result, indexed);
95+
return result;
96+
}
97+
98+
/**
99+
* Calls {@link #flattenMapProperties(Map, Map, boolean, String)} with parameter <code>prefix</code> as <code>null</code>, since when we begin
100+
* flattening the map, there is no prefix by default.
101+
*
102+
* @param input Map of properties that may contain nested Maps.
103+
* @param output Map that all properties are added to.
104+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
105+
* @param <V> Type of values the {@link Map} contains.
106+
* @see #flattenMapProperties(Map, Map, boolean, String)
107+
*/
108+
private static <V> void flattenMapProperties(final Map<String, V> input,
109+
final Map<String, String> output,
110+
final boolean indexed)
111+
{
112+
flattenMapProperties(input, output, indexed, null);
113+
}
114+
115+
/**
116+
* @param input Map of properties that may contain nested Maps.
117+
* @param output Map that all properties are added to.
118+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
119+
* @param prefix Name to prefix to any properties found on this level.
120+
* @param <V> Type of values the {@link Map} contains.
121+
*/
122+
private static <V> void flattenMapProperties(final Map<String, V> input,
123+
final Map<String, String> output,
124+
final boolean indexed,
125+
final String prefix)
126+
{
127+
input.forEach((key, value) ->
128+
{
129+
if (value == null)
130+
{
131+
return;
132+
}
133+
134+
final String k = (prefix == null) ? key : (prefix + '.' + key);
135+
136+
if (value instanceof Map)
137+
{
138+
flattenMapProperties((Map) value, output, indexed, k);
139+
}
140+
else if (value instanceof Iterable)
141+
{
142+
addIterable((Iterable) value, k, output, indexed);
143+
}
144+
else
145+
{
146+
output.put(k, (output.containsKey(k)) ? output.get(k) + "," + value : value.toString());
147+
}
148+
});
149+
}
150+
151+
/**
152+
* @param value Array of values that needs to be flattened.
153+
* @param key Property name for this value.
154+
* @param output Map that all properties are added to.
155+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
156+
* @param <V> Type of values the {@link Map} contains.
157+
*/
158+
private static <V> void addIterable(final Iterable<V> value,
159+
final String key,
160+
final Map<String, String> output,
161+
final boolean indexed)
162+
{
163+
final StringJoiner joiner = new StringJoiner(",");
164+
int index = 0;
165+
166+
for (final Object o : value)
167+
{
168+
if (o instanceof Map)
169+
{
170+
final Map map = (Map) o;
171+
172+
if (map.isEmpty())
173+
{
174+
continue;
175+
}
176+
177+
final String keyPrefix = (indexed) ? key + "[" + index++ + "]" : key;
178+
flattenMapProperties((Map) map, output, indexed, keyPrefix);
179+
}
180+
else
181+
{
182+
joiner.add(o.toString());
183+
}
184+
}
185+
186+
if (joiner.length() > 0)
187+
{
188+
output.put(key, joiner.toString());
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)