C - The type of the context.public abstract class FactoryHub<C> extends Object
A method is identified by a token (a constant) of the respective result type and called indirectly via
get(Object).
When a method is called, it is parameterized internally with a context of a certain type that is predefined when the hub is initialized. Externally, apart from the identification of the method via the token, no further parameterization is required.
In addition to the central method get(Object) mentioned above, a hub provides additional methods
that are used to generate composite instances, whereby tokens can be used to define the generation of
individual elements: stream(Object), stream(Object, int), map(Object, Object, int),
map(Map), map(Object, Function, Function).
Instances that contain a FactoryHub or are an extended FactoryHub itself are typically used as
context, so that the methods that are assigned to a FactoryHub can be used by other such methods.
Types that contain a FactoryHub or are themselves extended FactoryHubs are typically used
as context type, so that the methods assigned to a FactoryHub can be used by other such methods.
The following example shows a derivation:
public final class FactoryHubSample extends FactoryHub<FactoryHubSample> {
// Some pre-defined tokens ...
public static final Byte BYTE = Byte.MAX_VALUE;
public static final Short SHORT = Short.MAX_VALUE;
public static final Integer INTEGER = Integer.MAX_VALUE;
public static final Long LONG = Long.MAX_VALUE;
private final Random random = new Random();
// The instantiation takes place via a builder pattern ...
private FactoryHubSample(final Builder builder) {
super(builder);
}
// To get a builder that has already been pre-initialized
// with the tokens defined above and corresponding methods ...
public static Builder builder() {
return new Builder().on(BYTE).apply(context -> context.anyBits(Byte.SIZE).byteValue())
.on(SHORT).apply(context -> context.anyBits(Short.SIZE).shortValue())
.on(INTEGER).apply(context -> context.anyBits(Integer.SIZE).intValue())
.on(LONG).apply(context -> context.anyBits(Long.SIZE).longValue());
}
// Implementation of the method that provides the context ...
@Override
protected final FactoryHubSample getContext() {
return this;
}
// A basic method to be provided by this context ...
public final BigInteger anyBits(final int numBits) {
return new BigInteger(numBits, random);
}
// Definition of a Builder for a new instance ...
public static class Builder extends Collector<FactoryHubSample, Builder> {
// Implementation of the method that provides the Builder as such for the underlying Collector ...
@Override
protected final Builder getBuilder() {
return this;
}
// finally the typical production method of the Builder ...
public final FactoryHubSample build() {
return new FactoryHubSample(this);
}
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
FactoryHub.Builder<C>
A generic Builder implementation.
|
static class |
FactoryHub.Collector<C,B>
Abstracts a tool for preparing and initializing a
FactoryHub intended for extension into a
Builder implementation. |
| Modifier | Constructor and Description |
|---|---|
protected |
FactoryHub(FactoryHub.Collector<C,?> collector)
Initializes a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
static <C> FactoryHub.Builder<C> |
builder(Supplier<C> contextGetter)
Returns a new generic instance of
FactoryHub.Builder. |
<T> T |
get(T token)
Produces an instance of a certain type, whereby the production method to be used is identified by a
token of the result type.
|
protected abstract C |
getContext()
Returns the context associated with this
FactoryHub. |
static <C> FactoryHub<C> |
instance(FactoryHub.Collector<C,?> collector,
Supplier<C> contextGetter)
Returns a new generic instance of
FactoryHub. |
<K,V> Map<K,V> |
map(K keyToken,
V valueToken,
int size)
|
<K,V> Map<K,V> |
map(Map<K,V> template)
|
<T,R> R |
map(T template,
Function<T,Map<?,?>> toMap,
Function<Map<?,?>,R> reMap)
Produces a result of a certain type from a
template of a certain type. |
<T> Stream<T> |
stream(T token)
Produces an infinite (!)
|
<T> Stream<T> |
stream(T token,
int length)
Produces a
Stream with limited length of newly produced elements
af a certain type, whereby the production method to be used for each element is identified by a
token of the same type. |
protected FactoryHub(FactoryHub.Collector<C,?> collector)
public static <C> FactoryHub<C> instance(FactoryHub.Collector<C,?> collector, Supplier<C> contextGetter)
FactoryHub.public static <C> FactoryHub.Builder<C> builder(Supplier<C> contextGetter)
FactoryHub.Builder.protected abstract C getContext()
FactoryHub.public final <T> T get(T token)
The association of token and production method is made when the hub is initialized via a
FactoryHub.Collector.
Returns the token itself if no method is associated with it ("unknown token"), unless the initially defined unknownTokenListener, which is then called, throws an exception.
The result is always null if the token is null.
T - The type of the produced result.RuntimeException - if the token is not associated with a production method
and the initially defined
unknownTokenListener throws one.FactoryUtil.ACCEPT_UNKNOWN_TOKEN,
FactoryUtil.DENY_UNKNOWN_TOKEN,
FactoryUtil.LOG_UNKNOWN_TOKENpublic final <T> Stream<T> stream(T token)
Stream of newly produced elements af a certain type,
whereby the production method to be used for each element is identified by a token of the same type.
Does the same as Stream.generate(() -> get(token)).
T - The type of the produced elements.RuntimeException - if the token is not associated with a production method
and the initially defined unknownTokenListener throws one.get(Object),
stream(Object, int)public final <T> Stream<T> stream(T token, int length)
Stream with limited length of newly produced elements
af a certain type, whereby the production method to be used for each element is identified by a
token of the same type.
Does the same as Stream.generate(() -> get(token)).limit(length).
T - The type of the produced elements.RuntimeException - if the token is not associated with a production method
and the initially defined unknownTokenListener throws one.get(Object),
stream(Object)public final <K,V> Map<K,V> map(K keyToken, V valueToken, int size)
Map of a given size whose keys are produced based on
the given keyToken and whose values are produced based on the given
valueToken.
The size of the resulting map may be less than the requested size
if the method for key generation returns the same values over several calls.
K - The type of keys of the resulting Map.V - The type of values of the resulting Map.RuntimeException - if a token is not associated with a production method
and the initially defined unknownTokenListener throws one.get(Object),
map(Map)public final <K,V> Map<K,V> map(Map<K,V> template)
Map based on a given template Map.
The keys of the template are adopted unchanged in the result and the values are
produced based on the values (tokens) of the template.K - The type of keys of the resulting Map.V - The type of values of the resulting Map.RuntimeException - if a token is not associated with a production method
and the initially defined unknownTokenListener throws one.get(Object),
map(Object, Object, int)public final <T,R> R map(T template,
Function<T,Map<?,?>> toMap,
Function<Map<?,?>,R> reMap)
template of a certain type.
First, a Map is generated from the template that contains its properties,
which are interpreted as tokens. This is translated via map(Map).
The resulting Map is then used to generate the result.T - The type of the template.R - The type of the result. Mostly but not necessarily the same as the template type.template - The template.toMap - A method to create a Map containing the properties of the template.reMap - A method to create the result from a Map.RuntimeException - if a token is not associated with a production method
and the initially defined unknownTokenListener throws one.get(Object)Copyright © 2022 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.