Class ConfigSpec
- java.lang.Object
-
- com.electronwill.nightconfig.core.ConfigSpec
-
- Direct Known Subclasses:
ConcurrentConfigSpec
public class ConfigSpec extends java.lang.ObjectRepresents 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!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": a
Predicatethat returnstrueif and only if the given value is correct.
For instance, this defines a value "arraylist" that must be anArrayList:configSpec.define("arraylist", new ArrayList(), o -> o instanceof ArrayList);Suppliers of default value
If the default value is heavy to create you should use a
Supplierinstead 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 Classes Modifier and Type Class Description static classConfigSpec.CorrectionActionstatic interfaceConfigSpec.CorrectionListenerListens to the corrections made by the methodscorrect(Config)andcorrect(Config, CorrectionListener).
-
Constructor Summary
Constructors Constructor Description ConfigSpec()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcorrect(Config config)Corrects a configuration.intcorrect(Config config, ConfigSpec.CorrectionListener listener)Corrects a configuration.java.lang.Objectcorrect(java.lang.String path, java.lang.Object value)Corrects a value.java.lang.Objectcorrect(java.util.List<java.lang.String> path, java.lang.Object value)Corrects a value.voiddefine(java.lang.String path, java.lang.Object defaultValue)Defines an entry.voiddefine(java.lang.String path, java.lang.Object defaultValue, java.util.function.Predicate<java.lang.Object> validator)Defines an entry.voiddefine(java.lang.String path, java.util.function.Supplier<?> defaultValueSupplier, java.util.function.Predicate<java.lang.Object> validator)Defines an entry.voiddefine(java.util.List<java.lang.String> path, java.lang.Object defaultValue)Defines an entry.voiddefine(java.util.List<java.lang.String> path, java.lang.Object defaultValue, java.util.function.Predicate<java.lang.Object> validator)Defines an entry.voiddefine(java.util.List<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>>
voiddefineEnum(java.lang.String path, java.lang.Class<T> enumType, EnumGetMethod method, java.util.function.Supplier<T> defaultValueSupplier)<T extends java.lang.Enum<T>>
voiddefineEnum(java.lang.String path, T defaultValue, EnumGetMethod method)<T extends java.lang.Enum<T>>
voiddefineEnum(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>>
voiddefineEnum(java.util.List<java.lang.String> path, T defaultValue, EnumGetMethod method)voiddefineInList(java.lang.String path, java.lang.Object defaultValue, java.util.Collection<?> acceptableValues)Defines an entry.voiddefineInList(java.lang.String path, java.util.function.Supplier<?> defaultValueSupplier, java.util.Collection<?> acceptableValues)Defines an entry.voiddefineInList(java.util.List<java.lang.String> path, java.lang.Object defaultValue, java.util.Collection<?> acceptableValues)Defines an entry.voiddefineInList(java.util.List<java.lang.String> path, java.util.function.Supplier<?> defaultValueSupplier, java.util.Collection<?> acceptableValues)Defines an entry.<V extends java.lang.Comparable<? super V>>
voiddefineInRange(java.lang.String path, java.util.function.Supplier<V> defaultValueSupplier, V min, V max)Defines an entry.<V extends java.lang.Comparable<? super V>>
voiddefineInRange(java.lang.String path, V defaultValue, V min, V max)Defines an entry.<V extends java.lang.Comparable<? super V>>
voiddefineInRange(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>>
voiddefineInRange(java.util.List<java.lang.String> path, V defaultValue, V min, V max)Defines an entry.voiddefineList(java.lang.String path, java.util.function.Supplier<java.util.List<?>> defaultValueSupplier, java.util.function.Predicate<java.lang.Object> elementValidator)Defines an entry.voiddefineList(java.lang.String path, java.util.List<?> defaultValue, java.util.function.Predicate<java.lang.Object> elementValidator)Defines an entry.voiddefineList(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.voiddefineList(java.util.List<java.lang.String> path, java.util.List<?> defaultValue, java.util.function.Predicate<java.lang.Object> elementValidator)Defines an entry.<V> voiddefineOfClass(java.lang.String path, java.util.function.Supplier<V> defaultValueSupplier, java.lang.Class<? super V> acceptableValueClass)Defines an entry.<V> voiddefineOfClass(java.lang.String path, V defaultValue, java.lang.Class<? super V> acceptableValueClass)Defines an entry.<V> voiddefineOfClass(java.util.List<java.lang.String> path, java.util.function.Supplier<V> defaultValueSupplier, java.lang.Class<? super V> acceptableValueClass)Defines an entry.<V> voiddefineOfClass(java.util.List<java.lang.String> path, V defaultValue, java.lang.Class<? super V> acceptableValueClass)Defines an entry.<T extends java.lang.Enum<T>>
voiddefineRestrictedEnum(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>>
voiddefineRestrictedEnum(java.lang.String path, T defaultValue, java.util.Collection<T> acceptableValues, EnumGetMethod method)<T extends java.lang.Enum<T>>
voiddefineRestrictedEnum(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>>
voiddefineRestrictedEnum(java.util.List<java.lang.String> path, T defaultValue, java.util.Collection<T> acceptableValues, EnumGetMethod method)booleanisCorrect(Config config)Checks that a configuration is conform to the specification.booleanisCorrect(java.lang.String path, java.lang.Object value)Checks that a value is conform to the specification.booleanisCorrect(java.util.List<java.lang.String> path, java.lang.Object value)Checks that a value is conform to the specification.booleanisDefined(java.lang.String path)Checks if an entry has been defined.booleanisDefined(java.util.List<java.lang.String> path)Checks if an entry has been defined.voidundefine(java.lang.String path)Undefines an entry.voidundefine(java.util.List<java.lang.String> path)Undefines an entry.
-
-
-
Field Detail
-
storage
protected final Config storage
-
-
Method Detail
-
define
public void define(java.lang.String path, java.lang.Object defaultValue)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
public void define(java.util.List<java.lang.String> path, java.lang.Object defaultValue)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
public void define(java.lang.String path, java.lang.Object defaultValue, java.util.function.Predicate<java.lang.Object> validator)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(java.lang.String path, java.util.function.Supplier<?> defaultValueSupplier, java.util.function.Predicate<java.lang.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
-
define
public void define(java.util.List<java.lang.String> path, java.lang.Object defaultValue, java.util.function.Predicate<java.lang.Object> validator)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(java.util.List<java.lang.String> path, java.util.function.Supplier<?> defaultValueSupplier, java.util.function.Predicate<java.lang.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
public <V> void defineOfClass(java.lang.String path, V defaultValue, java.lang.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(java.lang.String path, java.util.function.Supplier<V> defaultValueSupplier, java.lang.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(java.util.List<java.lang.String> path, V defaultValue, java.lang.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(java.util.List<java.lang.String> path, java.util.function.Supplier<V> defaultValueSupplier, java.lang.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
public void defineInList(java.lang.String path, java.lang.Object defaultValue, java.util.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 pathdefaultValue- the default entry valueacceptableValues- the Collection containing all the acceptable values
-
defineInList
public void defineInList(java.lang.String path, java.util.function.Supplier<?> defaultValueSupplier, java.util.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
public void defineInList(java.util.List<java.lang.String> path, java.lang.Object defaultValue, java.util.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 pathdefaultValue- the default entry valueacceptableValues- the Collection containing all the acceptable values
-
defineInList
public void defineInList(java.util.List<java.lang.String> path, java.util.function.Supplier<?> defaultValueSupplier, java.util.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 java.lang.Comparable<? super V>> void defineInRange(java.lang.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 java.lang.Comparable<? super V>> void defineInRange(java.lang.String path, java.util.function.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 java.lang.Comparable<? super V>> void defineInRange(java.util.List<java.lang.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 java.lang.Comparable<? super V>> void defineInRange(java.util.List<java.lang.String> path, java.util.function.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
public void defineList(java.lang.String path, java.util.List<?> defaultValue, java.util.function.Predicate<java.lang.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 pathdefaultValue- the default entry valueelementValidator- the Predicate that checks that every element of the list is correct
-
defineList
public 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. 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
public void defineList(java.util.List<java.lang.String> path, java.util.List<?> defaultValue, java.util.function.Predicate<java.lang.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 pathdefaultValue- the default entry valueelementValidator- the Predicate that checks that every element of the list is correct
-
defineList
public 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. 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
public <T extends java.lang.Enum<T>> void defineEnum(java.lang.String path, T defaultValue, EnumGetMethod method)
-
defineEnum
public <T extends java.lang.Enum<T>> void defineEnum(java.util.List<java.lang.String> path, T defaultValue, EnumGetMethod method)
-
defineEnum
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)
-
defineEnum
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)
-
defineRestrictedEnum
public <T extends java.lang.Enum<T>> void defineRestrictedEnum(java.lang.String path, T defaultValue, java.util.Collection<T> acceptableValues, EnumGetMethod method)
-
defineRestrictedEnum
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)
-
defineRestrictedEnum
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)
-
defineRestrictedEnum
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)
-
undefine
public void undefine(java.lang.String path)
Undefines an entry.- Parameters:
path- the entry's path
-
undefine
public void undefine(java.util.List<java.lang.String> path)
Undefines an entry.- Parameters:
path- the entry's path
-
isDefined
public boolean isDefined(java.lang.String path)
Checks if an entry has been defined.- Parameters:
path- the entry's path- Returns:
trueif it has been defined,falseotherwise
-
isDefined
public boolean isDefined(java.util.List<java.lang.String> path)
Checks if an entry has been defined.- Parameters:
path- the entry's path- Returns:
trueif it has been defined,falseotherwise
-
isCorrect
public boolean isCorrect(java.lang.String path, java.lang.Object value)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
public boolean isCorrect(java.util.List<java.lang.String> path, java.lang.Object value)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
public boolean isCorrect(Config config)
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
public java.lang.Object correct(java.lang.String path, java.lang.Object value)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
public java.lang.Object correct(java.util.List<java.lang.String> path, java.lang.Object value)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
public int correct(Config config)
Corrects a configuration.- Parameters:
config- the config to correct- Returns:
- the number of added, removed or replaced values.
-
correct
public int correct(Config config, ConfigSpec.CorrectionListener listener)
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.
-
-