public abstract class Config extends java.lang.Object implements Configurable
A mandatory configuration is a set of parameters that are absolutely necessary for the configuration. There may be more such sets, and they may intersect.
In case of a config with ancestors, like RootConfig → ChildConfig → CurrentConfig, since a setter returns
an instance of its class (which may not be the original calling class), chained setters should be called for
convenience in the order:
currentConfigInstance.setParamInCurrentConfig().setOtherParamInCurrentConfig().setParamInChildConfig().setParamInRootConfig()
Obviously, a RootConfig instance will be returned in the end, but that can be casted back to CurrentConfig.
Rules
this
.
locked()
(by using the lock()
function). It
can never be unlocked. Setters may check that the configuration is locked by calling locked()
, that will
automatically throw an exception if necessary.
Modifier and Type | Class and Description |
---|---|
class |
Config.ConfigLockedException
Exception that is thrown when a 'locked' setter is called on a locked
Config instance. |
Modifier and Type | Field and Description |
---|---|
private boolean |
locked
Retains the 'locked' state of the Config instance.
|
Constructor and Description |
---|
Config()
Default constructor, that calls the
makeDefaults() method. |
Modifier and Type | Method and Description |
---|---|
Config |
build()
This method should always be an alias of
lock() . |
void |
ensureLocked()
This method should check the locking status and call
lock() only once in the lifetime of the
instance. |
Config |
lock()
'Locks' the configuration, meaning that construction time is over.
|
void |
locked()
Should be called at the entry point of methods that should not be called after the instance has been 'locked'.
|
protected void |
lockedEx()
|
Config |
makeDefaults()
Initializes all optional parameters to their default value.
|
public Config()
makeDefaults()
method.public Config makeDefaults()
Configurable
From Config
guidelines:
makeDefaults
in interface Configurable
public Config lock()
Configurable
in case any construction needs to be made, it should be done in this method, after calling
super.lock()
.
lock
in interface Configurable
public final Config build()
Configurable
lock()
. The difference is only semantic:
build()
should be associated with actually building the configured instance, and lock()
should be associated with locking the configuration / values of configured parameters.build
in interface Configurable
public void ensureLocked()
Configurable
lock()
only once in the lifetime of the
instance.
This can be called by various methods that need to ensure the locking has been performed, also resulting in a
call to lock()
the first time this method is called.
ensureLocked
in interface Configurable
public void locked() throws Config.ConfigLockedException
Configurable
In case such a call is made, the method should throw the specified exception.
locked
in interface Configurable
Config.ConfigLockedException
protected final void lockedEx() throws Config.ConfigLockedException
locked()
that cannot be overridden, meaning that a call to this method is sure to do what the
implementation in Config
specifies, so that extending classes cannot avoid the exception by overriding
locked()
.Config.ConfigLockedException