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

public abstract class ValidatedBuilder<T> extends Object
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 Details

    • ValidatedBuilder

      protected ValidatedBuilder(String builtTypeName)
      Create a new ValidatedBuilder.
      Parameters:
      builtTypeName - The name of the type this builder builds, used in validation error messages
  • Method Details

    • build

      public final T 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

      protected abstract T buildInternal()
      Construct the result after validate() has passed.
      Returns:
      The built result
    • requireField

      protected void requireField(Object value, String fieldName)
      Require that a builder field was set before building.
      Parameters:
      value - The field value to check
      fieldName - The name of the field, used in the error message
      Throws:
      IllegalStateException - if value is empty
    • requireField

      protected void requireField(Optional<?> value, String fieldName)
      Require that a builder field backed by an Optional was set before building.
      Parameters:
      value - The optional field value to check
      fieldName - The name of the field, used in the error message
      Throws:
      IllegalStateException - if value is empty