Package ca.phon.extensions
Class ExtensionSupport
java.lang.Object
ca.phon.extensions.ExtensionSupport
- All Implemented Interfaces:
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.MyExtensionProviderNow, every time a new instance of
MyExtendableObject
is created
a new MyExtension
object will also be created and attached to
our IExtendable
object.
-
Constructor Summary
ConstructorDescriptionExtensionSupport
(Class<? extends T> declaredType, T obj) Create a new extension support object for the given object. -
Method Summary
Modifier and TypeMethodDescription<T> T
getExtension
(Class<T> cap) Get the requested extension if available.Return all extension types supportedstatic <T> Optional<T>
getOptionalExtension
(IExtendable extendable, Class<T> cap) Static utility method for returning an extension wrapped in aOptional
void
Load automatic extensions as defined in the classpath META-INF/services files<T> T
putExtension
(Class<T> cap, T impl) Add a new extension.<T> T
removeExtension
(Class<T> cap) Remove a capability.
-
Constructor Details
-
ExtensionSupport
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
Description copied from interface:IExtendable
Return all extension types supported- Specified by:
getExtensions
in interfaceIExtendable
-
getExtension
Description copied from interface:IExtendable
Get the requested extension if available.- Specified by:
getExtension
in interfaceIExtendable
- Returns:
- the capability object or
null
if the cability is not available
-
putExtension
Description copied from interface:IExtendable
Add a new extension.- Specified by:
putExtension
in interfaceIExtendable
- Parameters:
cap
- the extension to add- Returns:
- the added extension implementation
-
removeExtension
Description copied from interface:IExtendable
Remove a capability.- Specified by:
removeExtension
in interfaceIExtendable
- Parameters:
cap
- the capability to remove
-
getOptionalExtension
Static utility method for returning an extension wrapped in aOptional
- Parameters:
extendable
-cap
-- Returns:
- optional of requested extension
-