#347 Create enum property + consistency tests
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package fr.xephi.authme.settings.domain;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
* Enum property type.
|
||||
* @param <E> The enum class
|
||||
*/
|
||||
class EnumPropertyType<E extends Enum<E>> extends PropertyType<E> {
|
||||
|
||||
private Class<E> clazz;
|
||||
|
||||
public EnumPropertyType(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;
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class Property<T> {
|
||||
}
|
||||
|
||||
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);
|
||||
return new Property<>(new EnumPropertyType<>(clazz), path, defaultValue);
|
||||
}
|
||||
|
||||
// -----
|
||||
|
||||
@@ -143,40 +143,4 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user