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

View File

@ -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 =

View File

@ -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) {

View File

@ -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
// -----

View File

@ -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;
}
}
}

View File

@ -60,15 +60,13 @@ public final class CollectionUtils {
return coll == null || coll.isEmpty();
}
public static <T> List<T> filterCommonStart(List<T> coll1, List<T> coll2) {
public static <T> List<T> filterCommonStart(List<T> list1, List<T> list2) {
List<T> commonStart = new ArrayList<>();
int minSize = Math.min(coll1.size(), coll2.size());
for (int i = 0; i < minSize; ++i) {
if (Objects.equals(coll1.get(i), coll2.get(i))) {
commonStart.add(coll1.get(i));
} else {
break;
}
int minSize = Math.min(list1.size(), list2.size());
int i = 0;
while (i < minSize && Objects.equals(list1.get(i), list2.get(i))) {
commonStart.add(list1.get(i));
++i;
}
return commonStart;
}

View File

@ -117,12 +117,4 @@ public final class StringUtils {
return "[" + th.getClass().getSimpleName() + "]: " + th.getMessage();
}
public static String repeat(String str, int times) {
StringBuilder sb = new StringBuilder(str.length() * times);
for (int i = 0; i < times; ++i) {
sb.append(str);
}
return sb.toString();
}
}

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.settings.custom;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.settings.custom.domain.Property;
import fr.xephi.authme.settings.custom.domain.PropertyType;
import org.bukkit.configuration.file.YamlConfiguration;
@ -83,12 +84,14 @@ public class NewSettingTest {
String helpHeader = settings.getOption(newProperty("settings.helpHeader", ""));
List<String> unsafePasswords = settings.getOption(
newProperty(PropertyType.STRING_LIST, "Security.unsafePasswords"));
DataSource.DataSourceType dataSourceType = settings.getOption(DatabaseSettings.BACKEND);
// then
assertThat(result, equalTo(22));
assertThat(systemName, equalTo(TestConfiguration.SYSTEM_NAME.getDefaultValue()));
assertThat(helpHeader, equalTo("AuthMeReloaded"));
assertThat(unsafePasswords, contains("123456", "qwerty", "54321"));
assertThat(dataSourceType, equalTo(DataSource.DataSourceType.MYSQL));
}
private File getConfigFile() {

View File

@ -3,7 +3,7 @@ test:
DataSource:
# What type of database do you want to use?
# Valid values: sqlite, mysql
backend: sqlite
backend: mysql
# Enable database caching, should improve database performance
caching: true
settings: