org.pfsw.extender
Class ObjectExtender

java.lang.Object
  extended by org.pfsw.extender.ObjectExtender

public class ObjectExtender
extends java.lang.Object

This class provides various static methods for creation of "extended" objects and to add mix-ins.


Method Summary
static
<I> I
createExtensibleObject(java.lang.Class<I> interfaceType, java.lang.Class... interfaces)
          Returns an object of the specified interface type that also implements the additional interfaces.
static
<I> I
createExtensibleObject(java.lang.Class<I> interfaceType, java.lang.Object object)
          Returns an extended object that implements the given interface and is based on the given object.
static
<I> I
createExtensibleObject(java.lang.Class<I> interfaceType, java.lang.Object object, java.lang.Class... additionalInterfaces)
          Returns an extended object that implements the given interface and is based on the given object.
static
<T> T
extend(T objectToExtend, java.lang.String extensionName, java.lang.Object extension)
          Extends the given objectToExtend with the specified extension it it is associated with an ExtendedObject.
static ExtendedObject getExtensionHandlerOf(java.lang.Object object)
          Returns the ExtendedObject that is attached to the given object or null if none is attached.
static java.lang.Object getExtensionOf(java.lang.Object object, java.lang.String extensionName)
          Returns the named extension associated with the given object or null if there is none.
static
<T> T
getMixin(java.lang.Class<T> interfaceClass, java.lang.Object object)
          Returns the mix-in implementation that is associated with the given interface and attached to the provided object.
static
<I> I
makeExtensible(java.lang.Class<I> interfaceType, I object, java.lang.Class... interfaces)
          Returns the given object enhanced by a change tracking mechanism.
static
<I,T extends I>
void
setMixinTo(java.lang.Object object, java.lang.Class<I> interfaceType, T mixin)
          Sets the mix-in implementation of the specified interface to the provided object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

makeExtensible

public static <I> I makeExtensible(java.lang.Class<I> interfaceType,
                                   I object,
                                   java.lang.Class... interfaces)
Returns the given object enhanced by a change tracking mechanism. The actual return type is the specified interface type.

Parameters:
interfaceType - The interface the provided object must implement
object - An object that should be enhanced by a change tracking mechanism

createExtensibleObject

public static <I> I createExtensibleObject(java.lang.Class<I> interfaceType,
                                           java.lang.Object object)
Returns an extended object that implements the given interface and is based on the given object.

Parameters:
interfaceType - The interface the extended object implements (must not be null)
object - The object to be extended (must not be null)

createExtensibleObject

public static <I> I createExtensibleObject(java.lang.Class<I> interfaceType,
                                           java.lang.Object object,
                                           java.lang.Class... additionalInterfaces)
Returns an extended object that implements the given interface and is based on the given object. It also supports the additional interfaces, which are however not part of the returned type.

Parameters:
interfaceType - The interface the extended object implements (must not be null)
object - The object to be extended (must not be null)
additionalInterfaces - More interface that are not part of the return type

createExtensibleObject

public static <I> I createExtensibleObject(java.lang.Class<I> interfaceType,
                                           java.lang.Class... interfaces)
Returns an object of the specified interface type that also implements the additional interfaces. The implementation instances for all interfaces must be added as mix-ins.

Parameters:
interfaceType - The interface the provided object must implement
interfaces - Optional additional interfaces

getMixin

public static <T> T getMixin(java.lang.Class<T> interfaceClass,
                             java.lang.Object object)
Returns the mix-in implementation that is associated with the given interface and attached to the provided object. If no such mix-in can be found or the given object is not an extended object then this method returns null.

Parameters:
interfaceClass - The interface of the mix-in
object - The extended object that might contain the mix-in

setMixinTo

public static <I,T extends I> void setMixinTo(java.lang.Object object,
                                              java.lang.Class<I> interfaceType,
                                              T mixin)
Sets the mix-in implementation of the specified interface to the provided object. If any of the given parameters is null, nothing happens.


getExtensionHandlerOf

public static ExtendedObject getExtensionHandlerOf(java.lang.Object object)
Returns the ExtendedObject that is attached to the given object or null if none is attached.


getExtensionOf

public static java.lang.Object getExtensionOf(java.lang.Object object,
                                              java.lang.String extensionName)
Returns the named extension associated with the given object or null if there is none.


extend

public static <T> T extend(T objectToExtend,
                           java.lang.String extensionName,
                           java.lang.Object extension)
Extends the given objectToExtend with the specified extension it it is associated with an ExtendedObject. If an extension with the same name already exists, it will be replaced.

Parameters:
objectToExtend - The object to be extended (must not be null).
extensionName - The name of the extension (must not be null).
extension - The extension object (must not be null).
Returns:
The now extended objectToExtend.