#1014 Use ConfigMe improvements to create custom Enum set property

This commit is contained in:
ljacqu
2016-12-23 23:51:23 +01:00
parent 9ce680f56e
commit d717f75bb4
5 changed files with 27 additions and 31 deletions
@@ -1,38 +1,38 @@
package fr.xephi.authme.settings;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.properties.StringListProperty;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyResource;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.collect.Sets.newHashSet;
/**
* Property whose value is a set of entries of a given enum.
*/
// TODO #1014: This property type currently extends StringListProperty with a dedicated method to convert the values
// into a Set of the selected enum due to multiple issues on ConfigMe's side.
public class EnumSetProperty<E extends Enum<E>> extends StringListProperty {
public class EnumSetProperty<E extends Enum<E>> extends Property<Set<E>> {
private final Class<E> enumClass;
public EnumSetProperty(Class<E> enumClass, String path, String... values) {
super(path, values);
@SafeVarargs
public EnumSetProperty(Class<E> enumClass, String path, E... values) {
super(path, newHashSet(values));
this.enumClass = enumClass;
}
/**
* Returns the value as a set of enum entries.
*
* @param settings the settings manager to look up the raw value with
* @return the property's value as mapped enum entries
*/
public Set<E> asEnumSet(SettingsManager settings) {
List<String> entries = settings.getProperty(this);
return entries.stream()
.map(str -> toEnum(str))
.filter(e -> e != null)
.collect(Collectors.toSet());
@Override
protected Set<E> getFromResource(PropertyResource resource) {
List<?> elements = resource.getList(getPath());
if (elements != null) {
return elements.stream()
.map(val -> toEnum(String.valueOf(val)))
.filter(e -> e != null)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
return null;
}
private E toEnum(String str) {