Class ExtensionSupport

java.lang.Object
ca.phon.extensions.ExtensionSupport
All Implemented Interfaces:
IExtendable

public final class ExtensionSupport extends Object implements IExtendable

Class which provides extension support for objects that implement the IExtendable interface.

This class can also perform automatic installation of extensions declared in the META-INF/services/ca.phon.extensions.ExtensionProvider. file.

Example:
The IExtendable object:

 public class MyExtendableObject implements IExtendable {

    private final ExtensionSupport = new ExtensionSupport(MyExtendableObject.class, this);

    ...
 }
 
Custom extension (note the requirement of the Extension annotation:)
 @MyExtension(MyExtendableObject.class)
 public class MyExtension {
 }
 
Extension provider (note the requirement of the Extension annotation:)
 @Extension(MyExtendableObject.class)
 public class MyExtensionProvider implements ExtensionProvider {

     @Overrides
     public void installExtension(IExtendable obj) {
         obj.putExtension(MyExtension.class, new MyExtension());
     }

 }
 
In the META-INF/services/ca.phon.extensions.ExtensionProvider file:
 my.class.path.MyExtensionProvider
 
Now, every time a new instance of MyExtendableObject is created a new MyExtension object will also be created and attached to our IExtendable object.

  • Constructor Details

    • ExtensionSupport

      public ExtensionSupport(Class<? extends T> declaredType, T obj)
      Create a new extension support object for the given object. This class will automatically load any extensions
      Parameters:
      declaredType -
      obj -
  • Method Details

    • initExtensions

      public void initExtensions()
      Load automatic extensions as defined in the classpath META-INF/services files
    • getExtensions

      public Set<Class<?>> getExtensions()
      Description copied from interface: IExtendable
      Return all extension types supported
      Specified by:
      getExtensions in interface IExtendable
    • getExtension

      public <T> T getExtension(Class<T> cap)
      Description copied from interface: IExtendable
      Get the requested extension if available.
      Specified by:
      getExtension in interface IExtendable
      Returns:
      the capability object or null if the cability is not available
    • putExtension

      public <T> T putExtension(Class<T> cap, T impl)
      Description copied from interface: IExtendable
      Add a new extension.
      Specified by:
      putExtension in interface IExtendable
      Parameters:
      cap - the extension to add
      Returns:
      the added extension implementation
    • removeExtension

      public <T> T removeExtension(Class<T> cap)
      Description copied from interface: IExtendable
      Remove a capability.
      Specified by:
      removeExtension in interface IExtendable
      Parameters:
      cap - the capability to remove
    • getOptionalExtension

      public static <T> Optional<T> getOptionalExtension(IExtendable extendable, Class<T> cap)
      Static utility method for returning an extension wrapped in a Optional
      Parameters:
      extendable -
      cap -
      Returns:
      optional of requested extension