Package cn.sliew.milky.dsl
Class AbstractConfiguredBuilder<O,B extends ConfigurableBuilder<O,B>>
- java.lang.Object
-
- cn.sliew.milky.dsl.AbstractBuilder<O>
-
- cn.sliew.milky.dsl.AbstractConfiguredBuilder<O,B>
-
- Type Parameters:
O- The object that this builder returnsB- The type of this builder (that is returned by the base class)
- All Implemented Interfaces:
Builder<O>,ConfigurableBuilder<O,B>
public abstract class AbstractConfiguredBuilder<O,B extends ConfigurableBuilder<O,B>> extends AbstractBuilder<O> implements ConfigurableBuilder<O,B>
A base
Builderthat allowsConfigurerto be applied to it. This makes modifying theBuildera strategy that can be customized and broken up into a number ofConfigurerobjects that have more specific goals than that of theBuilder.
-
-
Constructor Summary
Constructors Constructor Description AbstractConfiguredBuilder()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <C extends AbstractConfigurer<O,B>>
Capply(C configurer)protected voidbeforeConfigure()Invoked prior to invoking eachConfigurer.configure(Builder)method.protected voidbeforeInit()Invoked prior to invoking eachConfigurer.init(Builder)method.protected OdoBuild()Executes the build using theConfigurer's that have been applied using the following steps: InvokesbeforeInit()for any subclass to hook into InvokesConfigurer.init(Builder)for anyConfigurerthat was applied to this builder. InvokesbeforeConfigure()for any subclass to hook into InvokesperformBuild()which actually builds the Object<C extends Configurer<O,B>>
CgetConfigurer(Class<C> clazz)Gets theConfigurerby its class name ornullif not found.OgetOrBuild()Similar toAbstractBuilder.build()andAbstractBuilder.getObject()but checks the state to determine ifAbstractBuilder.build()needs to be called first.protected abstract OperformBuild()Subclasses must implement this method to build the object that is being returned.<C extends Configurer<O,B>>
CremoveConfigurer(Class<C> clazz)Removes and returns theConfigurerby its class name ornullif not found.-
Methods inherited from class cn.sliew.milky.dsl.AbstractBuilder
build, getObject
-
-
-
-
Method Detail
-
getOrBuild
public O getOrBuild()
Similar toAbstractBuilder.build()andAbstractBuilder.getObject()but checks the state to determine ifAbstractBuilder.build()needs to be called first.- Returns:
- the result of
AbstractBuilder.build()orAbstractBuilder.getObject(). If an error occurs while building, returns null.
-
doBuild
protected final O doBuild() throws Exception
Executes the build using theConfigurer's that have been applied using the following steps:- Invokes
beforeInit()for any subclass to hook into - Invokes
Configurer.init(Builder)for anyConfigurerthat was applied to this builder. - Invokes
beforeConfigure()for any subclass to hook into - Invokes
performBuild()which actually builds the Object
- Specified by:
doBuildin classAbstractBuilder<O>- Returns:
- the object that should be returned by
AbstractBuilder.build(). - Throws:
Exception- if an error occurs
- Invokes
-
beforeInit
protected void beforeInit() throws ExceptionInvoked prior to invoking eachConfigurer.init(Builder)method. Subclasses may override this method to hook into the lifecycle without using aConfigurer.- Throws:
Exception
-
beforeConfigure
protected void beforeConfigure() throws ExceptionInvoked prior to invoking eachConfigurer.configure(Builder)method. Subclasses may override this method to hook into the lifecycle without using aConfigurer.- Throws:
Exception
-
performBuild
protected abstract O performBuild() throws Exception
Subclasses must implement this method to build the object that is being returned.- Returns:
- the Object to be buit or null if the implementation allows it
- Throws:
Exception
-
apply
public <C extends AbstractConfigurer<O,B>> C apply(C configurer) throws Exception
- Parameters:
configurer-- Returns:
- the
AbstractConfigurerfor further customizations - Throws:
Exception
-
apply
public <C extends Configurer<O,B>> C apply(C configurer) throws Exception
Applies aConfigurerto thisBuilderoverriding anyConfigurerof the exact same class. Note that object hierarchies are not considered.- Parameters:
configurer-- Returns:
- the
AbstractConfigurerfor further customizations - Throws:
Exception
-
getConfigurer
public <C extends Configurer<O,B>> C getConfigurer(Class<C> clazz)
Gets theConfigurerby its class name ornullif not found. Note that object hierarchies are not considered.- Specified by:
getConfigurerin interfaceConfigurableBuilder<O,B extends ConfigurableBuilder<O,B>>- Parameters:
clazz-- Returns:
- the
Configurerfor further customizations
-
removeConfigurer
public <C extends Configurer<O,B>> C removeConfigurer(Class<C> clazz)
Removes and returns theConfigurerby its class name ornullif not found. Note that object hierarchies are not considered.- Specified by:
removeConfigurerin interfaceConfigurableBuilder<O,B extends ConfigurableBuilder<O,B>>- Parameters:
clazz-- Returns:
-
-