Create enum property type, trivial code simplification
This commit is contained in:
parent
7d41ccbc9c
commit
cb07b3df3d
@ -1,5 +1,6 @@
|
|||||||
package fr.xephi.authme.settings.custom;
|
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.Comment;
|
||||||
import fr.xephi.authme.settings.custom.domain.Property;
|
import fr.xephi.authme.settings.custom.domain.Property;
|
||||||
import fr.xephi.authme.settings.custom.domain.SettingsClass;
|
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?",
|
@Comment({"What type of database do you want to use?",
|
||||||
"Valid values: sqlite, mysql"})
|
"Valid values: sqlite, mysql"})
|
||||||
public static final Property<String> BACKEND =
|
public static final Property<DataSource.DataSourceType> BACKEND =
|
||||||
newProperty(STRING, "DataSource.backend", "sqlite");
|
newProperty(DataSource.DataSourceType.class, "DataSource.backend", DataSource.DataSourceType.SQLITE);
|
||||||
|
|
||||||
@Comment("Enable database caching, should improve database performance")
|
@Comment("Enable database caching, should improve database performance")
|
||||||
public static final Property<Boolean> USE_CACHING =
|
public static final Property<Boolean> USE_CACHING =
|
||||||
|
|||||||
@ -28,8 +28,6 @@ public class NewSetting {
|
|||||||
ConverterSettings.class, DatabaseSettings.class, EmailSettings.class, HooksSettings.class,
|
ConverterSettings.class, DatabaseSettings.class, EmailSettings.class, HooksSettings.class,
|
||||||
ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class);
|
ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class);
|
||||||
|
|
||||||
private static final int YAML_INDENTATION = 4;
|
|
||||||
|
|
||||||
private File file;
|
private File file;
|
||||||
private YamlConfiguration configuration;
|
private YamlConfiguration configuration;
|
||||||
|
|
||||||
@ -75,7 +73,7 @@ public class NewSetting {
|
|||||||
if (newPathParts.size() > 1) {
|
if (newPathParts.size() > 1) {
|
||||||
for (String path : newPathParts.subList(0, newPathParts.size() - 1)) {
|
for (String path : newPathParts.subList(0, newPathParts.size() - 1)) {
|
||||||
writer.append("\n")
|
writer.append("\n")
|
||||||
.append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION))
|
.append(indent(indentationLevel))
|
||||||
.append(path)
|
.append(path)
|
||||||
.append(": ");
|
.append(": ");
|
||||||
++indentationLevel;
|
++indentationLevel;
|
||||||
@ -83,12 +81,12 @@ public class NewSetting {
|
|||||||
}
|
}
|
||||||
for (String comment : entry.getValue()) {
|
for (String comment : entry.getValue()) {
|
||||||
writer.append("\n")
|
writer.append("\n")
|
||||||
.append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION))
|
.append(indent(indentationLevel))
|
||||||
.append("# ")
|
.append("# ")
|
||||||
.append(comment);
|
.append(comment);
|
||||||
}
|
}
|
||||||
writer.append("\n")
|
writer.append("\n")
|
||||||
.append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION))
|
.append(indent(indentationLevel))
|
||||||
.append(CollectionUtils.getRange(newPathParts, newPathParts.size() - 1).get(0))
|
.append(CollectionUtils.getRange(newPathParts, newPathParts.size() - 1).get(0))
|
||||||
.append(": ");
|
.append(": ");
|
||||||
|
|
||||||
@ -96,7 +94,7 @@ public class NewSetting {
|
|||||||
String delim = "";
|
String delim = "";
|
||||||
for (String yamlLine : yamlLines) {
|
for (String yamlLine : yamlLines) {
|
||||||
writer.append(delim).append(yamlLine);
|
writer.append(delim).append(yamlLine);
|
||||||
delim = "\n" + StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION);
|
delim = "\n" + indent(indentationLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentPath = propertyPath.subList(0, propertyPath.size() - 1);
|
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() {
|
private static PropertyMap getAllPropertyFields() {
|
||||||
PropertyMap properties = new PropertyMap();
|
PropertyMap properties = new PropertyMap();
|
||||||
for (Class<?> clazz : CONFIGURATION_CLASSES) {
|
for (Class<?> clazz : CONFIGURATION_CLASSES) {
|
||||||
|
|||||||
@ -31,6 +31,10 @@ public class Property<T> {
|
|||||||
return new Property<>(type, path, Arrays.asList(defaultValues));
|
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
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,15 +60,13 @@ public final class CollectionUtils {
|
|||||||
return coll == null || coll.isEmpty();
|
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<>();
|
List<T> commonStart = new ArrayList<>();
|
||||||
int minSize = Math.min(coll1.size(), coll2.size());
|
int minSize = Math.min(list1.size(), list2.size());
|
||||||
for (int i = 0; i < minSize; ++i) {
|
int i = 0;
|
||||||
if (Objects.equals(coll1.get(i), coll2.get(i))) {
|
while (i < minSize && Objects.equals(list1.get(i), list2.get(i))) {
|
||||||
commonStart.add(coll1.get(i));
|
commonStart.add(list1.get(i));
|
||||||
} else {
|
++i;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return commonStart;
|
return commonStart;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -117,12 +117,4 @@ public final class StringUtils {
|
|||||||
return "[" + th.getClass().getSimpleName() + "]: " + th.getMessage();
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package fr.xephi.authme.settings.custom;
|
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.Property;
|
||||||
import fr.xephi.authme.settings.custom.domain.PropertyType;
|
import fr.xephi.authme.settings.custom.domain.PropertyType;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
@ -83,12 +84,14 @@ public class NewSettingTest {
|
|||||||
String helpHeader = settings.getOption(newProperty("settings.helpHeader", ""));
|
String helpHeader = settings.getOption(newProperty("settings.helpHeader", ""));
|
||||||
List<String> unsafePasswords = settings.getOption(
|
List<String> unsafePasswords = settings.getOption(
|
||||||
newProperty(PropertyType.STRING_LIST, "Security.unsafePasswords"));
|
newProperty(PropertyType.STRING_LIST, "Security.unsafePasswords"));
|
||||||
|
DataSource.DataSourceType dataSourceType = settings.getOption(DatabaseSettings.BACKEND);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(22));
|
assertThat(result, equalTo(22));
|
||||||
assertThat(systemName, equalTo(TestConfiguration.SYSTEM_NAME.getDefaultValue()));
|
assertThat(systemName, equalTo(TestConfiguration.SYSTEM_NAME.getDefaultValue()));
|
||||||
assertThat(helpHeader, equalTo("AuthMeReloaded"));
|
assertThat(helpHeader, equalTo("AuthMeReloaded"));
|
||||||
assertThat(unsafePasswords, contains("123456", "qwerty", "54321"));
|
assertThat(unsafePasswords, contains("123456", "qwerty", "54321"));
|
||||||
|
assertThat(dataSourceType, equalTo(DataSource.DataSourceType.MYSQL));
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getConfigFile() {
|
private File getConfigFile() {
|
||||||
|
|||||||
@ -3,7 +3,7 @@ test:
|
|||||||
DataSource:
|
DataSource:
|
||||||
# What type of database do you want to use?
|
# What type of database do you want to use?
|
||||||
# Valid values: sqlite, mysql
|
# Valid values: sqlite, mysql
|
||||||
backend: sqlite
|
backend: mysql
|
||||||
# Enable database caching, should improve database performance
|
# Enable database caching, should improve database performance
|
||||||
caching: true
|
caching: true
|
||||||
settings:
|
settings:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user