Class InheritanceQuery<X>

  • Type Parameters:
    X - lower bound of the searched types
    All Implemented Interfaces:
    Iterable<Class<? super X>>

    public abstract class InheritanceQuery<X>
    extends Object
    Iterable-like container that allows access to supertypes of a class.

    This of course only lists types that are actually defined (transitively) by initially-provided class. Altrough wildcard types, type variables might be supertype or supertype of specified type, they are not listed.

    Instances of this class are immutable, each filtering produces new, modified instance. To obtain query for specific class, use of(java.lang.Class<X>).

    To obtain results either iterate this class with Iterable.iterator() (or in enhanced-for loop) or use one of stream(), unique(), option() or isPresent().

    Example usage, which registers all interfaces that DatabaseService implements that are annotated by Remote

         InheritanceQuery.of(DatabaseService.class)
             .annotatedWith(Remote.class)
             .onlyInterfaces()
             .stream()
             .forEach(this::register);
     
    • Method Detail

      • of

        public static <X> InheritanceQuery<X> of​(Class<X> type)
        Creates unrestricted query that will list all the supertypes that this class or interface extends/implements.
        Type Parameters:
        X - upper subtype of all results
        Parameters:
        type - class to start search with
        Returns:
        new, unrestricted inheritance query
      • annotatedWith

        public InheritanceQuery<X> annotatedWith​(Class<? extends Annotation> annotationClass)
        Creates query which lists the same classes as this one, but only if they have an annotation with provided class.
        Parameters:
        annotationClass - annotation class that will be used
        Returns:
        query filtered for classes annotated with specified class
      • annotatedWith

        public InheritanceQuery<X> annotatedWith​(AnnotationFilter annotationFilter)
        Creates query which lists the same classes as this one, but only if they have an annotation that matches specified filter.
        Parameters:
        annotationFilter - annotation filter that will be used
        Returns:
        query filtered for classes annotated with specific properties
      • filter

        public InheritanceQuery<X> filter​(Predicate<? super Class<? super X>> filter)
        Filter query elements by arbitrary predicate.

        Immutability warning: queries are assumed as immutable, but this method allows providing any Predicate, which will be held in resulting query. Therefore, if stateful predicate is provided, immutability of resulting query will be compromised. However, this is rarely the case, because most of the filters provided will be non-capturing lambdas or static method references.

        Parameters:
        filter - predicate that will determine if this query will contain element
        Returns:
        query returning the same elements as this one, but only if they match the predicate
      • upToExcluding

        public InheritanceQuery<X> upToExcluding​(Class<? super X> supertype)
        Create query which lists the same classes as this one, but only if they are subtypes of provided type, and are not this type.
        Parameters:
        supertype - supertype of the filtered classes
        Returns:
        query filtered for subtypes of specified type, excluding it
      • upToIncluding

        public InheritanceQuery<X> upToIncluding​(Class<? super X> supertype)
        Create query which lists the same classes as this one, but only if they are subtypes of provided type.

        Note that each type is its own subtype, so if supertype is actually supertype of initial class or interface, it will be included in results.

        Parameters:
        supertype - supertype of the filtered classes
        Returns:
        query filtered for subtypes of specified type, including it
      • onlyInterfaces

        public InheritanceQuery<X> onlyInterfaces()
        Creates query which lists the same classes as this one, but only if they are actually an interface.
        Returns:
        query filtered for interfaces
      • onlyClasses

        public InheritanceQuery<X> onlyClasses()
        Creates query which lists the same classes as this one, but only if they are actually a class (not an interface).
        Returns:
        query filtered for classes
      • contains

        public boolean contains​(@CompatibleWith("E")
                                Object candidate)
        Checks if provided object is in elements that would be returned by this query.

        This is often faster than checking by iteration, and most queries in library override this method, but this is not guaranteed, as it might not be possible.

        Parameters:
        candidate - object to check if it is contained in this query.
        Returns:
        if object would be returned by the query
      • stream

        public abstract Stream<E> stream()
        Adapts this query to a Stream.
        Returns:
        stream with elements of this query
      • unique

        public final E unique()
        Returns single element that this query contains.
        Returns:
        single element matches by this query
        Throws:
        NoSuchElementException - if this query didn't contain any element
        IllegalArgumentException - if the query contains multiple elements
      • isPresent

        public boolean isPresent()
        Checks if any element was contained in query.
        Returns:
        true if at least one element is present in this query
      • option

        public final Optional<E> option()
        Fetches only element present in this query, or empty optional.
        Returns:
        single element matched by this query, or empty optional.
        Throws:
        IllegalArgumentException - if the query contains multiple elements