T - The target 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.public class ProtoBuilder<T,B extends ProtoBuilder<T,B>> extends BuilderBase<B> implements Setup<T,B>
<B> and as such provides a model that separates basic
builder concepts from the actual target data model <T>.
This implementation can be used as a base if an instance of the target type should be linked to the builder instance from the start, but the result of the build process shouldn't necessarily be of the target type, but instead a mapping exists that converts the target type to the final result type.
Target type and result type can also be identical,
in particular the final result can be identical to the originally associated target 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!
| Modifier | Constructor and Description |
|---|---|
protected |
ProtoBuilder(T target,
Class<B> builderClass)
Initializes a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected <R> R |
build(Function<T,R> function)
Returns the result of the given
function on which the associated target was applied. |
B |
setup(Consumer<T> 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. |
THISprotected ProtoBuilder(T target, Class<B> builderClass)
target - The target 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.IllegalArgumentException - if the specified builder class does not represent the instance to create.public final B setup(Consumer<T> consumer)
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 target.
protected final <R> R build(Function<T,R> function)
function on which the associated target was applied.
Be careful with the identity function if the builder doesn't have a narrowly defined lifecycle.
R - The result type.Copyright © 2023 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.