Skip to content

Commit ef57b87

Browse files
chore: merge main into release [skip ci]
2 parents 945ee0d + 8e42d20 commit ef57b87

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

microsphere-java-core/src/main/java/io/microsphere/collection/PropertiesUtils.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
import io.microsphere.annotation.Nonnull;
2121
import io.microsphere.util.Utils;
2222

23+
import java.io.StringReader;
2324
import java.util.LinkedHashMap;
2425
import java.util.Map;
2526
import java.util.Properties;
2627

2728
import static io.microsphere.collection.MapUtils.isEmpty;
2829
import static io.microsphere.collection.MapUtils.newLinkedHashMap;
30+
import static io.microsphere.constants.SeparatorConstants.FILE_SEPARATOR;
31+
import static io.microsphere.constants.SeparatorConstants.LINE_SEPARATOR;
2932
import static io.microsphere.constants.SymbolConstants.DOT;
33+
import static io.microsphere.lang.function.ThrowableAction.execute;
34+
import static io.microsphere.util.ArrayUtils.length;
35+
import static io.microsphere.util.StringUtils.arrayToString;
3036
import static java.util.Collections.unmodifiableMap;
3137

3238
/**
@@ -144,6 +150,45 @@ public static Properties newProperties(Properties defaults) {
144150
return new Properties(defaults);
145151
}
146152

153+
/**
154+
* Loads properties from the given string values.
155+
*
156+
* <p>The provided string values are joined using the system file separator to form a single content string,
157+
* which is then parsed as a standard Java properties format.</p>
158+
*
159+
* <h3>Example Usage</h3>
160+
* <pre>{@code
161+
* // Load properties from multiple lines
162+
* Properties props = PropertiesUtils.loadProperties(
163+
* "key1=value1",
164+
* "key2=value2"
165+
* );
166+
* System.out.println(props.getProperty("key1")); // Output: value1
167+
* System.out.println(props.getProperty("key2")); // Output: value2
168+
*
169+
* // Load empty properties if no arguments are provided
170+
* Properties emptyProps = PropertiesUtils.loadProperties();
171+
* System.out.println(emptyProps.isEmpty()); // Output: true
172+
* }</pre>
173+
*
174+
* @param propertiesValue the string values representing properties content
175+
* @return a new {@link Properties} instance loaded from the given values
176+
*/
177+
@Nonnull
178+
public static Properties loadProperties(String... propertiesValue) {
179+
int length = length(propertiesValue);
180+
Properties properties = newProperties();
181+
if (length > 0) {
182+
String content = arrayToString(propertiesValue, LINE_SEPARATOR);
183+
execute(() -> {
184+
try (StringReader reader = new StringReader(content)) {
185+
properties.load(reader);
186+
}
187+
});
188+
}
189+
return properties;
190+
}
191+
147192
private PropertiesUtils() {
148193
}
149194
}

microsphere-java-core/src/test/java/io/microsphere/collection/PropertiesUtilsTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static io.microsphere.collection.MapUtils.ofMap;
2525
import static io.microsphere.collection.PropertiesUtils.flatProperties;
26+
import static io.microsphere.collection.PropertiesUtils.loadProperties;
2627
import static io.microsphere.collection.PropertiesUtils.newProperties;
2728
import static java.util.Collections.emptyList;
2829
import static java.util.Collections.emptyMap;
@@ -74,11 +75,22 @@ void testNewPropertiesWithDefaults() {
7475
Properties defaults = new Properties();
7576
defaults.setProperty("key1", "value1");
7677
defaults.setProperty("key2", "value2");
77-
78+
7879
Properties props = newProperties(defaults);
7980
assertNotNull(props);
8081
assertEquals("value1", props.getProperty("key1"));
8182
assertEquals("value2", props.getProperty("key2"));
8283
assertTrue(props.isEmpty()); // Properties from defaults are not in the main properties
8384
}
85+
86+
@Test
87+
void testLoadProperties() {
88+
Properties properties = loadProperties();
89+
assertTrue(properties.isEmpty());
90+
91+
properties = loadProperties("a=1", "b : 2", "c 3");
92+
assertEquals("1", properties.getProperty("a"));
93+
assertEquals("2", properties.getProperty("b"));
94+
assertEquals("3", properties.getProperty("c"));
95+
}
8496
}

0 commit comments

Comments
 (0)