Class ProtoBuilder<C,B extends ProtoBuilder<C,B>>

Type Parameters:
C - The core type: an instance of that type is associated with the builder instance to hold the data to be collected during the build process. That type is expected to be mutable, at least in the scope of the concrete builder implementation.
B - The builder type: the intended effective type of the concrete builder implementation.
All Implemented Interfaces:
Setup<C,B>
Direct Known Subclasses:
Charger, DataBuilder

public class ProtoBuilder<C,B extends ProtoBuilder<C,B>> extends BuilderBase<B> implements Setup<C,B>
Serves as a base class for builder implementations <B> and as such provides a model that separates basic builder concepts from the actual core data model <C> that holds the actual values for the final result.

This implementation can be used as a base if an instance of the core type should be linked to the builder instance from the start, but the result of the build process shouldn't necessarily be of the core type, but instead a mapping exists that converts the core type to the final result type.

Core type and result type can also be identical, in particular the final result can be identical to the originally associated core instance (if e.g. Function.identity() is used as mapping). But then you have to be careful with the life cycle of the builder instance!

  • Constructor Details

    • ProtoBuilder

      protected ProtoBuilder(C core, Class<B> builderClass)
      Initializes a new instance.
      Parameters:
      core - The core instance to be associated with the builder. The implementation assumes that it is exclusively available to the builder, at least for the course of the build process.
      builderClass - The Class representation of the intended effective builder type.
      Throws:
      IllegalArgumentException - if the given builder class does not represent this instance.
    • ProtoBuilder

      protected ProtoBuilder(C core, ProtoBuilder.Lifecycle lifecycle, Class<B> builderClass)
      Initializes a new instance.
      Parameters:
      core - The core instance to be associated with the builder. The implementation assumes that it is exclusively available to the builder, at least for the course of the build process.
      lifecycle - A specific lifecycle controller for this instance.
      builderClass - The Class representation of the intended effective builder type.
      Throws:
      IllegalArgumentException - if the given builder class does not represent this instance.
  • Method Details

    • setup

      public final B setup(Consumer<? super C> consumer)
      Accepts a Consumer as modifying operation to be performed on a target instance immediately or no later than the final build() operation and returns the builder instance itself.

      This implementation applies the given consumer directly to the associated core instance.

      Specified by:
      setup in interface Setup<C,B extends ProtoBuilder<C,B>>
    • build

      protected final <R> R build(Function<? super C,R> mapping)
      Applies the core of this builder to the given mapping and returns the result.

      Be careful with the identity function if the builder doesn't have a narrowly defined lifecycle.

      Type Parameters:
      R - The result type.