io.github.kits.configuration.ValueConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimthen-kits Show documentation
Show all versions of whimthen-kits Show documentation
Easy to use java tool library.
The newest version!
package io.github.kits.configuration;
import io.github.kits.PropertiesKit;
import java.util.Objects;
import java.util.function.Function;
/**
* @project: kits
* @created: with IDEA
* @author: nzlong
* @Date: 2019 01 23 13:47 | January. Wednesday
*/
public abstract class ValueConfiguration {
public abstract Object getProp(String[] fileNames, String key, String defaultValue);
}
class IntValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getInt(fileName, key), defaultValue);
}
}
class StringValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getString(fileName, key), defaultValue);
}
}
class BooleanValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getBoolean(fileName, key), defaultValue);
}
}
class LongValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getLong(fileName, key), defaultValue);
}
}
class ShortValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getShort(fileName, key), defaultValue);
}
}
class FloatValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getFloat(fileName, key), defaultValue);
}
}
class DoubleValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getDouble(fileName, key), defaultValue);
}
}
class PropValueConfiguration extends ValueConfiguration {
@Override
public Object getProp(String[] fileNames, String key, String defaultValue) {
return ConfigurationKit.getValue(fileNames, fileName -> PropertiesKit.getString(fileName, key), defaultValue);
}
}
class ConfigurationKit {
static Object getValue(String[] fileNames, Function function, Object defaultValue) {
Object value = null;
for (String fileName : fileNames) {
Object propVal = function.apply(fileName);
if (Objects.nonNull(propVal)) {
if (propVal instanceof Integer || propVal instanceof Short) {
if (!propVal.equals(0)) {
value = propVal;
break;
}
} else if (propVal instanceof Float) {
if (!propVal.equals(0F)) {
value = propVal;
break;
}
} else if (propVal instanceof Double) {
if (!propVal.equals(0D)) {
value = propVal;
break;
}
} else if (propVal instanceof Long) {
if (!propVal.equals(0L)) {
value = propVal;
break;
}
} else {
value = propVal;
break;
}
}
}
if (Objects.isNull(value) && Objects.nonNull(defaultValue)) {
value = defaultValue;
}
return value;
}
}