-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e58f636
commit a55a620
Showing
10 changed files
with
227 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.leavesmc.leaves.config; | ||
|
||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class AutoConfigTransformer implements ConfigTransformer<Object, Object> { | ||
|
||
@NotNull | ||
@Contract("_ -> new") | ||
public static ConfigTransformer<?, ?> createValidator(@NotNull Field field) { | ||
return new SimpleConfigTransformer(AutoConfigValidator.createValidator(field)); | ||
} | ||
|
||
@Override | ||
public Object transform(Object from) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public Object stringConvert(String value) throws IllegalArgumentException { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public Class<Object> getFieldClass() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public static class SimpleConfigTransformer implements ConfigTransformer<Object, Object> { | ||
|
||
private final ConfigValidator validator; | ||
|
||
public SimpleConfigTransformer(ConfigValidator<?> validator) { | ||
this.validator = validator; | ||
} | ||
|
||
@Override | ||
public Object transform(Object o) { | ||
return o; | ||
} | ||
|
||
@Override | ||
public Object stringConvert(String value) throws IllegalArgumentException { | ||
return validator.stringConvert(value); | ||
} | ||
|
||
@Override | ||
public Object loadConvert(Object value) throws IllegalArgumentException { | ||
return validator.loadConvert(value); | ||
} | ||
|
||
@Override | ||
public Object saveConvert(Object value) { | ||
return validator.saveConvert(value); | ||
} | ||
|
||
@Override | ||
public Class<Object> getFieldClass() { | ||
return validator.getFieldClass(); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.leavesmc.leaves.config; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public final class AutoConfigValidator implements ConfigValidator<Object> { | ||
|
||
private static final Map<Class<?>, Supplier<ConfigValidator<?>>> BASE_VALIDATOR = new HashMap<>(); | ||
|
||
static { | ||
BASE_VALIDATOR.put(Boolean.class, ConfigValidatorImpl.BooleanConfigValidator::new); | ||
BASE_VALIDATOR.put(boolean.class, ConfigValidatorImpl.BooleanConfigValidator::new); | ||
|
||
BASE_VALIDATOR.put(Integer.class, ConfigValidatorImpl.IntConfigValidator::new); | ||
BASE_VALIDATOR.put(int.class, ConfigValidatorImpl.IntConfigValidator::new); | ||
|
||
BASE_VALIDATOR.put(Double.class, ConfigValidatorImpl.DoubleConfigValidator::new); | ||
BASE_VALIDATOR.put(double.class, ConfigValidatorImpl.DoubleConfigValidator::new); | ||
|
||
BASE_VALIDATOR.put(String.class, ConfigValidatorImpl.StringConfigValidator::new); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public static ConfigValidator<?> createValidator(@NotNull Field field) { | ||
Class<?> fieldType = field.getType(); | ||
|
||
if (BASE_VALIDATOR.containsKey(fieldType)) { | ||
return BASE_VALIDATOR.get(fieldType).get(); | ||
} | ||
|
||
if (List.class.isAssignableFrom(fieldType)) { | ||
Type genericType = field.getGenericType(); | ||
if (genericType instanceof ParameterizedType parameterizedType) { | ||
Type[] typeArgs = parameterizedType.getActualTypeArguments(); | ||
if (typeArgs.length > 0 && typeArgs[0] instanceof Class<?> genericClass) { | ||
if (genericClass.equals(String.class)) { | ||
return new ConfigValidatorImpl.ListConfigValidator.STRING(); | ||
} | ||
throw new IllegalArgumentException("List type " + genericClass.getTypeName() + " is not supported."); | ||
} | ||
} | ||
throw new IllegalArgumentException("List type " + genericType.getTypeName() + " is not supported."); | ||
} | ||
|
||
if (fieldType.isEnum()) { | ||
return new ConfigValidatorImpl.EnumConfigValidator<>((Class<Enum>) fieldType); | ||
} | ||
|
||
throw new IllegalArgumentException("Can't find validator for type " + fieldType.getName()); | ||
} | ||
|
||
@Override | ||
public Object stringConvert(String value) throws IllegalArgumentException { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public Class<Object> getFieldClass() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.leavesmc.leaves.config; | ||
|
||
public interface ConfigTransformer<FROM, TO> extends ConfigConverter<FROM> { | ||
TO transform(FROM from); | ||
} |
Oops, something went wrong.