Class ConfigurationBuilder<T>
- java.lang.Object
-
- net.obvj.confectory.ConfigurationBuilder<T>
-
- Type Parameters:
T- the target configuration type
- All Implemented Interfaces:
ConfigurationMetadataRetriever<T>
public class ConfigurationBuilder<T> extends Object implements ConfigurationMetadataRetriever<T>
A mutable object that supports the creation of immutableConfigurationobjects.For example:
Configuration<Properties> config = new ConfigurationBuilder<Properties>().source(new ClasspathFileSource<>("my.properties")).mapper(new PropertiesMapper()).namespace("default").precedence(10).lazy().build();- Since:
- 0.1.0
- Author:
- oswaldo.bapvic.jr (Oswaldo Junior)
- See Also:
Configuration
-
-
Constructor Summary
Constructors Constructor Description ConfigurationBuilder()Creates a new, emptyConfigurationBuilder.ConfigurationBuilder(Configuration<T> sourceConfiguration)Creates a newConfigurationBuilderfilled with the attributes of an existing baseConfiguration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ConfigurationBuilder<T>bean(T bean)[Optional] Defines a preset bean for the newConfiguration.Configuration<T>build()Builds the targetConfiguration.ConfigurationBuilder<T>eager()Marks the newConfigurationas eager.TgetBean()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.booleanisLazy()Returns a flag indicating whether thisConfigurationis lazy, i.e., not loaded until needed.booleanisOptional()Returns a flag indicating whether thisConfigurationis optional.ConfigurationBuilder<T>lazy()Marks the newConfigurationas lazy.ConfigurationBuilder<T>mapper(Mapper<T> mapper)Defines theMapperof the newConfiguration.ConfigurationBuilder<T>namespace(String namespace)Declares a namespace to be assigned to the newConfigurationobject.ConfigurationBuilder<T>optional()Marks the newConfigurationas optional.ConfigurationBuilder<T>precedence(int precedence)Defines the level of precedence of the newConfigurationcompared to similar objects in the same namespace.ConfigurationBuilder<T>required()Marks the newConfigurationas required (default).ConfigurationBuilder<T>source(String path)Defines aDynamicSourcewith the specified path for the newConfiguration.ConfigurationBuilder<T>source(Source<T> source)Defines theSourceof the newConfiguration.
-
-
-
Constructor Detail
-
ConfigurationBuilder
public ConfigurationBuilder()
Creates a new, emptyConfigurationBuilder.
-
ConfigurationBuilder
public ConfigurationBuilder(Configuration<T> sourceConfiguration)
Creates a newConfigurationBuilderfilled with the attributes of an existing baseConfiguration.- Parameters:
sourceConfiguration- a presetConfigurationobject whose attributes are to be copied;nullis allowed
-
-
Method Detail
-
namespace
public ConfigurationBuilder<T> namespace(String namespace)
Declares a namespace to be assigned to the newConfigurationobject.Note: The namespace is optional, but usually recommended to organize the scope of the target object and to prevent key collisions.
- Parameters:
namespace- the namespace to set;nullis allowed- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls
-
precedence
public ConfigurationBuilder<T> precedence(int precedence)
Defines the level of precedence of the newConfigurationcompared to similar objects in the same namespace.In a common configuration container, the object with the highest precedence level may be selected first in the occurrence of a key collision in the same namespace.
Note: This precedence value is optional and the default value is
0(zero).- Parameters:
precedence- an integer number representing the order of importance given to the target configuration- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls
-
source
public ConfigurationBuilder<T> source(Source<T> source)
Defines theSourceof the newConfiguration.- Parameters:
source- theSourceto be set; not null- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls
-
source
public ConfigurationBuilder<T> source(String path)
Defines aDynamicSourcewith the specified path for the newConfiguration.- Parameters:
path- the path to be set; not null- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls
-
mapper
public ConfigurationBuilder<T> mapper(Mapper<T> mapper)
Defines theMapperof the newConfiguration.- Parameters:
mapper- theMapperto be set; not null- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls
-
optional
public ConfigurationBuilder<T> optional()
Marks the newConfigurationas optional.The configuration source will be loaded quietly, i.e., not throwing an exception if the source is not found or not loaded successfully.
- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls - See Also:
required()
-
required
public ConfigurationBuilder<T> required()
Marks the newConfigurationas required (default).- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls - See Also:
optional()
-
lazy
public ConfigurationBuilder<T> lazy()
Marks the newConfigurationas lazy.The configuration source will not be loaded until needed.
- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls - Since:
- 0.4.0
- See Also:
eager()
-
eager
public ConfigurationBuilder<T> eager()
Marks the newConfigurationas eager.The configuration source will loaded directly during
build()time (default).- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls - Since:
- 0.4.0
- See Also:
lazy()
-
bean
public ConfigurationBuilder<T> bean(T bean)
[Optional] Defines a preset bean for the newConfiguration.- Parameters:
bean- the preset bean to be set- Returns:
- a reference to this same
ConfigurationBuilderfor chained calls - Since:
- 2.1.0
-
build
public Configuration<T> build()
Builds the targetConfiguration.- Returns:
- a new
Configurationobject - Throws:
NullPointerException- if either theSourceorMapperconfiguration parameters are missingConfigurationSourceException- in the event of a failure loading the configuration source
-
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()
-
-