Skip to content

Commit a61d04e

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

File tree

13 files changed

+1056
-4
lines changed

13 files changed

+1056
-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: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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 {@link Map}s 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);</code></pre>
68+
*
69+
* Will result in the following properties, assuming <code>indexed</code> is <code>false</code>:
70+
*
71+
* <pre><code>
72+
* application.name=My App
73+
* application.prefixes=&gt;,$</code></pre>
74+
*
75+
* If <code>indexed</code> is <code>true</code>, the result would be:
76+
*
77+
* <pre><code>
78+
* application.name=My App
79+
* application.prefixes[0]=&gt;
80+
* application.prefixes[1]=$</code></pre>
81+
*
82+
*
83+
* @param input Map of properties that may contain nested Maps.
84+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
85+
* @param <V> Type of values the {@link Map} contains.
86+
* @return Map of all properties indexed by their fully qualified names.
87+
*/
88+
public static <V> Map<String, String> flattenMapProperties(final Map<String, V> input, final boolean indexed)
89+
{
90+
final Map<String, String> result = new HashMap<>();
91+
flattenMapProperties(input, result, indexed);
92+
return result;
93+
}
94+
95+
/**
96+
* Calls {@link #flattenMapProperties(Map, Map, boolean, String)} with parameter <code>prefix</code> as <code>null</code>, since when we begin
97+
* flattening the map, there is no prefix by default.
98+
*
99+
* @param input Map of properties that may contain nested Maps.
100+
* @param output Map that all properties are added to.
101+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
102+
* @param <V> Type of values the {@link Map} contains.
103+
* @see #flattenMapProperties(Map, Map, boolean, String)
104+
*/
105+
private static <V> void flattenMapProperties(final Map<String, V> input,
106+
final Map<String, String> output,
107+
final boolean indexed)
108+
{
109+
flattenMapProperties(input, output, indexed, null);
110+
}
111+
112+
/**
113+
* @param input Map of properties that may contain nested Maps.
114+
* @param output Map that all properties are added to.
115+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
116+
* @param prefix Name to prefix to any properties found on this level.
117+
* @param <V> Type of values the {@link Map} contains.
118+
*/
119+
private static <V> void flattenMapProperties(final Map<String, V> input,
120+
final Map<String, String> output,
121+
final boolean indexed,
122+
final String prefix)
123+
{
124+
input.forEach((key, value) ->
125+
{
126+
if (value == null)
127+
{
128+
return;
129+
}
130+
131+
final String k = (prefix == null) ? key : (prefix + '.' + key);
132+
133+
if (value instanceof Map)
134+
{
135+
flattenMapProperties((Map) value, output, indexed, k);
136+
}
137+
else if (value instanceof Iterable)
138+
{
139+
addIterable((Iterable) value, k, output, indexed);
140+
}
141+
else
142+
{
143+
output.put(k, (output.containsKey(k)) ? output.get(k) + "," + value : value.toString());
144+
}
145+
});
146+
}
147+
148+
/**
149+
* @param value Array of values that needs to be flattened.
150+
* @param key Property name for this value.
151+
* @param output Map that all properties are added to.
152+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
153+
* @param <V> Type of values the {@link Map} contains.
154+
*/
155+
private static <V> void addIterable(final Iterable<V> value,
156+
final String key,
157+
final Map<String, String> output,
158+
final boolean indexed)
159+
{
160+
final StringJoiner joiner = new StringJoiner(",");
161+
int index = 0;
162+
163+
for (final Object o : value)
164+
{
165+
if (o instanceof Map)
166+
{
167+
final Map map = (Map) o;
168+
169+
if (map.isEmpty())
170+
{
171+
continue;
172+
}
173+
174+
final String keyPrefix = (indexed) ? key + "[" + index++ + "]" : key;
175+
flattenMapProperties((Map) map, output, indexed, keyPrefix);
176+
}
177+
else
178+
{
179+
joiner.add(o.toString());
180+
}
181+
}
182+
183+
if (joiner.length() > 0)
184+
{
185+
output.put(key, joiner.toString());
186+
}
187+
}
188+
}

0 commit comments

Comments
 (0)