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.public class ProtoBuilder<C,B extends ProtoBuilder<C,B>> extends BuilderBase<B> implements Setup<C,B>
<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!
| Modifier and Type | Class and Description |
|---|---|
static interface |
ProtoBuilder.Lifecycle
Abstracts a lifecycle controller for a
ProtoBuilder implementation. |
| Modifier | Constructor and Description |
|---|---|
protected |
ProtoBuilder(C core,
Class<B> builderClass)
Initializes a new instance.
|
protected |
ProtoBuilder(C core,
ProtoBuilder.Lifecycle lifecycle,
Class<B> builderClass)
Initializes a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected <R> R |
build(Function<C,R> mapping)
Applies the core of this builder to the given
mapping and returns the result. |
B |
setup(Consumer<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. |
THISprotected ProtoBuilder(C core, Class<B> builderClass)
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.IllegalArgumentException - if the given builder class does not represent this instance.protected ProtoBuilder(C core, ProtoBuilder.Lifecycle lifecycle, Class<B> builderClass)
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.IllegalArgumentException - if the given builder class does not represent this instance.public final B setup(Consumer<C> 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 core instance.
protected final <R> R build(Function<C,R> mapping)
mapping and returns the result.
Be careful with the identity function if the builder doesn't have a narrowly defined lifecycle.
R - The result type.Copyright © 2025 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.