Class Parameters
- java.lang.Object
-
- org.apache.commons.configuration2.builder.fluent.Parameters
-
public final class Parameters extends java.lang.Object
A convenience class for creating parameter objects for initializing configuration builder objects.For setting initialization properties of new configuration objects, a number of specialized parameter classes exists. These classes use inheritance to organize the properties they support in a logic way. For instance, parameters for file-based configurations also support the basic properties common to all configuration implementations, parameters for XML configurations also include file-based and basic properties, etc.
When constructing a configuration builder, an easy-to-use fluent API is desired to define specific properties for the configuration to be created. However, the inheritance structure of the parameter classes makes it surprisingly difficult to provide such an API. This class comes to rescue by defining a set of methods for the creation of interface-based parameter objects offering a truly fluent API. The methods provided can be called directly when setting up a configuration builder as shown in the following example code fragment:
Parameters params = new Parameters(); configurationBuilder.configure(params.fileBased() .setThrowExceptionOnMissing(true) .setEncoding("UTF-8") .setListDelimiter('#') .setFileName("test.xml"));
Using this class it is not only possible to create new parameters objects but also to initialize the newly created objects with default values. This is via the associated
DefaultParametersManager
object. Such an object can be passed to the constructor, or a new (uninitialized) instance is created. There are convenience methods for interacting with the associatedDefaultParametersManager
, namely to register or removeDefaultParametersHandler
objects. On all newly created parameters objects the handlers registered at the associatedDefaultParametersHandler
are automatically applied.Implementation note: This class is thread-safe.
- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description Parameters()
Creates a new instance ofParameters
.Parameters(DefaultParametersManager manager)
Creates a new instance ofParameters
and initializes it with the givenDefaultParametersManager
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BasicBuilderParameters
basic()
Creates a new instance of a parameters object for basic configuration properties.CombinedBuilderParameters
combined()
Creates a new instance of a parameters object for combined configuration builder properties.DatabaseBuilderParameters
database()
Creates a new instance of a parameters object for database configurations.FileBasedBuilderParameters
fileBased()
Creates a new instance of a parameters object for file-based configuration properties.DefaultParametersManager
getDefaultParametersManager()
Gets theDefaultParametersManager
associated with this object.HierarchicalBuilderParameters
hierarchical()
Creates a new instance of a parameters object for hierarchical configurations.INIBuilderParameters
ini()
Creates a new instance of a parameters object for INI configurations.JndiBuilderParameters
jndi()
Creates a new instance of a parameters object for JNDI configurations.MultiFileBuilderParameters
multiFile()
Creates a new instance of a parameters object for a builder for multiple file-based configurations.PropertiesBuilderParameters
properties()
Creates a new instance of a parameters object for properties configurations.<T> void
registerDefaultsHandler(java.lang.Class<T> paramsClass, DefaultParametersHandler<? super T> handler)
Registers the specifiedDefaultParametersHandler
object for the given parameters class.<T> void
registerDefaultsHandler(java.lang.Class<T> paramsClass, DefaultParametersHandler<? super T> handler, java.lang.Class<?> startClass)
Registers the specifiedDefaultParametersHandler
object for the given parameters class and start class in the inheritance hierarchy.XMLBuilderParameters
xml()
Creates a new instance of a parameters object for XML configurations.
-
-
-
Constructor Detail
-
Parameters
public Parameters()
Creates a new instance ofParameters
. A new, uninitializedDefaultParametersManager
is created.
-
Parameters
public Parameters(DefaultParametersManager manager)
Creates a new instance ofParameters
and initializes it with the givenDefaultParametersManager
. BecauseDefaultParametersManager
is thread-safe, it makes sense to share a single instance between multipleParameters
objects; that way the same initialization is performed on newly created parameters objects.- Parameters:
manager
- theDefaultParametersHandler
(may be null, then a new default instance is created)
-
-
Method Detail
-
getDefaultParametersManager
public DefaultParametersManager getDefaultParametersManager()
Gets theDefaultParametersManager
associated with this object.- Returns:
- the
DefaultParametersManager
-
registerDefaultsHandler
public <T> void registerDefaultsHandler(java.lang.Class<T> paramsClass, DefaultParametersHandler<? super T> handler)
Registers the specifiedDefaultParametersHandler
object for the given parameters class. This is a convenience method which just delegates to the associatedDefaultParametersManager
.- Type Parameters:
T
- the type of the parameters supported by this handler- Parameters:
paramsClass
- the parameters class supported by this handler (must not be null)handler
- theDefaultParametersHandler
to be registered (must not be null)- Throws:
java.lang.IllegalArgumentException
- if a required parameter is missing- See Also:
DefaultParametersManager
-
registerDefaultsHandler
public <T> void registerDefaultsHandler(java.lang.Class<T> paramsClass, DefaultParametersHandler<? super T> handler, java.lang.Class<?> startClass)
Registers the specifiedDefaultParametersHandler
object for the given parameters class and start class in the inheritance hierarchy. This is a convenience method which just delegates to the associatedDefaultParametersManager
.- Type Parameters:
T
- the type of the parameters supported by this handler- Parameters:
paramsClass
- the parameters class supported by this handler (must not be null)handler
- theDefaultParametersHandler
to be registered (must not be null)startClass
- an optional start class in the hierarchy of parameter objects for which this handler should be applied- Throws:
java.lang.IllegalArgumentException
- if a required parameter is missing
-
basic
public BasicBuilderParameters basic()
Creates a new instance of a parameters object for basic configuration properties.- Returns:
- the new parameters object
-
fileBased
public FileBasedBuilderParameters fileBased()
Creates a new instance of a parameters object for file-based configuration properties.- Returns:
- the new parameters object
-
combined
public CombinedBuilderParameters combined()
Creates a new instance of a parameters object for combined configuration builder properties.- Returns:
- the new parameters object
-
jndi
public JndiBuilderParameters jndi()
Creates a new instance of a parameters object for JNDI configurations.- Returns:
- the new parameters object
-
hierarchical
public HierarchicalBuilderParameters hierarchical()
Creates a new instance of a parameters object for hierarchical configurations.- Returns:
- the new parameters object
-
xml
public XMLBuilderParameters xml()
Creates a new instance of a parameters object for XML configurations.- Returns:
- the new parameters object
-
properties
public PropertiesBuilderParameters properties()
Creates a new instance of a parameters object for properties configurations.- Returns:
- the new parameters object
-
multiFile
public MultiFileBuilderParameters multiFile()
Creates a new instance of a parameters object for a builder for multiple file-based configurations.- Returns:
- the new parameters object
-
database
public DatabaseBuilderParameters database()
Creates a new instance of a parameters object for database configurations.- Returns:
- the new parameters object
-
ini
public INIBuilderParameters ini()
Creates a new instance of a parameters object for INI configurations.- Returns:
- the new parameters object
-
-