Skip to content

Commit

Permalink
Fixed retrieval of properties in the presence of defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoutanov committed Jun 28, 2017
1 parent 3fa8736 commit f684328
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public static <T> T get(String key, Function<String, T> parser, T defaultValue)
return get(System.getProperties(), key, parser, defaultValue);
}

public static <T> T get(Hashtable<?, ?> props, String key, Function<String, T> parser, T defaultValue) {
final String str = (String) props.get(key);
public static <T> T get(Properties props, String key, Function<String, T> parser, T defaultValue) {
final String str = props.getProperty(key);
return str != null ? parser.apply(str) : defaultValue;
}

public static <T> T getOrSet(Hashtable<Object, Object> props, String key, Function<String, T> parser, T defaultValue) {
final String str = (String) props.get(key);
public static <T> T getOrSet(Properties props, String key, Function<String, T> parser, T defaultValue) {
final String str = props.getProperty(key);
if (str == null) {
props.put(key, defaultValue);
return defaultValue;
Expand All @@ -38,19 +38,22 @@ public static Properties load(String resourceFile) throws IOException {
return props;
}

public static Properties load(String resourceFile, Properties defaultHashtable) {
public static Properties load(String resourceFile, Properties defaultProps) {
try {
return load(resourceFile);
} catch (IOException e) {
return defaultHashtable;
return defaultProps;
}
}

public static Properties filter(String keyPrefix, Hashtable<Object, Object> props) {
public static Properties filter(String keyPrefix, Properties props) {
final Properties filtered = new Properties();
for (Map.Entry<Object, Object> entry : props.entrySet()) {
if (((String) entry.getKey()).startsWith(keyPrefix)) {
filtered.put(entry.getKey(), entry.getValue());
final Enumeration<?> keys = props.propertyNames();
while (keys.hasMoreElements()) {
final String key = (String) keys.nextElement();
final String value = props.getProperty(key);
if (key.startsWith(keyPrefix)) {
filtered.put(key, value);
}
}
return filtered;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ public void testFilter() {
assertFalse(filtered.containsKey("b.foo"));
}

@Test
public void testFilterWithDefaults() {
final Properties props = new Properties();
props.put("a.foo", "foo");
props.put("a.bar", "bar");
props.put("b.foo", "bar");
final Properties filtered = PropertyUtils.filter("a.", new Properties(props));
assertEquals(2, filtered.size());
assertTrue(filtered.containsKey("a.foo"));
assertTrue(filtered.containsKey("a.bar"));
assertFalse(filtered.containsKey("b.foo"));
}

@Test
public void assertPrivateConstructor() throws NoSuchMethodException, SecurityException, InvocationTargetException, InstantiationException, IllegalAccessException {
assertUtilityClassWellDefined(PropertyUtils.class);
Expand Down

0 comments on commit f684328

Please sign in to comment.