TDecoratedType - The type of the instances whose ILookup.lookup(Class) behavior should be modified.TInterface - The interface or type that clients will use during the ILookup.lookup(Class) call. Note that this is normally an
interface and not the implementation of the interface since client code will use the interface for the query.public final class LookupDecorator<TDecoratedType,TInterface> extends Object
ILookupDecorator.
This class provides convenience methods that help in performing common tasks related to decorating the ILookup.lookup(Class)
method for a certain TDecoratedType.
ILookup,
GraphDecorator,
LookupExtensions| Constructor and Description |
|---|
LookupDecorator(Class<TDecoratedType> decoratedType,
Class<TInterface> interfaceType,
ILookupDecorator decorator,
boolean decorateNulls,
boolean nullIsFallback)
Initializes a new instance of the
LookupDecorator class. |
| Modifier and Type | Method and Description |
|---|---|
IContextLookupChainLink |
addChainLink(IContextLookupChainLink lookup)
Simple helper method that tries to add the provided chain link to the
Decorator. |
ILookupDecorator |
getDecorator()
Gets the
ILookupDecorator instance that is used. |
IContextLookupChainLink |
hideImplementation()
Amends the
lookup chain for all TDecoratedType items matching a predicate
to return null if TInterface is queried during the
ILookup.lookup(Class). |
IContextLookupChainLink |
hideImplementation(Predicate<TDecoratedType> predicate)
Amends the
lookup chain for all TDecoratedType items matching a predicate
to return null if TInterface is queried during the
ILookup.lookup(Class). |
boolean |
isDecoratingNullsEnabled()
Gets a value indicating whether wrapping implementations will wrap
null values from the rest of the lookup chain
or those values will simply be returned instead. |
boolean |
isNullFallback()
Gets a value indicating whether a
null result value of the methods setImplementation(Predicate, java.lang.Object)
and setFactory(Function) should be interpreted as taking the fallback value, or actually return null. |
void |
setDecoratingNullsEnabled(boolean value)
Sets a value indicating whether wrapping implementations will wrap
null values from the rest of the lookup chain
or those values will simply be returned instead. |
IContextLookupChainLink |
setFactory(Function<TDecoratedType,TInterface> factory)
Amends the
lookup chain for all TDecoratedType items, so that factory is
used to create a TInterface instance if TInterface is queried during the
ILookup.lookup(Class). |
IContextLookupChainLink |
setFactory(Predicate<TDecoratedType> predicate,
Function<TDecoratedType,TInterface> factory)
Amends the
lookup chain for items matching a predicate with a factory
that creates a TInterface instance if TInterface is queried during the
ILookup.lookup(Class). |
IContextLookupChainLink |
setImplementation(Predicate<TDecoratedType> predicate,
TInterface implementation)
Amends the
lookup chain for all items matching a predicate with a fixed TInterface
implementation if TInterface is queried on an item's
ILookup.lookup(Class). |
IContextLookupChainLink |
setImplementation(TDecoratedType item,
TInterface implementation)
Amends the
lookup chain for a single item with a fixed TInterface implementation
if TInterface is queried on the item's ILookup.lookup(Class). |
IContextLookupChainLink |
setImplementation(TInterface implementation)
Amends the
lookup chain, so that implementation is always returned if TInterface
is queried on an item's ILookup.lookup(Class). |
IContextLookupChainLink |
setImplementationWrapper(BiFunction<TDecoratedType,TInterface,TInterface> factory)
Amends the
lookup chain for all items with a factory callback that wraps the
underlying implementation if TInterface is queried during the
ILookup.lookup(Class). |
IContextLookupChainLink |
setImplementationWrapper(Predicate<TDecoratedType> predicate,
BiFunction<TDecoratedType,TInterface,TInterface> factory)
Amends the
lookup chain for all items matching a predicate with a factory
callback that wraps the underlying implementation if TInterface is queried during the
ILookup.lookup(Class). |
void |
setNullFallback(boolean value)
Sets a value indicating whether a
null result value of the methods setImplementation(Predicate, java.lang.Object)
and setFactory(Function) should be interpreted as taking the fallback value, or actually return null. |
public LookupDecorator(Class<TDecoratedType> decoratedType, Class<TInterface> interfaceType, ILookupDecorator decorator, boolean decorateNulls, boolean nullIsFallback)
LookupDecorator class.decoratedType - The type of the instances whose ILookup.lookup(Class) behavior should be modified.interfaceType - The interface or type that clients will use during the ILookup.lookup(Class) call. Note
that this is normally an interface and not the implementation of the interface since client code will use the interface
for the query.decorator - The decorator instance that will be used to
extend the Lookup mechanism.decorateNulls - The initial value of the DecoratingNullsEnabled property.nullIsFallback - The initial value of the NullFallback property.public final IContextLookupChainLink addChainLink(IContextLookupChainLink lookup)
Decorator.lookup - The link to add.lookup that has been added to the Decorator
as a result of this call, or null if the decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final ILookupDecorator getDecorator()
ILookupDecorator instance that is used.
This can be null. In this case all methods of this class do nothing.
public final IContextLookupChainLink hideImplementation()
lookup chain for all TDecoratedType items matching a predicate
to return null if TInterface is queried during the
ILookup.lookup(Class).
This can be used to temporarily or permanently disable a certain behavior for all items. To do this only for items
matching a predicate, use hideImplementation(Predicate) instead.
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final IContextLookupChainLink hideImplementation(Predicate<TDecoratedType> predicate)
lookup chain for all TDecoratedType items matching a predicate
to return null if TInterface is queried during the
ILookup.lookup(Class).
This can be used to temporarily or permanently disable a certain behavior for all items. To do this only for items
matching a predicate, use hideImplementation(Predicate) instead.
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
predicate - The predicate that determines for which items the implementation shall be hidden.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final boolean isDecoratingNullsEnabled()
null values from the rest of the lookup chain
or those values will simply be returned instead.
If this is true, then the SetImplementationWrapper
methods will not wrap null values from the rest of the lookup chain and the result of the call to ILookup.lookup(Class)
will simply be null. If this is false, the wrapper will get null as the implementation to wrap
and should handle this accordingly.
setDecoratingNullsEnabled(boolean)public final boolean isNullFallback()
null result value of the methods setImplementation(Predicate, java.lang.Object)
and setFactory(Function) should be interpreted as taking the fallback value, or actually return null.
If this is true, then null values returned from the implementation or factory set with setImplementation(java.lang.Object)
or setFactory(Function) are not returned, but rather interpreted as a hint to query the rest of the lookup
chain. If this is false, then null values are returned as is.
setNullFallback(boolean)public final void setDecoratingNullsEnabled(boolean value)
null values from the rest of the lookup chain
or those values will simply be returned instead.
If this is true, then the SetImplementationWrapper
methods will not wrap null values from the rest of the lookup chain and the result of the call to ILookup.lookup(Class)
will simply be null. If this is false, the wrapper will get null as the implementation to wrap
and should handle this accordingly.
value - The DecoratingNullsEnabled to set.isDecoratingNullsEnabled()public final IContextLookupChainLink setFactory(Function<TDecoratedType,TInterface> factory)
lookup chain for all TDecoratedType items, so that factory is
used to create a TInterface instance if TInterface is queried during the
ILookup.lookup(Class).
This can be used to modify the lookup of all items where the returned instance either depends on the item or uses it in some way.
To only adjust the lookup of items matching a predicate in this manner, use setFactory(Predicate, Function)
instead. To adjust the lookup of a single item, use setImplementation(java.lang.Object).
Note that depending on the value of the NullFallback property a null return value from
the factory will be either returned directly, or interpreted as a hint to return a fallback value which is
obtained by querying the remainder of the lookup chain.
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
factory - The factory that is queried to create the result.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensions,
isNullFallback()public final IContextLookupChainLink setFactory(Predicate<TDecoratedType> predicate, Function<TDecoratedType,TInterface> factory)
lookup chain for items matching a predicate with a factory
that creates a TInterface instance if TInterface is queried during the
ILookup.lookup(Class).
This can be used to modify the lookup of all items matching a certain predicate where the returned instance either depends on the item or uses it in some way.
To adjust the lookup of all items in this manner, use setFactory(Function) instead. To adjust the lookup of a
single item, use setImplementation(java.lang.Object).
Note that depending on the value of the NullFallback property a null return value from
the factory will be either returned directly, or interpreted as a hint to return a fallback value which is
obtained by querying the remainder of the lookup chain.
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
predicate - The predicate that determines for which items the factory shall be called.factory - The factory that is queried to create the result.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensions,
isNullFallback()public final IContextLookupChainLink setImplementation(Predicate<TDecoratedType> predicate, TInterface implementation)
lookup chain for all items matching a predicate with a fixed TInterface
implementation if TInterface is queried on an item's
ILookup.lookup(Class).
This can be used to modify the lookup of all items matching a certain predicate in the same way.
To adjust the lookup of only a single item, use setImplementation(java.lang.Object, java.lang.Object) instead.
To adjust the lookup of all items, use setImplementation(java.lang.Object).
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
predicate - A predicate that determines which items are affected.implementation - The implementation to return if the predicate matches and TInterface is queried.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final IContextLookupChainLink setImplementation(TDecoratedType item, TInterface implementation)
lookup chain for a single item with a fixed TInterface implementation
if TInterface is queried on the item's ILookup.lookup(Class).
This can be used to modify the lookup of a single item only.
To adjust the lookup of all items matching a predicate, use setImplementation(Predicate, java.lang.Object)
instead. To adjust the lookup of all items, use setImplementation(java.lang.Object).
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
item - The item for which the implementation shall be returned.implementation - The implementation to return if item is queried for TInterface.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final IContextLookupChainLink setImplementation(TInterface implementation)
lookup chain, so that implementation is always returned if TInterface
is queried on an item's ILookup.lookup(Class).
This can be used to modify the lookup of all items in the same way.
To adjust the lookup of only a single item, use setImplementation(java.lang.Object, java.lang.Object) instead.
To adjust the lookup of all items matching a certain predicate, use
setImplementation(Predicate, java.lang.Object).
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
implementation - The implementation to return unconditionally.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final IContextLookupChainLink setImplementationWrapper(BiFunction<TDecoratedType,TInterface,TInterface> factory)
lookup chain for all items with a factory callback that wraps the
underlying implementation if TInterface is queried during the
ILookup.lookup(Class).
This can be used to modify the lookup of all items matching where the factory implementation may delegate to the wrapped implementation in certain cases.
To adjust the lookup in this manner only for items matching a predicate, use setImplementationWrapper(Predicate, BiFunction)
instead. To adjust the lookup of a single item, use setImplementation(java.lang.Object).
Note that depending on the value of the DecoratingNullsEnabled property, the factory
will not not be called to decorate null results.
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
factory - The factory that will be passed the underlying implementation and that is queried to create the result.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final IContextLookupChainLink setImplementationWrapper(Predicate<TDecoratedType> predicate, BiFunction<TDecoratedType,TInterface,TInterface> factory)
lookup chain for all items matching a predicate with a factory
callback that wraps the underlying implementation if TInterface is queried during the
ILookup.lookup(Class).
This can be used to modify the lookup of all items matching a certain predicate where the factory implementation may delegate to the wrapped implementation in certain cases.
To adjust the lookup for all items in this manner, use setImplementationWrapper(BiFunction) instead. To adjust
the lookup of a single item, use setImplementation(java.lang.Object).
Note that depending on the value of the DecoratingNullsEnabled property, the factory
will not not be called to decorate null results.
The effects of this method call can be undone by passing the return value to
ILookupDecorator.removeLookup(Class, IContextLookupChainLink).
predicate - The predicate that determines for which items the wrapping shall be performed.factory - The factory that will be passed the underlying implementation and that is queried to create the result.IContextLookupChainLink that has been added
to the Decorator as a result of this call, or null if decoration was unsuccessful.ILookupDecorator.addLookup(Class, IContextLookupChainLink),
LookupExtensionspublic final void setNullFallback(boolean value)
null result value of the methods setImplementation(Predicate, java.lang.Object)
and setFactory(Function) should be interpreted as taking the fallback value, or actually return null.
If this is true, then null values returned from the implementation or factory set with setImplementation(java.lang.Object)
or setFactory(Function) are not returned, but rather interpreted as a hint to query the rest of the lookup
chain. If this is false, then null values are returned as is.
value - The NullFallback to set.isNullFallback()