public class Config extends Object implements Serializable
| Constructor and Description |
|---|
Config(String file)
Constructor that loads a specific file and loads the settings in that file.
|
Config(String file,
String defaultConfigurationFile)
Constructor that loads specific file augmented with defaults and the name of the default configuration file.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears out the configuration.
|
Object |
get(String key)
Looks up the key and returns the matching value or null if not.
|
Map<String,Object> |
getAll(Optional<Set<String>> keys)
Gets all mappings for a set of keys.
|
Map<String,Object> |
getAllBut(Optional<Set<String>> keys)
Gets all mappings other than a set of keys.
|
Map<String,Object> |
getAllWithPrefix(Optional<Set<String>> keys,
String prefix,
boolean stripPrefix)
Get mappings for all keys with the specified prefix.
|
<T> T |
getAs(String key,
Class<T> clazz)
Get a value from the config as a particular type.
|
Object |
getOrDefault(String key,
Object defaultValue)
If there was a mapping for the key, the mapping is returned if not null, otherwise returns the defaultValue.
|
<T> T |
getOrDefaultAs(String key,
T defaultValue,
Class<T> clazz)
Get a value from the config as a particular type or default to a provided value.
|
<T> T |
getRequiredConfigAs(String key,
Class<T> clazz)
Get a value from the config as a particular type or throw an exception with a message if not found.
|
void |
merge(Config other)
Merges another Config into this one.
|
protected Map<String,Object> |
readYAML(String yamlFile)
Reads a YAML file containing mappings and returns them as a
Map. |
void |
set(String key,
Object value)
Adds a key/value pair to the configuration.
|
String |
toString() |
public static final String DELIMITER
public Config(String file)
file - YAML file to load.public Object get(String key)
key - The key to usepublic Object getOrDefault(String key, Object defaultValue)
key - The key to get.defaultValue - The defaultValue to return if there is no mapping or the mapping is null.public <T> T getAs(String key, Class<T> clazz)
T - The type of the config.key - The name of the config.clazz - The Class of the type.ClassCastException - if the value of the config could not be casted to the type.public <T> T getOrDefaultAs(String key, T defaultValue, Class<T> clazz)
T - The type of the config.key - The name of the config.defaultValue - A default of the same type to use if the config was not found.clazz - The Class of the type.ClassCastException - if the value of the config or default could not be casted to the type.public <T> T getRequiredConfigAs(String key, Class<T> clazz)
T - The type of the config.key - The name of the config.clazz - The Class of the type.ClassCastException - if the value of the config could not be casted to the type.NullPointerException - if the config was not found.public Map<String,Object> getAll(Optional<Set<String>> keys)
public Map<String,Object> getAllWithPrefix(Optional<Set<String>> keys, String prefix, boolean stripPrefix)
public Map<String,Object> getAllBut(Optional<Set<String>> keys)
public void set(String key, Object value)
key - to usevalue - to usepublic void merge(Config other)
other - The other Config to merge into this one.public void clear()
protected Map<String,Object> readYAML(String yamlFile)
Map.yamlFile - The String name of the YAML resource file in classpath or the path to a YAML file containing the mappings.Map of String names to Objects of the mappings in the YAML file.Copyright © 2021. All rights reserved.