A
- The type of IAdaptable
supported by this class. If
passed-in adapters implement the IAdaptable.Bound
interface, the generic type parameter of IAdaptable.Bound
has to match this one.public class AdaptableSupport<A extends IAdaptable> extends java.lang.Object implements IDisposable
IAdaptable
. It offers
all methods defined by IAdaptable
, while not formally implementing
the interface, and can thus be used by a source IAdaptable
as a
delegate.
In addition to the source IAdaptable
a PropertyChangeSupport
is expected to be passe in during construction. It will be used to fire
PropertyChangeEvent
s whenever an adapter is set (
setAdapter(AdapterKey, Object)
) or unset (
unsetAdapter(AdapterKey)
). IAdaptable.ADAPTERS_PROPERTY
will
be used as the property name within all change events.
If the IAdaptable
is also IActivatable
, it will ensure
adapters are activated/deactivated when being set/unset dependent on the
active state of the adaptable at that moment. However, the
AdaptableSupport
will not register a listener for the active state of
the source IAdaptable
, so changes to its active state will not result
in state changes of the registered adapters. For this purpose, an
ActivatableSupport
may be used by the source IAdaptable
as a
second delegate.
Constructor and Description |
---|
AdaptableSupport(A source,
java.beans.PropertyChangeSupport pcs)
|
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Disposes this
AdaptableSupport , which will unregister all
currently registered adapters, unbind them from their source
IAdaptable (in case they are IAdaptable.Bound ), and
dispose them (if they are IDisposable ). |
<T> T |
getAdapter(AdapterKey<? super T> key)
Returns an adapter for the given
AdapterKey if one can
unambiguously be retrieved, i.e. if there is only a single adapter
registered under a key that 'matches' the given AdapterKey . |
<T> T |
getAdapter(java.lang.Class<? super T> key)
Returns an adapter for the given
Class key if one can
unambiguously be retrieved. |
<T> T |
getAdapter(com.google.common.reflect.TypeToken<? super T> key)
Returns an adapter for the given
TypeToken key if one can
unambiguously be retrieved. |
java.util.Map<AdapterKey<?>,java.lang.Object> |
getAdapters()
Retrieves all registered adapters, mapped to the respective
AdapterKey s they are registered. |
<T> java.util.Map<AdapterKey<? extends T>,T> |
getAdapters(java.lang.Class<? super T> key)
Returns all adapters 'matching' the given
Class key, i.e. all
adapters whose AdapterKey 's TypeToken key
AdapterKey.getKey() ) refers to the same or a sub-type of the
given Class key (see TypeToken.isAssignableFrom(Type) ). |
<T> java.util.Map<AdapterKey<? extends T>,T> |
getAdapters(com.google.common.reflect.TypeToken<? super T> key)
Returns all adapters 'matching' the given
TypeToken key, i.e. all
adapters whose AdapterKey 's TypeToken key
AdapterKey.getKey() ) refers to the same or a sub-type or of the
given TypeToken key (see
TypeToken.isAssignableFrom(TypeToken) ). |
<T> void |
setAdapter(AdapterKey<? super T> key,
T adapter)
Registers the given adapter under the given
AdapterKey . |
<T> void |
setAdapter(java.lang.Class<? super T> key,
T adapter)
Registers the given adapter under an
AdapterKey , which will use a
TypeToken representing the given Class key, i.e. using
TypeToken.of(Class) , as well as the default role (see
AdapterKey.DEFAULT_ROLE . |
<T> void |
setAdapter(com.google.common.reflect.TypeToken<? super T> key,
T adapter)
Registers the given adapter under an
AdapterKey , which will use
the given TypeToken key as well as the default role (see
AdapterKey.DEFAULT_ROLE . |
void |
setAdapters(java.util.Map<AdapterKey<?>,java.lang.Object> adaptersWithKeys,
boolean overwrite)
Registers the given adapters under the provided keys by delegating to
setAdapter(AdapterKey, Object) . |
<T> T |
unsetAdapter(AdapterKey<? super T> key)
Unregisters the adapter registered under the exact
AdapterKey
given, returning it for convenience. |
public AdaptableSupport(A source, java.beans.PropertyChangeSupport pcs)
source
- The IAdaptable
that encloses the to be created
AdaptableSupport
, delegating calls to it. May not be
null
pcs
- An PropertyChangeSupport
, which will be used to fire
PropertyChangeEvent
's whenever adapters are set or
unset. May not be null
public <T> T getAdapter(AdapterKey<? super T> key)
AdapterKey
if one can
unambiguously be retrieved, i.e. if there is only a single adapter
registered under a key that 'matches' the given AdapterKey
.T
- The adapter type.key
- The AdapterKey
used to retrieve a registered adapter.AdapterKey
or null
if none could be
retrieved.IAdaptable.getAdapter(AdapterKey)
public <T> T getAdapter(java.lang.Class<? super T> key)
Class
key if one can
unambiguously be retrieved. That is, if there is only a single adapter
that 'matches' the given Class
key, this adapter is returned,
ignoring the role under which it is registered (see
AdapterKey.getRole()
).T
- The adapter type.key
- The Class
key used to retrieve a registered adapter.Class
key or null
if none could be retrieved.IAdaptable.getAdapter(Class)
public <T> T getAdapter(com.google.common.reflect.TypeToken<? super T> key)
TypeToken
key if one can
unambiguously be retrieved. That is, if there is only a single adapter
that 'matches' the given TypeToken
key, this adapter is returned,
ignoring the role under which it is registered (see
AdapterKey.getRole()
).T
- The adapter type.key
- The TypeToken
key used to retrieve a registered
adapter.TypeToken
key or null
if none could be
retrieved.IAdaptable.getAdapter(TypeToken)
public java.util.Map<AdapterKey<?>,java.lang.Object> getAdapters()
AdapterKey
s they are registered.AdapterKey
s as a copy.public <T> java.util.Map<AdapterKey<? extends T>,T> getAdapters(java.lang.Class<? super T> key)
Class
key, i.e. all
adapters whose AdapterKey
's TypeToken
key
AdapterKey.getKey()
) refers to the same or a sub-type of the
given Class
key (see TypeToken.isAssignableFrom(Type)
).T
- The adapter type.key
- The Class
key to retrieve adapters for.Map
containing all those adapters registered at this
AdaptableSupport
, whose AdapterKey
's
TypeToken
key (AdapterKey.getKey()
) refers to the
same or a sub-type of the given Class
key, qualified by
their respective AdapterKey
s.IAdaptable.getAdapters(Class)
public <T> java.util.Map<AdapterKey<? extends T>,T> getAdapters(com.google.common.reflect.TypeToken<? super T> key)
TypeToken
key, i.e. all
adapters whose AdapterKey
's TypeToken
key
AdapterKey.getKey()
) refers to the same or a sub-type or of the
given TypeToken
key (see
TypeToken.isAssignableFrom(TypeToken)
).T
- The adapter type.key
- The TypeToken
key to retrieve adapters for.Map
containing all those adapters registered at this
AdaptableSupport
, whose AdapterKey
's
TypeToken
key (AdapterKey.getKey()
) refers to the
same or a sub-type of the given TypeToken
key, qualified
by their respective AdapterKey
s.IAdaptable.getAdapters(TypeToken)
public <T> void setAdapter(java.lang.Class<? super T> key, T adapter)
AdapterKey
, which will use a
TypeToken
representing the given Class
key, i.e. using
TypeToken.of(Class)
, as well as the default role (see
AdapterKey.DEFAULT_ROLE
.T
- The adapter type.key
- The Class
under which to register the given adapter.adapter
- The adapter to register under the given Class
key.IAdaptable.setAdapter(Class, Object)
public <T> void setAdapter(com.google.common.reflect.TypeToken<? super T> key, T adapter)
AdapterKey
, which will use
the given TypeToken
key as well as the default role (see
AdapterKey.DEFAULT_ROLE
.T
- The adapter type.key
- The TypeToken
under which to register the given
adapter.adapter
- The adapter to register under the given TypeToken
key.IAdaptable.setAdapter(TypeToken, Object)
public <T> void setAdapter(AdapterKey<? super T> key, T adapter)
AdapterKey
. The
adapter has to be compliant to the AdapterKey
, i.e. it has to be
of the same type or a sub-type of the AdapterKey
's type key (
AdapterKey.getKey()
).T
- The adapter type.key
- The AdapterKey
under which to register the given
adapter.adapter
- The adapter to register under the given AdapterKey
.IAdaptable.setAdapter(AdapterKey, Object)
public void setAdapters(java.util.Map<AdapterKey<?>,java.lang.Object> adaptersWithKeys, boolean overwrite)
setAdapter(AdapterKey, Object)
. Note, that delegation will only
occur for those adapters, whose key is not already bound to an adapter,
in case overwrite is set to false
.adaptersWithKeys
- A map of class keys and related adapters to be added via
setAdapter(AdapterKey, Object)
.overwrite
- Indicates whether adapters whose keys are already registered
for another adapter should be ignored. If set to
true
existing entries will be overwritten,
otherwise, existing entries will be preserved.public <T> T unsetAdapter(AdapterKey<? super T> key)
AdapterKey
given, returning it for convenience.T
- The adapter type.key
- The AdapterKey
for which to remove a registered
adapter.IAdaptable.unsetAdapter(AdapterKey)
public void dispose()
AdaptableSupport
, which will unregister all
currently registered adapters, unbind them from their source
IAdaptable
(in case they are IAdaptable.Bound
), and
dispose them (if they are IDisposable
). No notification will be
fired to notify listeners about the unregistering of adapters. It is
expected that in case the source IAdaptable
is
IActivatable
, it is deactivated before disposing this
AdaptableSupport
.dispose
in interface IDisposable
Copyright (c) 2014 itemis AG and others. All rights reserved.