Class ConfigSpec
java.lang.Object
com.electronwill.nightconfig.core.ConfigSpec
- Direct Known Subclasses:
ConcurrentConfigSpec
Represents a specification for a configuration. With a ConfigSpec you can define mandatory
"properties" that the config's values must have and then check that the config is correct, and
even correct it automatically!
For instance, the following code defines a value with path "a.b" and which must be a String:
For instance, this defines a value "arraylist" that must be an
For instance, the code in the previous paragraph could be rewritten like this:
Defining entries
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");
Validators
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": aPredicate 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);
Suppliers of default value
If the default value is heavy to create you should use aSupplier 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);
Checking configurations
Use the "isCorrect" methods to check whether a configuration is correct or not. A configuration is correct if and only if:*- Each entry defined in the spec is present in the config.
- Each entry in the config is defined in the spec.
- Each entry in the config has a correct value according to the spec.
Correcting configurations
Use the "correct" methods to correct a configuration. The correction behaves like this:- Each entry that is defined in the spec but absent from the config is added to the config, with the default value defined in the spec.
- Each entry that isn't defined in the spec is removed from the config.
- Each incorrect config value is replaced by the default value specified in the spec.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic interfaceListens to the corrections made by the methodscorrect(Config)andcorrect(Config, CorrectionListener). -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintCorrects a configuration.intcorrect(Config config, ConfigSpec.CorrectionListener listener) Corrects a configuration.Corrects a value.Corrects a value.voidDefines an entry.voidDefines an entry.voidDefines an entry.voidDefines an entry.voidDefines an entry.voidDefines an entry.<T extends Enum<T>>
voiddefineEnum(String path, Class<T> enumType, EnumGetMethod method, Supplier<T> defaultValueSupplier) <T extends Enum<T>>
voiddefineEnum(String path, T defaultValue, EnumGetMethod method) <T extends Enum<T>>
voiddefineEnum(List<String> path, Class<T> enumType, EnumGetMethod method, Supplier<T> defaultValueSupplier) <T extends Enum<T>>
voiddefineEnum(List<String> path, T defaultValue, EnumGetMethod method) voiddefineInList(String path, Object defaultValue, Collection<?> acceptableValues) Defines an entry.voiddefineInList(String path, Supplier<?> defaultValueSupplier, Collection<?> acceptableValues) Defines an entry.voiddefineInList(List<String> path, Object defaultValue, Collection<?> acceptableValues) Defines an entry.voiddefineInList(List<String> path, Supplier<?> defaultValueSupplier, Collection<?> acceptableValues) Defines an entry.<V extends Comparable<? super V>>
voiddefineInRange(String path, Supplier<V> defaultValueSupplier, V min, V max) Defines an entry.<V extends Comparable<? super V>>
voiddefineInRange(String path, V defaultValue, V min, V max) Defines an entry.<V extends Comparable<? super V>>
voiddefineInRange(List<String> path, Supplier<V> defaultValueSupplier, V min, V max) Defines an entry.<V extends Comparable<? super V>>
voiddefineInRange(List<String> path, V defaultValue, V min, V max) Defines an entry.voidDefines an entry.voiddefineList(String path, List<?> defaultValue, Predicate<Object> elementValidator) Defines an entry.voiddefineList(List<String> path, Supplier<List<?>> defaultValueSupplier, Predicate<Object> elementValidator) Defines an entry.voidDefines an entry.<V> voiddefineOfClass(String path, Supplier<V> defaultValueSupplier, Class<? super V> acceptableValueClass) Defines an entry.<V> voiddefineOfClass(String path, V defaultValue, Class<? super V> acceptableValueClass) Defines an entry.<V> voiddefineOfClass(List<String> path, Supplier<V> defaultValueSupplier, Class<? super V> acceptableValueClass) Defines an entry.<V> voiddefineOfClass(List<String> path, V defaultValue, Class<? super V> acceptableValueClass) Defines an entry.<T extends Enum<T>>
voiddefineRestrictedEnum(String path, Class<T> enumType, Collection<T> acceptableValues, EnumGetMethod method, Supplier<T> defaultValueSupplier) <T extends Enum<T>>
voiddefineRestrictedEnum(String path, T defaultValue, Collection<T> acceptableValues, EnumGetMethod method) <T extends Enum<T>>
voiddefineRestrictedEnum(List<String> path, Class<T> enumType, Collection<T> acceptableValues, EnumGetMethod method, Supplier<T> defaultValueSupplier) <T extends Enum<T>>
voiddefineRestrictedEnum(List<String> path, T defaultValue, Collection<T> acceptableValues, EnumGetMethod method) booleanChecks that a configuration is conform to the specification.booleanChecks that a value is conform to the specification.booleanChecks that a value is conform to the specification.booleanChecks if an entry has been defined.booleanChecks if an entry has been defined.voidUndefines an entry.voidUndefines an entry.
-
Field Details
-
storage
-
-
Constructor Details
-
ConfigSpec
public ConfigSpec()
-
-
Method Details
-
define
Defines an entry. To be correct, the type of the config value must be of the same as or a subtype of the defaultValue's type.- Parameters:
path- the entry's pathdefaultValue- the default entry value
-
define
Defines an entry. To be correct, the type of the config value must be of the same as or a subtype of the defaultValue's type.- Parameters:
path- the entry's pathdefaultValue- the default entry value
-
define
Defines an entry. A config value is considered correct if and only ifvalidator.test(configValue)returns true.- Parameters:
path- the entry's pathdefaultValue- the default entry valuevalidator- the Predicate that determines if the value is correct or not
-
define
Defines an entry. A config value is considered correct if and only ifvalidator.test(configValue)returns true.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valuevalidator- the Predicate that determines if the value is correct or not
-
define
Defines an entry. A config value is considered correct if and only ifvalidator.test(configValue)returns true.- Parameters:
path- the entry's pathdefaultValue- the default entry valuevalidator- the Predicate that determines if the value is correct or not
-
define
public void define(List<String> path, Supplier<?> defaultValueSupplier, Predicate<Object> validator) Defines an entry. A config value is considered correct if and only ifvalidator.test(configValue)returns true.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valuevalidator- the Predicate that determines if the value is correct or not
-
defineOfClass
Defines an entry. A config value is considered correct if and only if it is of the same type as, or of a subtype of the specified class.- Parameters:
path- the entry's pathdefaultValue- the default entry valueacceptableValueClass- the class that a value of this entry must have
-
defineOfClass
public <V> void defineOfClass(String path, Supplier<V> defaultValueSupplier, Class<? super V> acceptableValueClass) Defines an entry. A config value is considered correct if and only if it is of the same type as, or of a subtype of the specified class.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valueacceptableValueClass- the class that a value of this entry must have
-
defineOfClass
public <V> void defineOfClass(List<String> path, V defaultValue, Class<? super V> acceptableValueClass) Defines an entry. A config value is considered correct if and only if it is of the same type as, or of a subtype of the specified class.- Parameters:
path- the entry's pathdefaultValue- the default entry valueacceptableValueClass- the class that a value of this entry must have
-
defineOfClass
public <V> void defineOfClass(List<String> path, Supplier<V> defaultValueSupplier, Class<? super V> acceptableValueClass) Defines an entry. A config value is considered correct if and only if it is of the same type as, or of a subtype of the specified class.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valueacceptableValueClass- the class that a value of this entry must have
-
defineInList
Defines an entry. A config value is considered correct if and only if it is contained in the specified collection.- Parameters:
path- the entry's pathdefaultValue- the default entry valueacceptableValues- the Collection containing all the acceptable values
-
defineInList
public void defineInList(String path, Supplier<?> defaultValueSupplier, Collection<?> acceptableValues) Defines an entry. A config value is considered correct if and only if it is contained in the specified collection.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplifer of the default entry valueacceptableValues- the Collection containing all the acceptable values
-
defineInList
Defines an entry. A config value is considered correct if and only if it is contained in the specified collection.- Parameters:
path- the entry's pathdefaultValue- the default entry valueacceptableValues- the Collection containing all the acceptable values
-
defineInList
public void defineInList(List<String> path, Supplier<?> defaultValueSupplier, Collection<?> acceptableValues) Defines an entry. A config value is considered correct if and only if it is contained in the specified collection.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valueacceptableValues- the Collection containing all the acceptable values
-
defineInRange
public <V extends Comparable<? super V>> void defineInRange(String path, V defaultValue, V min, V max) Defines an entry. A config value is considered correct if and only if it is less than or equal tominand bigger than or equal tomax.- Parameters:
path- the entry's pathdefaultValue- the default entry valuemin- the minimum, inclusivemax- the maximum, inclusive
-
defineInRange
public <V extends Comparable<? super V>> void defineInRange(String path, Supplier<V> defaultValueSupplier, V min, V max) Defines an entry. A config value is considered correct if and only if it is less than or equal tominand bigger than or equal tomax.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valuemin- the minimum, inclusivemax- the maximum, inclusive
-
defineInRange
public <V extends Comparable<? super V>> void defineInRange(List<String> path, V defaultValue, V min, V max) Defines an entry. A config value is considered correct if and only if it is less than or equal tominand bigger than or equal tomax.- Parameters:
path- the entry's pathdefaultValue- the default entry valuemin- the minimum, inclusivemax- the maximum, inclusive
-
defineInRange
public <V extends Comparable<? super V>> void defineInRange(List<String> path, Supplier<V> defaultValueSupplier, V min, V max) Defines an entry. A config value is considered correct if and only if it is less than or equal tominand bigger than or equal tomax.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valuemin- the minimum, inclusivemax- the maximum, inclusive
-
defineList
Defines an entry. A config value is considered correct if and only if all its element are valid according to theelementValidator, that is, if and only if for all element e in the list the callelementValidator.test(e)returns true.- Parameters:
path- the entry's pathdefaultValue- the default entry valueelementValidator- the Predicate that checks that every element of the list is correct
-
defineList
public void defineList(String path, Supplier<List<?>> defaultValueSupplier, Predicate<Object> elementValidator) Defines an entry. A config value is considered correct if and only if all its element are valid according to theelementValidator, that is, if and only if for all element e in the list the callelementValidator.test(e)returns true.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valueelementValidator- the Predicate that checks that every element of the list is correct
-
defineList
Defines an entry. A config value is considered correct if and only if all its element are valid according to theelementValidator, that is, if and only if for all element e in the list the callelementValidator.test(e)returns true.- Parameters:
path- the entry's pathdefaultValue- the default entry valueelementValidator- the Predicate that checks that every element of the list is correct
-
defineList
public void defineList(List<String> path, Supplier<List<?>> defaultValueSupplier, Predicate<Object> elementValidator) Defines an entry. A config value is considered correct if and only if all its element are valid according to theelementValidator, that is, if and only if for all element e in the list the callelementValidator.test(e)returns true.- Parameters:
path- the entry's pathdefaultValueSupplier- the Supplier of the default entry valueelementValidator- the Predicate that checks that every element of the list is correct
-
defineEnum
-
defineEnum
-
defineEnum
public <T extends Enum<T>> void defineEnum(String path, Class<T> enumType, EnumGetMethod method, Supplier<T> defaultValueSupplier) -
defineEnum
-
defineRestrictedEnum
public <T extends Enum<T>> void defineRestrictedEnum(String path, T defaultValue, Collection<T> acceptableValues, EnumGetMethod method) -
defineRestrictedEnum
public <T extends Enum<T>> void defineRestrictedEnum(List<String> path, T defaultValue, Collection<T> acceptableValues, EnumGetMethod method) -
defineRestrictedEnum
public <T extends Enum<T>> void defineRestrictedEnum(String path, Class<T> enumType, Collection<T> acceptableValues, EnumGetMethod method, Supplier<T> defaultValueSupplier) -
defineRestrictedEnum
public <T extends Enum<T>> void defineRestrictedEnum(List<String> path, Class<T> enumType, Collection<T> acceptableValues, EnumGetMethod method, Supplier<T> defaultValueSupplier) -
undefine
Undefines an entry.- Parameters:
path- the entry's path
-
undefine
Undefines an entry.- Parameters:
path- the entry's path
-
isDefined
Checks if an entry has been defined.- Parameters:
path- the entry's path- Returns:
trueif it has been defined,falseotherwise
-
isDefined
Checks if an entry has been defined.- Parameters:
path- the entry's path- Returns:
trueif it has been defined,falseotherwise
-
isCorrect
Checks that a value is conform to the specification.- Parameters:
path- the entry's pathvalue- the entry's value- Returns:
trueif it's correct,falseif it's incorrect
-
isCorrect
Checks that a value is conform to the specification.- Parameters:
path- the entry's pathvalue- the entry's value- Returns:
trueif it's correct,falseif it's incorrect
-
isCorrect
Checks that a configuration is conform to the specification.- Parameters:
config- the config to check- Returns:
trueif it's correct,falseif it's incorrect
-
correct
Corrects a value.- Parameters:
path- the value's pathvalue- the value to correct- Returns:
- the corrected value, or the value itself if's it already correct
-
correct
Corrects a value.- Parameters:
path- the value's pathvalue- the value to correct- Returns:
- the corrected value, or the value itself if's it already correct
-
correct
Corrects a configuration.- Parameters:
config- the config to correct- Returns:
- the number of added, removed or replaced values.
-
correct
Corrects a configuration.- Parameters:
config- the config to correctlistener- the listener that will be notified of every change made during the correction of the config.- Returns:
- the number of added, removed or replaced values.
-