public class Composable extends Object implements Transformable
Transformable
which supports a composition pattern.
Next to the Adapter Pattern supported via Transformers
, this implementation permits to
add components via the attach methods which can later be queried via tryAs, as and is.
This class can be used as base class or embedded into another class (using Composable(Object)
) to
make it Transformable.
Modifier and Type | Field and Description |
---|---|
protected Map<Class<?>,Object> |
components |
Constructor and Description |
---|
Composable()
Default constructor used, when Composable is used as parent class.
|
Composable(Object source)
Provides a constructor which can be used to support the composition pattern.
|
Modifier and Type | Method and Description |
---|---|
<A> A |
as(Class<A> adapterType)
Adapts this into the given adapterType.
|
<T> void |
attach(Class<? extends T> type,
T component)
Adds the given component as instance for the given type.
|
void |
attach(Object component)
Adds the given component as instance for its class.
|
boolean |
is(Class<?> adapterType)
Determines if this can be transformed to the given type.
|
<A> Optional<A> |
tryAs(Class<A> adapterType)
Tries to transform this into the given adapterType.
|
public Composable()
public Composable(Object source)
source
- the class or object which is made Transformable
.public boolean is(@Nonnull Class<?> adapterType)
Transformable
is
in interface Transformable
adapterType
- the target type to check forpublic <A> A as(@Nonnull Class<A> adapterType)
Transformable
as
in interface Transformable
A
- the type of the adapter typeadapterType
- the target type to adapt topublic <A> Optional<A> tryAs(@Nonnull Class<A> adapterType)
Transformable
tryAs
in interface Transformable
A
- the type of the adapter typeadapterType
- the target type to adapt topublic <T> void attach(Class<? extends T> type, T component)
T
- the generic type which ensures, that the component actually implements the given typetype
- the type of the component which can be used to later retrieve the componentcomponent
- the component itselfpublic void attach(Object component)
This is a boilerplate method for attach(component.getClass(), component)
.
component
- the component to attach for its own classCopyright © 2018. All rights reserved.