Class CoreResourceFactory

java.lang.Object
org.jdrupes.builder.core.CoreResourceFactory
All Implemented Interfaces:
ResourceFactory

public class CoreResourceFactory extends Object implements ResourceFactory
A factory for creating the Core resource objects.
  • Constructor Details

  • 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:

      1. The wanted type has no superclass (i.e. is an interface).

      2. The wanted type is a subclass of the implemented type.

      3. 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 type
      I - the implemented (available) type
      Parameters:
      wanted - the wanted
      implemented - the implemented interface
      supplier - the supplier of a class that implements I
      Returns:
      an instance if possible
    • addsMethod

      public static <T> boolean addsMethod(Class<T> base, Class<? extends T> derived)
      Checks if the derived interface adds any methods to the base interface.
      Type Parameters:
      T - the generic type
      Parameters:
      base - the base
      derived - 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:
      newResource in interface ResourceFactory
      Type Parameters:
      T - the generic type
      Parameters:
      type - the type
      project - the project
      args - the args
      Returns:
      the optional