Minor Java 8 changes

pull/705/head
Brett Wooldridge 9 years ago
parent 6c3957b04b
commit aabc47737f

@ -18,11 +18,9 @@ package com.zaxxer.hikari.util;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -51,22 +49,14 @@ public final class PropertyElf
} }
List<Method> methods = Arrays.asList(target.getClass().getMethods()); List<Method> methods = Arrays.asList(target.getClass().getMethods());
Enumeration<?> propertyNames = properties.propertyNames(); properties.stringPropertyNames().forEach(key -> {
while (propertyNames.hasMoreElements()) { if (target instanceof HikariConfig && key.startsWith("dataSource.")) {
Object key = propertyNames.nextElement(); ((HikariConfig) target).addDataSourceProperty(key.substring("dataSource.".length()), properties.getProperty(key));
String propName = key.toString();
Object propValue = properties.getProperty(propName);
if (propValue == null) {
propValue = properties.get(key);
}
if (target instanceof HikariConfig && propName.startsWith("dataSource.")) {
((HikariConfig) target).addDataSourceProperty(propName.substring("dataSource.".length()), propValue);
} }
else { else {
setProperty(target, propName, propValue, methods); setProperty(target, key, properties.getProperty(key), methods);
}
} }
});
} }
/** /**
@ -121,33 +111,19 @@ public final class PropertyElf
public static Properties copyProperties(final Properties props) public static Properties copyProperties(final Properties props)
{ {
Properties copy = new Properties(); Properties copy = new Properties();
for (Map.Entry<Object, Object> entry : props.entrySet()) { props.forEach((key, value) -> copy.setProperty(key.toString(), value.toString()));
copy.setProperty(entry.getKey().toString(), entry.getValue().toString());
}
return copy; return copy;
} }
private static void setProperty(final Object target, final String propName, final Object propValue, final List<Method> methods) private static void setProperty(final Object target, final String propName, final Object propValue, final List<Method> methods)
{ {
Method writeMethod = null;
// use the english locale to avoid the infamous turkish locale bug // use the english locale to avoid the infamous turkish locale bug
String methodName = "set" + propName.substring(0, 1).toUpperCase(Locale.ENGLISH) + propName.substring(1); String methodName = "set" + propName.substring(0, 1).toUpperCase(Locale.ENGLISH) + propName.substring(1);
Method writeMethod = methods.stream().filter(m -> m.getName().equals(methodName) && m.getParameterCount() == 1).findFirst().orElse(null);
for (Method method : methods) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
writeMethod = method;
break;
}
}
if (writeMethod == null) { if (writeMethod == null) {
methodName = "set" + propName.toUpperCase(Locale.ENGLISH); String methodName2 = "set" + propName.toUpperCase(Locale.ENGLISH);
for (Method method : methods) { writeMethod = methods.stream().filter(m -> m.getName().equals(methodName2) && m.getParameterCount() == 1).findFirst().orElse(null);
if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
writeMethod = method;
break;
}
}
} }
if (writeMethod == null) { if (writeMethod == null) {

Loading…
Cancel
Save