Package net.starype.quiz.api.parser
Class ConfigMatcher<T>
- java.lang.Object
-
- net.starype.quiz.api.parser.ConfigMatcher<T>
-
- Type Parameters:
T
- the type of result produced by the mappers
public class ConfigMatcher<T> extends java.lang.Object
GroupsConfigMapper
s, allowing simple matchings by key or value.
-
-
Constructor Summary
Constructors Constructor Description ConfigMatcher(java.util.Collection<ConfigMapper<T>> mappers, ConfigMapper<T> defaultMapper)
Construct a ConfigMatcher with a default mapper, used in caseloadFromKeyOrDefault(String, ReadableMap)
fails to find any mapper.
-
Method Summary
Modifier and Type Method Description java.util.Optional<T>
loadFromKey(java.lang.String key, ReadableMap config)
Produce a result from the first mapper whose name equals (case ignored) the value associated to the given key, then store it into an optional.T
loadFromKeyOrDefault(java.lang.String key, ReadableMap config)
Produce a result from the first mapper whose name equals (case ignored) the value associated to the given keyjava.util.Optional<T>
loadFromValue(java.lang.String value, ReadableMap config)
Produce a result from the first mapper whose name equals (case ignored) the given value, then store it into an optionaljava.util.Collection<T>
loadList(java.lang.String key, ReadableMap config)
Produce a list of results from the list of values associated to the given key.
-
-
-
Constructor Detail
-
ConfigMatcher
public ConfigMatcher(java.util.Collection<ConfigMapper<T>> mappers, ConfigMapper<T> defaultMapper)
Construct a ConfigMatcher with a default mapper, used in caseloadFromKeyOrDefault(String, ReadableMap)
fails to find any mapper.- Parameters:
mappers
- the collection of mappers used for matchingsdefaultMapper
- a default value when matching fails (may be null)
-
-
Method Detail
-
loadFromKey
public java.util.Optional<T> loadFromKey(java.lang.String key, ReadableMap config)
Produce a result from the first mapper whose name equals (case ignored) the value associated to the given key, then store it into an optional. If no mapper matches the given key, an empty optional is returned- Parameters:
key
- the full path to the keyconfig
- the config loaded from the TOML file- Returns:
- an optional containing the result created, if present
-
loadFromValue
public java.util.Optional<T> loadFromValue(java.lang.String value, ReadableMap config)
Produce a result from the first mapper whose name equals (case ignored) the given value, then store it into an optional- Parameters:
value
- the given valueconfig
- the config loaded from the TOML file- Returns:
- an optional containing the result created, if present
-
loadList
public java.util.Collection<T> loadList(java.lang.String key, ReadableMap config)
Produce a list of results from the list of values associated to the given key. The algorithm tries to match every value found to a mapper stored, and adds the result created if it succeeds to do so.- Parameters:
key
- the full path to the key corresponding to a list of string valuesconfig
- the config loaded from the TOML file- Returns:
- a potentially empty collection containing all the results successfully computed
-
loadFromKeyOrDefault
public T loadFromKeyOrDefault(java.lang.String key, ReadableMap config)
Produce a result from the first mapper whose name equals (case ignored) the value associated to the given key- Parameters:
key
- the full path to the keyconfig
- the config loaded from the TOML file- Returns:
- the result created if present, otherwise the default value defined when constructing the object
-
-