Class ResourceType<T extends Resource>

java.lang.Object
org.jdrupes.builder.api.ResourceType<T>
Type Parameters:
T - the resource type

public class ResourceType<T extends Resource> extends Object

A special kind of type token for representing a resource type. The method rawType() returns the type as Class. If this class if derived from Resources, containedType() returns the ResourceType of the contained elements.

Beware of automatic inference of type arguments. The inferred type arguments will usually be super classes of what you expect.

An alternative to using an anonymous class to create a type token is to statically import the resourceType methods. Using these typically also results in clear code that is sometimes easier to read.

  • Field Details

  • Constructor Details

    • ResourceType

      public ResourceType(Class<? extends Resource> type, ResourceType<?> containedType)
      Initializes a new resource type.
      Parameters:
      type - the type
      containedType - the contained type
    • ResourceType

      protected ResourceType()
      Instantiates a new resource type, using the information from a derived class.
  • Method Details

    • resourceType

      public static <C extends Resources<?>, T extends Resource> ResourceType<C> resourceType(Class<C> type, Class<T> containedType)
      Creates a new resource type from the given container type and contained type. The common usage pattern is to import this method statically.
      Type Parameters:
      C - the generic type
      T - the generic type
      Parameters:
      type - the type
      containedType - the contained type
      Returns:
      the resource type
    • resourceType

      public static <T extends Resource> ResourceType<T> resourceType(Class<T> type)
      Creates a new resource type from the given type. The common usage pattern is to import this method statically.
      Type Parameters:
      T - the generic type
      Parameters:
      type - the type
      Returns:
      the resource type
    • getAllInterfaces

      public static Stream<Class<?>> getAllInterfaces(Class<?> clazz)
      Gets all interfaces that the given class implements, including the class itself.
      Parameters:
      clazz - the clazz
      Returns:
      all interfaces
    • rawType

      public Class<T> rawType()
      Return the type.
      Returns:
      the class
    • containedType

      Return the contained type or null, if the resource is not a container.
      Returns:
      the type
    • isAssignableFrom

      public boolean isAssignableFrom(ResourceType<?> other)
      Checks if this is assignable from the other resource type.
      Parameters:
      other - the other
      Returns:
      true, if is assignable from
    • widened

      public <R extends Resource> ResourceType<R> widened(Class<? extends Resource> type)
      Returns a new ResourceType with the type (this.type()) widened to the given type. While this method may be invoked for any ResourceType, it is intended to be used for containers (ResourceType<Resources<?>>) only.
      Type Parameters:
      R - the new raw type
      Parameters:
      type - the desired super type. This should actually be declared as Class <R>, but there is no way to specify a parameterized type as actual parameter.
      Returns:
      the new resource type
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object