Package org.kigalisim.engine.support
Class ValidatedBuilder<T>
java.lang.Object
org.kigalisim.engine.support.ValidatedBuilder<T>
- Type Parameters:
T- The type of object this builder builds
- Direct Known Subclasses:
ChangeExecutorConfigBuilder,DemandAnalysisBuilder,EngineResultBuilder,RecalcKitBuilder,SalesStreamDistributionBuilder,ServicingOffsetBuilder,SimulationStateUpdateBuilder,StreamUpdateBuilder
Base class for builders that validate required fields before constructing their result.
Subclasses implement validate() to check fields required by every build path, and
buildInternal() to construct the result. Fields required only by a specific branch of
construction logic can call requireField(Object, String) directly from within that
branch instead of (or in addition to) validate(), so validation can be deferred until
it is known that branch will actually run.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedValidatedBuilder(String builtTypeName) Create a new ValidatedBuilder. -
Method Summary
Modifier and TypeMethodDescriptionfinal Tbuild()Validate required fields and build the result.protected abstract TConstruct the result aftervalidate()has passed.protected voidrequireField(Object value, String fieldName) Require that a builder field was set before building.protected voidrequireField(Optional<?> value, String fieldName) Require that a builder field backed by anOptionalwas set before building.protected abstract voidvalidate()Validate the fields required by every build path.
-
Constructor Details
-
ValidatedBuilder
Create a new ValidatedBuilder.- Parameters:
builtTypeName- The name of the type this builder builds, used in validation error messages
-
-
Method Details
-
build
Validate required fields and build the result.- Returns:
- The built result
- Throws:
IllegalStateException- if a required field is missing
-
validate
protected abstract void validate()Validate the fields required by every build path.- Throws:
IllegalStateException- if a required field is missing
-
buildInternal
Construct the result aftervalidate()has passed.- Returns:
- The built result
-
requireField
Require that a builder field was set before building.- Parameters:
value- The field value to checkfieldName- The name of the field, used in the error message- Throws:
IllegalStateException- if value is empty
-
requireField
Require that a builder field backed by anOptionalwas set before building.- Parameters:
value- The optional field value to checkfieldName- The name of the field, used in the error message- Throws:
IllegalStateException- if value is empty
-