Class Configuration<T>
- java.lang.Object
-
- net.obvj.confectory.Configuration<T>
-
- Type Parameters:
T- the target configuration type
- All Implemented Interfaces:
ConfigurationDataRetriever<T>,ConfigurationMetadataRetriever<T>
public final class Configuration<T> extends Object implements ConfigurationDataRetriever<T>, ConfigurationMetadataRetriever<T>
An object that contains configuration data from a specific source, as well as related metadata.A
Configurationmay also be defined as a combination of aSourceand aMapper, producing either a properties list, a JSON object, or a user-defined bean.Each
Configurationmay be assigned a namespace and precedence number, which determines an order of importance. So, once stored in a common configuration container, the object with the highest precedence value will be chosen first, taking precedence over the other ones in the same namespace.A
Configurationobject is eager by default, that is, the resource will be loaded directly duringbuild()time. Optionally, aConfigurationmay be created with the lazy flag, indicating that the resource shall not be loaded until really needed.A
Configurationmay also be marked as optional, indicating that the configuration shall be loaded quietly, that is, an "empty"Configurationobject will be instantiated even if the resource cannot be loaded (not default behavior).IMPORTANT: Use a
ConfigurationBuilderto create aConfigurationobject. A builder instance can be retrieved by calling the static methodbuilder(). For example:Configuration<Properties> config = Configuration.<Properties>builder().source(new ClasspathFileSource<>("my.properties")).mapper(new PropertiesMapper()).namespace("default").precedence(10).build();- Since:
- 0.1.0
- Author:
- oswaldo.bapvic.jr (Oswaldo Junior)
- See Also:
Source,Mapper,ConfigurationBuilder
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> ConfigurationBuilder<T>builder()Creates a new configuration builder.booleanequals(Object other)Indicates whether some other object is "equal to" this one.Objectget(String key)Returns the object associated with the specifiedkey.StringgetAsString()Returns a string representation of the bean used by this data retriever, typically for manual handling and/or troubleshooting purposes.TgetBean()Returns the configuration bean used by this data retriever, typically for manual handling and/or troubleshooting purposes.BooleangetBoolean(String key)Returns theBooleanobject associated with the specifiedkey.DoublegetDouble(String key)Returns theDoubleobject associated with the specifiedkey.IntegergetInteger(String key)Returns theIntegerobject associated with the specifiedkey.LonggetLong(String key)Returns theLongobject associated with the specifiedkey.BooleangetMandatoryBoolean(String key)Returns theBooleanobject associated with the specifiedkey, throwing an exception if not found.DoublegetMandatoryDouble(String key)Returns theDoubleobject associated with the specifiedkey, throwing an exception if not found.IntegergetMandatoryInteger(String key)Returns theIntegerobject associated with the specifiedkey, throwing an exception if not found.LonggetMandatoryLong(String key)Returns theLongobject associated with the specifiedkey, throwing an exception if not found.StringgetMandatoryString(String key)Returns theStringobject associated with the specifiedkey, throwing an exception if not found.Mapper<T>getMapper()Returns theMapperobject associated with thisConfiguration.StringgetNamespace()Returns the namespace defined for thisConfigurationobject.intgetPrecedence()Returns the precedence value defined for thisConfigurationobject.Source<T>getSource()Returns theSourceobject associated with thisConfiguration.StringgetString(String key)Returns theStringobject associated with the specifiedkey.inthashCode()booleanisLazy()Returns a flag indicating whether thisConfigurationis lazy, i.e., not loaded until needed.booleanisOptional()Returns a flag indicating whether thisConfigurationis optional.Configuration<T>merge(Configuration<T> other)Combines thisConfigurationwith another one, producing a newConfiguration.Configuration<T>merge(Configuration<T> other, net.obvj.jsonmerge.JsonMergeOption... mergeOptions)Combines thisConfigurationwith another one, producing a newConfiguration, using advanced options.StringtoString()
-
-
-
Method Detail
-
builder
public static <T> ConfigurationBuilder<T> builder()
Creates a new configuration builder.- Type Parameters:
T- the target configuration type- Returns:
- a new
ConfigurationBuilder
-
getNamespace
public String getNamespace()
Description copied from interface:ConfigurationMetadataRetrieverReturns the namespace defined for thisConfigurationobject.- Specified by:
getNamespacein interfaceConfigurationMetadataRetriever<T>- Returns:
- the namespace defined for this
Configuration
-
getPrecedence
public int getPrecedence()
Description copied from interface:ConfigurationMetadataRetrieverReturns the precedence value defined for thisConfigurationobject.In a common container, objects with higher-precedence may be selected first in case of key collision.
- Specified by:
getPrecedencein interfaceConfigurationMetadataRetriever<T>- Returns:
- an integer number representing the order of importance given to this
Configuration
-
getSource
public Source<T> getSource()
Description copied from interface:ConfigurationMetadataRetrieverReturns theSourceobject associated with thisConfiguration.- Specified by:
getSourcein interfaceConfigurationMetadataRetriever<T>- Returns:
- a
Sourceinstance
-
getMapper
public Mapper<T> getMapper()
Description copied from interface:ConfigurationMetadataRetrieverReturns theMapperobject associated with thisConfiguration.- Specified by:
getMapperin interfaceConfigurationMetadataRetriever<T>- Returns:
- a
Mapperinstance
-
isOptional
public boolean isOptional()
Description copied from interface:ConfigurationMetadataRetrieverReturns a flag indicating whether thisConfigurationis optional.An optional
Configurationobject may behave quietly in the event of a failure to load the data.- Specified by:
isOptionalin interfaceConfigurationMetadataRetriever<T>- Returns:
trueif thisConfigurationsetup is optional;false, otherwise
-
isLazy
public boolean isLazy()
Description copied from interface:ConfigurationMetadataRetrieverReturns a flag indicating whether thisConfigurationis lazy, i.e., not loaded until needed.- Specified by:
isLazyin interfaceConfigurationMetadataRetriever<T>- Returns:
trueif thisConfigurationis lazy;false, otherwise
-
getBean
public T getBean()
Description copied from interface:ConfigurationDataRetrieverReturns the configuration bean used by this data retriever, typically for manual handling and/or troubleshooting purposes.- Specified by:
getBeanin interfaceConfigurationDataRetriever<T>- Returns:
- the configuration bean (Note: it can be
nullif theConfigurationis marked as optional)
-
get
public Object get(String key)
Description copied from interface:ConfigurationDataRetrieverReturns the object associated with the specifiedkey.Notes:
- The actual return type may vary depending on the underlying implementation
- On a JSON implementation, the return will usually be an array
- Specified by:
getin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the object object associated with the specified
key. Depending on the actual implementation, the result may be eithernullor an empty array if the specified key is not found
-
getAsString
public String getAsString()
Description copied from interface:ConfigurationDataRetrieverReturns a string representation of the bean used by this data retriever, typically for manual handling and/or troubleshooting purposes.- Specified by:
getAsStringin interfaceConfigurationDataRetriever<T>- Returns:
- a string representation of the configuration bean (Note: it can be
nullif theConfigurationis marked as optional)
-
getBoolean
public Boolean getBoolean(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theBooleanobject associated with the specifiedkey.- Specified by:
getBooleanin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the
Booleanobject associated with the specifiedkey;nullif not found
-
getInteger
public Integer getInteger(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theIntegerobject associated with the specifiedkey.- Specified by:
getIntegerin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath- Returns:
- the
Integerobject associated with the specifiedkey;nullif not found
-
getLong
public Long getLong(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theLongobject associated with the specifiedkey.- Specified by:
getLongin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath- Returns:
- the
Longobject associated with the specifiedkey;nullif not found
-
getDouble
public Double getDouble(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theDoubleobject associated with the specifiedkey.- Specified by:
getDoublein interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath- Returns:
- the
Doubleobject associated with the specifiedkey;nullif not found
-
getString
public String getString(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theStringobject associated with the specifiedkey.- Specified by:
getStringin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the
Stringobject associated with the specifiedkey;nullif not found
-
equals
public boolean equals(Object other)
Indicates whether some other object is "equal to" this one.Two
Configurationobjects can be considered equal if both share the samenamespaceandSource.
-
getMandatoryBoolean
public Boolean getMandatoryBoolean(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theBooleanobject associated with the specifiedkey, throwing an exception if not found.- Specified by:
getMandatoryBooleanin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the
Booleanvalue associated with the specifiedkey; nevernull
-
getMandatoryInteger
public Integer getMandatoryInteger(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theIntegerobject associated with the specifiedkey, throwing an exception if not found.- Specified by:
getMandatoryIntegerin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the
Integerobject associated with the specifiedkey; nevernull
-
getMandatoryLong
public Long getMandatoryLong(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theLongobject associated with the specifiedkey, throwing an exception if not found.- Specified by:
getMandatoryLongin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the
Longobject associated with the specifiedkey; nevernull
-
getMandatoryDouble
public Double getMandatoryDouble(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theDoubleobject associated with the specifiedkey, throwing an exception if not found.- Specified by:
getMandatoryDoublein interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the
Doubleobject associated with the specifiedkey; nevernull
-
getMandatoryString
public String getMandatoryString(String key)
Description copied from interface:ConfigurationDataRetrieverReturns theStringobject associated with the specifiedkey, throwing an exception if not found.- Specified by:
getMandatoryStringin interfaceConfigurationDataRetriever<T>- Parameters:
key- the object key (some implementations may also accept a path expression, e.g:JSONPath)- Returns:
- the
Stringobject associated with the specifiedkey; nevernull
-
merge
public Configuration<T> merge(Configuration<T> other)
Combines thisConfigurationwith another one, producing a newConfiguration.The resulting
Configurationwill receive all the elements from both objects. In case of conflicting keys, the values at the highest-precedenceConfigurationwill be selected.The metadata of the highest-precedence
Configuration(namespace and precedence) will be applied to the newConfiguration.Note: The other
Configurationmust be of the same type as the current one.- Parameters:
other- theConfigurationto be merged with this one; not null- Returns:
- a new
Configurationresulting from the combination of this object and the specified one - Throws:
NullPointerException- if the otherConfigurationisnull- Since:
- 2.2.0
- See Also:
ConfigurationMerger
-
merge
public Configuration<T> merge(Configuration<T> other, net.obvj.jsonmerge.JsonMergeOption... mergeOptions)
Combines thisConfigurationwith another one, producing a newConfiguration, using advanced options.The resulting
Configurationwill receive all the elements from both objects. In case of conflicting keys, the values at the highest-precedenceConfigurationwill be selected.The metadata of the highest-precedence
Configuration(namespace and precedence) will be applied to the newConfiguration.Note: The other
Configurationmust be of the same type as the current one.- Parameters:
other- theConfigurationto be merged with this one; not nullmergeOptions- an array of options on how to merge the objects (optional)- Returns:
- a new
Configurationresulting from the combination of this object and the specified one - Throws:
NullPointerException- if the otherConfigurationisnull- Since:
- 2.2.0
- See Also:
ConfigurationMerger
-
-