Class DataBuilder<C,T,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.
T - The target type: an instance of that type will be returned by build().
B - The builder type: the intended effective type of the concrete builder implementation.
All Implemented Interfaces:
Setup<C,B>

public class DataBuilder<C,T,B extends ProtoBuilder<C,B>> extends ProtoBuilder<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>.

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

Core type <C> and target type <T> may also be the same, but avoid the final result to be identical to the originally associated target instance (e.g. when using Function.identity() as mapping). You should prefer Charger in this particular case!

  • Constructor Details

    • DataBuilder

      protected DataBuilder(C core, Function<? super C,? extends T> mapping, 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.
      mapping - A Function to map the associated core to the final target. Even if the core type <C> matches the target type <T> that Function should create a new target instance (otherwise prefer Charger).
      builderClass - The Class representation of the intended effective builder type.
      Throws:
      IllegalArgumentException - if the given builder class does not represent this instance.
  • Method Details

    • peek

      public final <R> R peek(Function<? super C,R> function)
      Applies the core of this builder to the given Function and returns the result.

      While the Function is being called, the core is exclusively available to it, but must not be "hijacked" from the context of the call or the executing thread. Otherwise, you may produce uncontrollable side effects!

    • build

      public final T build()
      Returns the build result.