Create enum property type, trivial code simplification

This commit is contained in:
ljacqu
2016-01-04 20:33:31 +01:00
parent 7d41ccbc9c
commit cb07b3df3d
8 changed files with 66 additions and 25 deletions
@@ -1,5 +1,6 @@
package fr.xephi.authme.settings.custom;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.settings.custom.domain.Comment;
import fr.xephi.authme.settings.custom.domain.Property;
import fr.xephi.authme.settings.custom.domain.SettingsClass;
@@ -12,8 +13,8 @@ public class DatabaseSettings implements SettingsClass {
@Comment({"What type of database do you want to use?",
"Valid values: sqlite, mysql"})
public static final Property<String> BACKEND =
newProperty(STRING, "DataSource.backend", "sqlite");
public static final Property<DataSource.DataSourceType> BACKEND =
newProperty(DataSource.DataSourceType.class, "DataSource.backend", DataSource.DataSourceType.SQLITE);
@Comment("Enable database caching, should improve database performance")
public static final Property<Boolean> USE_CACHING =
@@ -28,8 +28,6 @@ public class NewSetting {
ConverterSettings.class, DatabaseSettings.class, EmailSettings.class, HooksSettings.class,
ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class);
private static final int YAML_INDENTATION = 4;
private File file;
private YamlConfiguration configuration;
@@ -75,7 +73,7 @@ public class NewSetting {
if (newPathParts.size() > 1) {
for (String path : newPathParts.subList(0, newPathParts.size() - 1)) {
writer.append("\n")
.append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION))
.append(indent(indentationLevel))
.append(path)
.append(": ");
++indentationLevel;
@@ -83,12 +81,12 @@ public class NewSetting {
}
for (String comment : entry.getValue()) {
writer.append("\n")
.append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION))
.append(indent(indentationLevel))
.append("# ")
.append(comment);
}
writer.append("\n")
.append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION))
.append(indent(indentationLevel))
.append(CollectionUtils.getRange(newPathParts, newPathParts.size() - 1).get(0))
.append(": ");
@@ -96,7 +94,7 @@ public class NewSetting {
String delim = "";
for (String yamlLine : yamlLines) {
writer.append(delim).append(yamlLine);
delim = "\n" + StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION);
delim = "\n" + indent(indentationLevel);
}
currentPath = propertyPath.subList(0, propertyPath.size() - 1);
@@ -109,6 +107,15 @@ public class NewSetting {
}
}
private static String indent(int level) {
// YAML uses indentation of 4 spaces
StringBuilder sb = new StringBuilder(level * 4);
for (int i = 0; i < level; ++i) {
sb.append(" ");
}
return sb.toString();
}
private static PropertyMap getAllPropertyFields() {
PropertyMap properties = new PropertyMap();
for (Class<?> clazz : CONFIGURATION_CLASSES) {
@@ -31,6 +31,10 @@ public class Property<T> {
return new Property<>(type, path, Arrays.asList(defaultValues));
}
public static <E extends Enum<E>> Property<E> newProperty(Class<E> clazz, String path, E defaultValue) {
return new Property<>(new PropertyType.EnumProperty<>(clazz), path, defaultValue);
}
// -----
// Overloaded convenience methods for specific types
// -----
@@ -143,4 +143,40 @@ public abstract class PropertyType<T> {
}
}
/**
* Enum property.
* @param <E> The enum class
*/
static final class EnumProperty<E extends Enum<E>> extends PropertyType<E> {
private Class<E> clazz;
public EnumProperty(Class<E> clazz) {
this.clazz = clazz;
}
@Override
public E getFromFile(Property<E> property, YamlConfiguration configuration) {
String textValue = configuration.getString(property.getPath());
if (textValue == null) {
return property.getDefaultValue();
}
E mappedValue = mapToEnum(textValue);
return mappedValue != null ? mappedValue : property.getDefaultValue();
}
@Override
protected List<String> asYaml(E value) {
return asList("'" + value + "'");
}
private E mapToEnum(String value) {
for (E entry : clazz.getEnumConstants()) {
if (entry.name().equalsIgnoreCase(value)) {
return entry;
}
}
return null;
}
}
}