Class ConfigurationContainer
- java.lang.Object
-
- net.obvj.confectory.ConfigurationContainer
-
public class ConfigurationContainer extends Object
An object that holds multipleConfigurationobjects and retrieves configuration data seamlessly, by namespace and key.The
Configurationobjects are sorted by precedence (from highest to lowest). So, in case of key collision, the object with the highest precedence will be selected first. The container may still select other lower-precedenceConfigurationobjects if a key is not found in the highest-precedenceConfiguration.To create a new empty container, use the default constructor
new ConfigurationContainer(). To create a new container with presetConfigurationobjects, pass them as "var-args".Use the
add(Configuration)method at any time to register new objects inside the container.To retrieve
Configurationdata, use any of the getter methods, specifying a namespace and key.Single-argument getter methods can be used to retrieve data from
Configurationobjects that do not have a declared namespace. These methods may also retrieve data from allConfigurationobjects if the selected data-fetch strategy isLENIENT.Each container may have a custom
DataFetchStrategywhich may be specified at container construction time and modified using the setter method anytime. If not specified, the container uses the default choice configured viaConfectorySettings.IMPORTANT: This class works only with map-based
Configurationobjects (e.g.:Properties,JSONObject,JsonNode, etc.). In other words, only "container" objects which values can be accessed using either a key or path expression (e.g.JSONPath). POJO-basedConfigurationobjects are not supported inside a theConfigurationContainer.- Since:
- 0.1.0
- Author:
- oswaldo.bapvic.jr (Oswaldo Junior)
- See Also:
Configuration,DataFetchStrategy
-
-
Constructor Summary
Constructors Constructor Description ConfigurationContainer(Configuration<?>... configs)Builds a newConfigurationContainerwith an arbitrary number of presetConfigurationobjects to be registered.ConfigurationContainer(DataFetchStrategy dataFetchStrategy, Configuration<?>... configs)Builds a newConfigurationContainerwith a customDataFetchStrategyand an arbitrary number of presetConfigurationobjects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(Configuration<?> configuration)Adds the specifiedConfigurationto this container.voidaddAll(ConfigurationContainer source)Copies all of theConfigurationobjects from another container to this container.voidclear()Removes all of theConfigurationobjects from this container.BooleangetBoolean(String key)Returns theBooleanobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).BooleangetBoolean(String namespace, String key)Returns theBooleanobject associated with the specifiedkeyin the specifiednamespace.DataFetchStrategygetDataFetchStrategy()Returns theDataFetchStrategyassociated with this container.DoublegetDouble(String key)Returns theDoubleobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).DoublegetDouble(String namespace, String key)Returns theDoubleobject associated with the specifiedkeyin the specifiednamespace.IntegergetInteger(String key)Returns theIntegerobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).IntegergetInteger(String namespace, String key)Returns theIntegerobject associated with the specifiedkeyin the specifiednamespace.LonggetLong(String key)Returns theLongobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).LonggetLong(String namespace, String key)Returns theLongobject associated with the specifiedkeyin the specifiednamespace.Collection<String>getNamespaces()Returns all of the namespaces defined inside this container.StringgetString(String key)Returns theStringobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).StringgetString(String namespace, String key)Returns theStringobject associated with the specifiedkeyin the specifiednamespace.booleanisEmpty()Returnstrueif this container contains noConfigurationobjects.voidsetDataFetchStrategy(DataFetchStrategy strategy)Defines a customDataFetchStrategyfor this container.longsize()Returns the number ofConfigurationobjects in this container.longsize(String namespace)Returns the number ofConfigurationobjects associated with the specifiednamespacein this container.
-
-
-
Constructor Detail
-
ConfigurationContainer
public ConfigurationContainer(Configuration<?>... configs)
Builds a newConfigurationContainerwith an arbitrary number of presetConfigurationobjects to be registered.- Parameters:
configs- an arbitrary number ofConfigurationobjects (zero or more) to be registered at constructor time
-
ConfigurationContainer
public ConfigurationContainer(DataFetchStrategy dataFetchStrategy, Configuration<?>... configs)
Builds a newConfigurationContainerwith a customDataFetchStrategyand an arbitrary number of presetConfigurationobjects.- Parameters:
dataFetchStrategy- an optionalDataFetchStrategyto be applied by this container;nullis allowed and indicates that the default strategy defined inConfectory.settings()will be appliedconfigs- an arbitrary number ofConfigurationobjects (zero or more) to be registered at constructor time- See Also:
ConfectorySettings
-
-
Method Detail
-
getDataFetchStrategy
public DataFetchStrategy getDataFetchStrategy()
Returns theDataFetchStrategyassociated with this container.- Returns:
- a
DataFetchStrategy
-
setDataFetchStrategy
public void setDataFetchStrategy(DataFetchStrategy strategy)
Defines a customDataFetchStrategyfor this container.- Parameters:
strategy- theDataFetchStrategyto set; not null- Throws:
NullPointerException- if the specifiedstrategyis null
-
add
public void add(Configuration<?> configuration)
Adds the specifiedConfigurationto this container.- Parameters:
configuration- theConfigurationto be added to the container
-
addAll
public void addAll(ConfigurationContainer source)
Copies all of theConfigurationobjects from another container to this container.- Parameters:
source- the source container whichConfigurationobjects are to be stored in this container;nullis allowed
-
clear
public void clear()
Removes all of theConfigurationobjects from this container.
-
getBoolean
public Boolean getBoolean(String key)
Returns theBooleanobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).- Parameters:
key- the object key (or path)- Returns:
- the
Booleanobject associated with the specifiedkey;nullif not found - See Also:
DataFetchStrategy
-
getBoolean
public Boolean getBoolean(String namespace, String key)
Returns theBooleanobject associated with the specifiedkeyin the specifiednamespace.- Parameters:
namespace- the namespace to be usedkey- the object key (or path)- Returns:
- the
Booleanobject associated with the specifiedkey;nullif not found
-
getInteger
public Integer getInteger(String key)
Returns theIntegerobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).- Parameters:
key- the object key (or path)- Returns:
- the
Integerobject associated with the specifiedkey;nullif not found - See Also:
DataFetchStrategy
-
getInteger
public Integer getInteger(String namespace, String key)
Returns theIntegerobject associated with the specifiedkeyin the specifiednamespace.- Parameters:
namespace- the namespace to be usedkey- the object key (or path)- Returns:
- the
Integerobject associated with the specifiedkey;nullif not found
-
getLong
public Long getLong(String key)
Returns theLongobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).- Parameters:
key- the object key (or path)- Returns:
- the
Longobject associated with the specifiedkey;nullif not found - See Also:
DataFetchStrategy
-
getLong
public Long getLong(String namespace, String key)
Returns theLongobject associated with the specifiedkeyin the specifiednamespace.- Parameters:
namespace- the namespace to be usedkey- the object key (or path)- Returns:
- the
Longobject associated with the specifiedkey;nullif not found
-
getDouble
public Double getDouble(String key)
Returns theDoubleobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).- Parameters:
key- the object key (or path)- Returns:
- the
Doublevalue associated with the specifiedkey;nullif not found - See Also:
DataFetchStrategy
-
getDouble
public Double getDouble(String namespace, String key)
Returns theDoubleobject associated with the specifiedkeyin the specifiednamespace.- Parameters:
namespace- the namespace to be usedkey- the object key (or path)- Returns:
- the
Doubleobject associated with the specifiedkey;nullif not found
-
getString
public String getString(String key)
Returns theStringobject associated with the specifiedkeyin the default namespace (or in all namespaces depending on theDataFetchStrategyin scope).- Parameters:
key- the object key (or path)- Returns:
- the
Stringobject associated with the specifiedkey;nullif not found - See Also:
DataFetchStrategy
-
getString
public String getString(String namespace, String key)
Returns theStringobject associated with the specifiedkeyin the specifiednamespace.- Parameters:
namespace- the namespace to be usedkey- the object key (or path)- Returns:
- the
Stringobject associated with the specifiedkey;nullif not found
-
size
public long size()
Returns the number ofConfigurationobjects in this container.- Returns:
- the number of
Configurationobjects in this container - Since:
- 1.0.0
-
size
public long size(String namespace)
Returns the number ofConfigurationobjects associated with the specifiednamespacein this container.- Parameters:
namespace- the namespace to be tested- Returns:
- the number of
Configurationobjects associated with the specifiednamespace
-
isEmpty
public boolean isEmpty()
Returnstrueif this container contains noConfigurationobjects.- Returns:
trueif this container contains noConfigurationobjects- Since:
- 1.0.0
-
getNamespaces
public Collection<String> getNamespaces()
Returns all of the namespaces defined inside this container.- Returns:
- a set of namespaces
-
-