Class BaseConfiguration<T>
- java.lang.Object
-
- io.confound.config.AbstractConfiguration
-
- io.confound.config.BaseConfiguration<T>
-
- Type Parameters:
T
- The type of values used in the underlying storage.
- All Implemented Interfaces:
Configuration
- Direct Known Subclasses:
AbstractObjectConfiguration
,AbstractStringConfiguration
public abstract class BaseConfiguration<T> extends AbstractConfiguration
Abstract configuration implementation providing common base functionality.Concrete implementations must override
findConfigurationValueImpl(String)
, and may also overridehasConfigurationValueImpl(String)
for added efficiency.This class provides a facility
normalizeKey(String)
for modifying the requested key if necessary before ultimate retrieval viafindConfigurationValueImpl(String)
. Most implementations will not need this facility, and will use the default implementation which uses the requested key unmodified. In any case, the implementation must use the original key, not the normalized key, when delegating to the parent configuration, if any.- Author:
- Garret Wilson
-
-
Field Summary
-
Fields inherited from interface io.confound.config.Configuration
KEY_SEGMENT_SEPARATOR
-
-
Constructor Summary
Constructors Constructor Description BaseConfiguration()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected <C> java.util.Optional<C>
convertValue(java.util.Optional<T> value, java.lang.Class<C> convertClass)
Converts a configuration value from its actual type in the underlying storage to the requested type.protected java.util.Optional<T>
findConfigurationValue(java.lang.String key)
Tries to retrieves a general configuration value from the underlying storage.protected abstract java.util.Optional<T>
findConfigurationValueImpl(java.lang.String key)
Implementation to retrieves a general configuration value that may not be present from the underlying storage.<O> java.util.Optional<O>
findObject(java.lang.String key, java.lang.Class<O> type)
Retrieves a general configuration object that may not be present as the requested type, converting it as necessary.boolean
hasConfigurationValue(java.lang.String key)
Determines whether a configuration of some type exists for the given configuration key.protected boolean
hasConfigurationValueImpl(java.lang.String key)
Determines whether a configuration value is present in the underlying storage.protected java.lang.String
normalizeKey(java.lang.String key)
Normalizes a requested key if required by this implementation.-
Methods inherited from class io.confound.config.AbstractConfiguration
or
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.confound.config.Configuration
createMissingConfigurationKeyException, findBoolean, findDouble, findInt, findLong, findObject, findPath, findString, findUri, getBoolean, getDouble, getInt, getLong, getObject, getObject, getPath, getString, getUri, requireConfiguration, resolvePath, subConfiguration, superConfiguration, withFallback
-
-
-
-
Method Detail
-
normalizeKey
protected java.lang.String normalizeKey(@Nonnull java.lang.String key)
Normalizes a requested key if required by this implementation.- Implementation Specification:
- The default implementation returns the key unmodified after checking for
null
. - Parameters:
key
- The configuration key.- Returns:
- The requested configuration key, modified as needed for lookup in this implementation.
-
convertValue
protected <C> java.util.Optional<C> convertValue(@Nonnull java.util.Optional<T> value, @Nonnull java.lang.Class<C> convertClass) throws ConfigurationException
Converts a configuration value from its actual type in the underlying storage to the requested type.- Implementation Specification:
- This implementation merely checked to see if the value can be cast to the requested type; otherwise, an exception is thrown.
- Type Parameters:
C
- The requested conversion type.- Parameters:
value
- The value to convert.convertClass
- The class indicating the requested conversion type.- Returns:
- The value converted to the requested type.
- Throws:
ConfigurationException
- if the value is present and cannot be converted to the requested type.
-
hasConfigurationValue
public boolean hasConfigurationValue(@Nonnull java.lang.String key) throws ConfigurationException
Determines whether a configuration of some type exists for the given configuration key.- Implementation Specification:
- This implementation normalizes the key and delegates to
hasConfigurationValueImpl(String)
. - Implementation Note:
- Most subclasses should not override this method, and instead override
hasConfigurationValueImpl(String)
. - Parameters:
key
- The configuration key.- Returns:
true
if a value of any type could be retrieved from this configuration using the given key.- Throws:
java.lang.NullPointerException
- if the given key isnull
.java.lang.SecurityException
- If a security manager exists and it doesn't allow access to the specified configuration.ConfigurationException
- if there is a configuration value stored in an invalid format.
-
findObject
public <O> java.util.Optional<O> findObject(java.lang.String key, java.lang.Class<O> type) throws ConfigurationException
Retrieves a general configuration object that may not be present as the requested type, converting it as necessary. If the object is present but cannot be converted, aConfigurationException
will be thrown.- Implementation Specification:
- This implementation converts the value using
convertValue(Optional, Class)
. - Type Parameters:
O
- The type of configuration object expected.- Parameters:
key
- The configuration key.type
- The type of object requested.- Returns:
- The optional configuration value associated with the given key.
- Throws:
ConfigurationException
- if there is a configuration value stored in an invalid format.
-
hasConfigurationValueImpl
protected boolean hasConfigurationValueImpl(@Nonnull java.lang.String key) throws ConfigurationException
Determines whether a configuration value is present in the underlying storage.The given configuration key is assumed to already be normalized, and should not be modified.
This method must not fall back to parent configuration; only local values must be returned.
- Implementation Specification:
- The default implementation delegates to
findConfigurationValueImpl(String)
. - Parameters:
key
- The normalized configuration key.- Returns:
true
if a value of any type could be retrieved from this configuration using the given key.- Throws:
java.lang.NullPointerException
- if the given key isnull
.java.lang.SecurityException
- If a security manager exists and it doesn't allow access to the specified configuration.ConfigurationException
- if there is a configuration value stored in an invalid format.
-
findConfigurationValue
protected java.util.Optional<T> findConfigurationValue(@Nonnull java.lang.String key) throws ConfigurationException
Tries to retrieves a general configuration value from the underlying storage. The key need not be normalized; it will be normalized as necessary.This method must not fall back to parent configuration; only local values must be returned.
- Implementation Note:
- This is an internal API call that should be used by child classes to funnel requests to the underlying storage. Normally child classes will not
override this method, but override
findConfigurationValueImpl(String)
instead. - Parameters:
key
- The configuration key, which may not be normalized.- Returns:
- The optional configuration value associated with the given key.
- Throws:
java.lang.NullPointerException
- if the given key isnull
.java.lang.SecurityException
- If a security manager exists and it doesn't allow access to the specified configuration.ConfigurationException
- if there is a configuration value stored in an invalid format.- See Also:
normalizeKey(String)
-
findConfigurationValueImpl
protected abstract java.util.Optional<T> findConfigurationValueImpl(@Nonnull java.lang.String key) throws ConfigurationException
Implementation to retrieves a general configuration value that may not be present from the underlying storage.The given configuration key is assumed to already be normalized, and should not be modified.
This method must not fall back to parent configuration; only local values must be returned.
- Parameters:
key
- The normalized configuration key.- Returns:
- The optional configuration value associated with the given key.
- Throws:
java.lang.NullPointerException
- if the given key isnull
.java.lang.SecurityException
- If a security manager exists and it doesn't allow access to the specified configuration.ConfigurationException
- if there is a configuration value stored in an invalid format.
-
-