Class CoreResourceFactory
java.lang.Object
org.jdrupes.builder.core.CoreResourceFactory
- All Implemented Interfaces:
ResourceFactory
A factory for creating the Core resource objects.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleanaddsMethod(Class<T> base, Class<? extends T> derived) Checks if the derived interface adds any methods to the base interface.createNarrowed(ResourceType<T> wanted, Class<I> implemented, Supplier<? extends I> supplier) Creates a narrowed resource.newResource(ResourceType<T> type, Project project, Object... args) New resource.
-
Constructor Details
-
CoreResourceFactory
public CoreResourceFactory()Instantiates a new core resource factory.
-
-
Method Details
-
createNarrowed
public static <T extends Resource, I extends Resource> Optional<T> createNarrowed(ResourceType<T> wanted, Class<I> implemented, Supplier<? extends I> supplier) Creates a narrowed resource. Given a wanted interface type, an implemented interface type and a supplier that returns an instance of the implemented type, returns an instance of the wanted type if possible.
Returning an implementation of the wanted type is possible if the following conditions are met:
-
The wanted type has no superclass (i.e. is an interface).
-
The wanted type is a subclass of the implemented type.
-
The wanted type does not add any methods to the implemented type.
The implementation uses a dynamic proxy to wrap the implemented instance together with a
ForwardingHandler, that simply forwards all invocations to the proxied object (hence the requirement that the wanted type does not add any methods to the implemented type).- Type Parameters:
T- the wanted typeI- the implemented (available) type- Parameters:
wanted- the wantedimplemented- the implemented interfacesupplier- the supplier of a class that implements I- Returns:
- an instance if possible
-
-
addsMethod
Checks if the derived interface adds any methods to the base interface.- Type Parameters:
T- the generic type- Parameters:
base- the basederived- the derived- Returns:
- true, if successful
-
newResource
public <T extends Resource> Optional<T> newResource(ResourceType<T> type, Project project, Object... args) New resource.- Specified by:
newResourcein interfaceResourceFactory- Type Parameters:
T- the generic type- Parameters:
type- the typeproject- the projectargs- the args- Returns:
- the optional
-