public class ConfigSpec
extends java.lang.Object
Use the "define" methods to define that some entry must be present in the config, and how its
value must be. You have to specify - at least - the path of the value and a default value that
will be used to replace the config's value in case it's incorrect.
For instance, the following code defines a value with path "a.b" and which must be a String:
configSpec.define("a.b", "defaultString");
Some methods (like the one used in the previous paragraph) automatically generate the rules that
make a config value correct or incorrect. But you can provide your own rule by specifying a
"validator": a Predicate that returns true if and only if the given value is
correct.
For instance, this defines a value "arraylist" that must be an ArrayList:
configSpec.define("arraylist", new ArrayList(), o -> o instanceof ArrayList);
If the default value is heavy to create you should use a Supplier instead of creating a
default value, which is useless if the config's value happens to be correct.
For instance, the code in the previous paragraph could be rewritten like this:
configSpec.define("heavy", () -> new ArrayList(), o -> o instanceof ArrayList);
Use the "isCorrect" methods to check whether a configuration is correct or not. A configuration is correct if and only if:*
Use the "correct" methods to correct a configuration. The correction behaves like this:
| Modifier and Type | Class and Description |
|---|---|
static class |
ConfigSpec.CorrectionAction |
static interface |
ConfigSpec.CorrectionListener
Listens to the corrections made by the methods
correct(Config) and
correct(Config, CorrectionListener). |
| Constructor and Description |
|---|
ConfigSpec() |
| Modifier and Type | Method and Description |
|---|---|
int |
correct(Config config)
Corrects a configuration.
|
int |
correct(Config config,
ConfigSpec.CorrectionListener listener)
Corrects a configuration.
|
java.lang.Object |
correct(java.util.List<java.lang.String> path,
java.lang.Object value)
Corrects a value.
|
java.lang.Object |
correct(java.lang.String path,
java.lang.Object value)
Corrects a value.
|
void |
define(java.util.List<java.lang.String> path,
java.lang.Object defaultValue)
Defines an entry.
|
void |
define(java.util.List<java.lang.String> path,
java.lang.Object defaultValue,
java.util.function.Predicate<java.lang.Object> validator)
Defines an entry.
|
void |
define(java.util.List<java.lang.String> path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> validator)
Defines an entry.
|
void |
define(java.lang.String path,
java.lang.Object defaultValue)
Defines an entry.
|
void |
define(java.lang.String path,
java.lang.Object defaultValue,
java.util.function.Predicate<java.lang.Object> validator)
Defines an entry.
|
void |
define(java.lang.String path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> validator)
Defines an entry.
|
<T extends java.lang.Enum<T>> |
defineEnum(java.util.List<java.lang.String> path,
java.lang.Class<T> enumType,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier) |
<T extends java.lang.Enum<T>> |
defineEnum(java.util.List<java.lang.String> path,
T defaultValue,
EnumGetMethod method) |
<T extends java.lang.Enum<T>> |
defineEnum(java.lang.String path,
java.lang.Class<T> enumType,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier) |
<T extends java.lang.Enum<T>> |
defineEnum(java.lang.String path,
T defaultValue,
EnumGetMethod method) |
void |
defineInList(java.util.List<java.lang.String> path,
java.lang.Object defaultValue,
java.util.Collection<?> acceptableValues)
Defines an entry.
|
void |
defineInList(java.util.List<java.lang.String> path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.Collection<?> acceptableValues)
Defines an entry.
|
void |
defineInList(java.lang.String path,
java.lang.Object defaultValue,
java.util.Collection<?> acceptableValues)
Defines an entry.
|
void |
defineInList(java.lang.String path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.Collection<?> acceptableValues)
Defines an entry.
|
<V extends java.lang.Comparable<? super V>> |
defineInRange(java.util.List<java.lang.String> path,
java.util.function.Supplier<V> defaultValueSupplier,
V min,
V max)
Defines an entry.
|
<V extends java.lang.Comparable<? super V>> |
defineInRange(java.util.List<java.lang.String> path,
V defaultValue,
V min,
V max)
Defines an entry.
|
<V extends java.lang.Comparable<? super V>> |
defineInRange(java.lang.String path,
java.util.function.Supplier<V> defaultValueSupplier,
V min,
V max)
Defines an entry.
|
<V extends java.lang.Comparable<? super V>> |
defineInRange(java.lang.String path,
V defaultValue,
V min,
V max)
Defines an entry.
|
void |
defineList(java.util.List<java.lang.String> path,
java.util.List<?> defaultValue,
java.util.function.Predicate<java.lang.Object> elementValidator)
Defines an entry.
|
void |
defineList(java.util.List<java.lang.String> path,
java.util.function.Supplier<java.util.List<?>> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> elementValidator)
Defines an entry.
|
void |
defineList(java.lang.String path,
java.util.List<?> defaultValue,
java.util.function.Predicate<java.lang.Object> elementValidator)
Defines an entry.
|
void |
defineList(java.lang.String path,
java.util.function.Supplier<java.util.List<?>> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> elementValidator)
Defines an entry.
|
<V> void |
defineOfClass(java.util.List<java.lang.String> path,
java.util.function.Supplier<V> defaultValueSupplier,
java.lang.Class<? super V> acceptableValueClass)
Defines an entry.
|
<V> void |
defineOfClass(java.util.List<java.lang.String> path,
V defaultValue,
java.lang.Class<? super V> acceptableValueClass)
Defines an entry.
|
<V> void |
defineOfClass(java.lang.String path,
java.util.function.Supplier<V> defaultValueSupplier,
java.lang.Class<? super V> acceptableValueClass)
Defines an entry.
|
<V> void |
defineOfClass(java.lang.String path,
V defaultValue,
java.lang.Class<? super V> acceptableValueClass)
Defines an entry.
|
<T extends java.lang.Enum<T>> |
defineRestrictedEnum(java.util.List<java.lang.String> path,
java.lang.Class<T> enumType,
java.util.Collection<T> acceptableValues,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier) |
<T extends java.lang.Enum<T>> |
defineRestrictedEnum(java.util.List<java.lang.String> path,
T defaultValue,
java.util.Collection<T> acceptableValues,
EnumGetMethod method) |
<T extends java.lang.Enum<T>> |
defineRestrictedEnum(java.lang.String path,
java.lang.Class<T> enumType,
java.util.Collection<T> acceptableValues,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier) |
<T extends java.lang.Enum<T>> |
defineRestrictedEnum(java.lang.String path,
T defaultValue,
java.util.Collection<T> acceptableValues,
EnumGetMethod method) |
boolean |
isCorrect(Config config)
Checks that a configuration is conform to the specification.
|
boolean |
isCorrect(java.util.List<java.lang.String> path,
java.lang.Object value)
Checks that a value is conform to the specification.
|
boolean |
isCorrect(java.lang.String path,
java.lang.Object value)
Checks that a value is conform to the specification.
|
boolean |
isDefined(java.util.List<java.lang.String> path)
Checks if an entry has been defined.
|
boolean |
isDefined(java.lang.String path)
Checks if an entry has been defined.
|
void |
undefine(java.util.List<java.lang.String> path)
Undefines an entry.
|
void |
undefine(java.lang.String path)
Undefines an entry.
|
protected final Config storage
public void define(java.lang.String path,
java.lang.Object defaultValue)
path - the entry's pathdefaultValue - the default entry valuepublic void define(java.util.List<java.lang.String> path,
java.lang.Object defaultValue)
path - the entry's pathdefaultValue - the default entry valuepublic void define(java.lang.String path,
java.lang.Object defaultValue,
java.util.function.Predicate<java.lang.Object> validator)
validator.test(configValue) returns true.path - the entry's pathdefaultValue - the default entry valuevalidator - the Predicate that determines if the value is correct or notpublic void define(java.lang.String path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> validator)
validator.test(configValue) returns true.path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valuevalidator - the Predicate that determines if the value is correct or notpublic void define(java.util.List<java.lang.String> path,
java.lang.Object defaultValue,
java.util.function.Predicate<java.lang.Object> validator)
validator.test(configValue) returns true.path - the entry's pathdefaultValue - the default entry valuevalidator - the Predicate that determines if the value is correct or notpublic void define(java.util.List<java.lang.String> path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> validator)
validator.test(configValue) returns true.path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valuevalidator - the Predicate that determines if the value is correct or notpublic <V> void defineOfClass(java.lang.String path,
V defaultValue,
java.lang.Class<? super V> acceptableValueClass)
path - the entry's pathdefaultValue - the default entry valueacceptableValueClass - the class that a value of this entry must havepublic <V> void defineOfClass(java.lang.String path,
java.util.function.Supplier<V> defaultValueSupplier,
java.lang.Class<? super V> acceptableValueClass)
path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valueacceptableValueClass - the class that a value of this entry must havepublic <V> void defineOfClass(java.util.List<java.lang.String> path,
V defaultValue,
java.lang.Class<? super V> acceptableValueClass)
path - the entry's pathdefaultValue - the default entry valueacceptableValueClass - the class that a value of this entry must havepublic <V> void defineOfClass(java.util.List<java.lang.String> path,
java.util.function.Supplier<V> defaultValueSupplier,
java.lang.Class<? super V> acceptableValueClass)
path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valueacceptableValueClass - the class that a value of this entry must havepublic void defineInList(java.lang.String path,
java.lang.Object defaultValue,
java.util.Collection<?> acceptableValues)
path - the entry's pathdefaultValue - the default entry valueacceptableValues - the Collection containing all the acceptable valuespublic void defineInList(java.lang.String path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.Collection<?> acceptableValues)
path - the entry's pathdefaultValueSupplier - the Supplifer of the default entry valueacceptableValues - the Collection containing all the acceptable valuespublic void defineInList(java.util.List<java.lang.String> path,
java.lang.Object defaultValue,
java.util.Collection<?> acceptableValues)
path - the entry's pathdefaultValue - the default entry valueacceptableValues - the Collection containing all the acceptable valuespublic void defineInList(java.util.List<java.lang.String> path,
java.util.function.Supplier<?> defaultValueSupplier,
java.util.Collection<?> acceptableValues)
path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valueacceptableValues - the Collection containing all the acceptable valuespublic <V extends java.lang.Comparable<? super V>> void defineInRange(java.lang.String path,
V defaultValue,
V min,
V max)
min and bigger than or equal to max.path - the entry's pathdefaultValue - the default entry valuemin - the minimum, inclusivemax - the maximum, inclusivepublic <V extends java.lang.Comparable<? super V>> void defineInRange(java.lang.String path,
java.util.function.Supplier<V> defaultValueSupplier,
V min,
V max)
min and bigger than or equal to max.path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valuemin - the minimum, inclusivemax - the maximum, inclusivepublic <V extends java.lang.Comparable<? super V>> void defineInRange(java.util.List<java.lang.String> path,
V defaultValue,
V min,
V max)
min and bigger than or equal to max.path - the entry's pathdefaultValue - the default entry valuemin - the minimum, inclusivemax - the maximum, inclusivepublic <V extends java.lang.Comparable<? super V>> void defineInRange(java.util.List<java.lang.String> path,
java.util.function.Supplier<V> defaultValueSupplier,
V min,
V max)
min and bigger than or equal to max.path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valuemin - the minimum, inclusivemax - the maximum, inclusivepublic void defineList(java.lang.String path,
java.util.List<?> defaultValue,
java.util.function.Predicate<java.lang.Object> elementValidator)
elementValidator, that is, if and only if for all element e
in the list the call elementValidator.test(e) returns true.path - the entry's pathdefaultValue - the default entry valueelementValidator - the Predicate that checks that every element of the list is correctpublic void defineList(java.lang.String path,
java.util.function.Supplier<java.util.List<?>> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> elementValidator)
elementValidator, that is, if and only if for all element e
in the list the call elementValidator.test(e) returns true.path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valueelementValidator - the Predicate that checks that every element of the list is
correctpublic void defineList(java.util.List<java.lang.String> path,
java.util.List<?> defaultValue,
java.util.function.Predicate<java.lang.Object> elementValidator)
elementValidator, that is, if and only if for all element e
in the list the call elementValidator.test(e) returns true.path - the entry's pathdefaultValue - the default entry valueelementValidator - the Predicate that checks that every element of the list is correctpublic void defineList(java.util.List<java.lang.String> path,
java.util.function.Supplier<java.util.List<?>> defaultValueSupplier,
java.util.function.Predicate<java.lang.Object> elementValidator)
elementValidator, that is, if and only if for all element e
in the list the call elementValidator.test(e) returns true.path - the entry's pathdefaultValueSupplier - the Supplier of the default entry valueelementValidator - the Predicate that checks that every element of the list is
correctpublic <T extends java.lang.Enum<T>> void defineEnum(java.lang.String path,
T defaultValue,
EnumGetMethod method)
public <T extends java.lang.Enum<T>> void defineEnum(java.util.List<java.lang.String> path,
T defaultValue,
EnumGetMethod method)
public <T extends java.lang.Enum<T>> void defineEnum(java.lang.String path,
java.lang.Class<T> enumType,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier)
public <T extends java.lang.Enum<T>> void defineEnum(java.util.List<java.lang.String> path,
java.lang.Class<T> enumType,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier)
public <T extends java.lang.Enum<T>> void defineRestrictedEnum(java.lang.String path,
T defaultValue,
java.util.Collection<T> acceptableValues,
EnumGetMethod method)
public <T extends java.lang.Enum<T>> void defineRestrictedEnum(java.util.List<java.lang.String> path,
T defaultValue,
java.util.Collection<T> acceptableValues,
EnumGetMethod method)
public <T extends java.lang.Enum<T>> void defineRestrictedEnum(java.lang.String path,
java.lang.Class<T> enumType,
java.util.Collection<T> acceptableValues,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier)
public <T extends java.lang.Enum<T>> void defineRestrictedEnum(java.util.List<java.lang.String> path,
java.lang.Class<T> enumType,
java.util.Collection<T> acceptableValues,
EnumGetMethod method,
java.util.function.Supplier<T> defaultValueSupplier)
public void undefine(java.lang.String path)
path - the entry's pathpublic void undefine(java.util.List<java.lang.String> path)
path - the entry's pathpublic boolean isDefined(java.lang.String path)
path - the entry's pathtrue if it has been defined, false otherwisepublic boolean isDefined(java.util.List<java.lang.String> path)
path - the entry's pathtrue if it has been defined, false otherwisepublic boolean isCorrect(java.lang.String path,
java.lang.Object value)
path - the entry's pathvalue - the entry's valuetrue if it's correct, false if it's incorrectpublic boolean isCorrect(java.util.List<java.lang.String> path,
java.lang.Object value)
path - the entry's pathvalue - the entry's valuetrue if it's correct, false if it's incorrectpublic boolean isCorrect(Config config)
config - the config to checktrue if it's correct, false if it's incorrectpublic java.lang.Object correct(java.lang.String path,
java.lang.Object value)
path - the value's pathvalue - the value to correctpublic java.lang.Object correct(java.util.List<java.lang.String> path,
java.lang.Object value)
path - the value's pathvalue - the value to correctpublic int correct(Config config)
config - the config to correctpublic int correct(Config config, ConfigSpec.CorrectionListener listener)
config - the config to correctlistener - the listener that will be notified of every change made during the
correction of the config.