Class Linq4j



  • public abstract class Linq4j
    extends Object
    Utility and factory methods for Linq4j.
    • Field Detail

      • DUMMY

        private static final Object DUMMY
      • DEFAULT_PROVIDER

        public static final QueryProvider DEFAULT_PROVIDER
        Query provider that simply executes a Queryable by calling its enumerator method; does not attempt optimization.
      • EMPTY_ENUMERABLE

        public static final Enumerable<?> EMPTY_ENUMERABLE
    • Constructor Detail

      • Linq4j

        private Linq4j​()
    • Method Detail

      • enumeratorIterator

        public static <T> Iterator<T> enumeratorIterator​(Enumerator<T> enumerator)
        Adapter that converts an enumerator into an iterator.

        WARNING: The iterator returned by this method does not call Enumerator.close(), so it is not safe to use with an enumerator that allocates resources.

        Type Parameters:
        T - Element type
        Parameters:
        enumerator - Enumerator
        Returns:
        Iterator
      • iterableEnumerator

        public static <T> Enumerator<T> iterableEnumerator​(Iterable<? extends T> iterable)
        Adapter that converts an iterable into an enumerator.
        Type Parameters:
        T - Element type
        Parameters:
        iterable - Iterable
        Returns:
        enumerator
      • asEnumerable

        public static <T> Enumerable<T> asEnumerable​(List<T> list)
        Adapter that converts an List into an Enumerable.
        Type Parameters:
        T - Element type
        Parameters:
        list - List
        Returns:
        enumerable
      • asEnumerable

        public static <T> Enumerable<T> asEnumerable​(Collection<T> collection)
        Adapter that converts an Collection into an Enumerable.

        It uses more efficient implementations if the iterable happens to be a List.

        Type Parameters:
        T - Element type
        Parameters:
        collection - Collection
        Returns:
        enumerable
      • asEnumerable

        public static <T> Enumerable<T> asEnumerable​(Iterable<T> iterable)
        Adapter that converts an Iterable into an Enumerable.

        It uses more efficient implementations if the iterable happens to be a Collection or a List.

        Type Parameters:
        T - Element type
        Parameters:
        iterable - Iterable
        Returns:
        enumerable
      • asEnumerable

        public static <T> Enumerable<T> asEnumerable​(T[] ts)
        Adapter that converts an array into an enumerable.
        Type Parameters:
        T - Element type
        Parameters:
        ts - Array
        Returns:
        enumerable
      • enumerator

        public static <V> Enumerator<V> enumerator​(Collection<? extends V> values)
        Adapter that converts a collection into an enumerator.
        Type Parameters:
        V - Element type
        Parameters:
        values - Collection
        Returns:
        Enumerator over the collection
      • listEnumerator

        private static <V> Enumerator<V> listEnumerator​(List<? extends V> list)
      • transform

        public static <F,E> Enumerator<E> transform​(Enumerator<F> enumerator,
                                                    Function1<F,E> func)
        Applies a function to each element of an Enumerator.
        Type Parameters:
        F - Backing element type
        E - Element type
        Parameters:
        enumerator - Backing enumerator
        func - Transform function
        Returns:
        Enumerator
      • ofType

        public static <TSource,TResult> Enumerable<TResult> ofType​(Iterable<TSource> source,
                                                                   Class<TResult> clazz)
        Returns elements of a given Iterable that are of the specified type.

        This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its RawEnumerable.enumerator() method directly or by using for (... in ...).

        The ofType method returns only those elements in source that can be cast to type TResult. To instead receive an exception if an element cannot be cast to type TResult, use cast(Iterable, Class).

        Since standard Java Collection objects implement the Iterable interface, the cast method enables the standard query operators to be invoked on collections (including List and Set) by supplying the necessary type information. For example, ArrayList does not implement Enumerable<F>, but you can invoke

        Linq4j.ofType(list, Integer.class)

        to convert the list of an enumerable that can be queried using the standard query operators.

        See Also:
        ExtendedEnumerable.cast(Class), cast(java.lang.Iterable<TSource>, java.lang.Class<TResult>)
      • singletonEnumerable

        public static <T> Enumerable<T> singletonEnumerable​(T element)
        Returns an Enumerable that has one element.
        Type Parameters:
        T - Element type
        Returns:
        Singleton enumerable
      • singletonEnumerator

        public static <T> Enumerator<T> singletonEnumerator​(T element)
        Returns an Enumerator that has one element.
        Type Parameters:
        T - Element type
        Returns:
        Singleton enumerator
      • singletonNullEnumerator

        public static <T> Enumerator<T> singletonNullEnumerator​()
        Returns an Enumerator that has one null element.
        Type Parameters:
        T - Element type
        Returns:
        Singleton enumerator
      • emptyEnumerable

        public static <T> Enumerable<T> emptyEnumerable​()
        Returns an Enumerable that has no elements.
        Type Parameters:
        T - Element type
        Returns:
        Empty enumerable
      • emptyEnumerator

        public static <T> Enumerator<T> emptyEnumerator​()
        Returns an Enumerator that has no elements.
        Type Parameters:
        T - Element type
        Returns:
        Empty enumerator
      • concat

        public static <E> Enumerable<E> concat​(List<Enumerable<E>> enumerableList)
        Concatenates two or more Enumerables to form a composite enumerable that contains the union of their elements.
        Type Parameters:
        E - Element type
        Parameters:
        enumerableList - List of enumerable objects
        Returns:
        Composite enumerator
      • product

        public static <T> Enumerator<List<T>> product​(List<Enumerator<T>> enumerators)
        Returns an enumerator that is the cartesian product of the given enumerators.

        For example, given enumerator A that returns {"a", "b", "c"} and enumerator B that returns {"x", "y"}, product(List(A, B)) will return {List("a", "x"), List("a", "y"), List("b", "x"), List("b", "y"), List("c", "x"), List("c", "y")}.

        Notice that the cardinality of the result is the product of the cardinality of the inputs. The enumerators A and B have 3 and 2 elements respectively, and the result has 3 * 2 = 6 elements. This is always the case. In particular, if any of the enumerators is empty, the result is empty.

        Type Parameters:
        T - Element type
        Parameters:
        enumerators - List of enumerators
        Returns:
        Enumerator over the cartesian product
      • product

        public static <T> Iterable<List<T>> product​(Iterable<? extends Iterable<T>> iterables)
        Returns the cartesian product of an iterable of iterables.
      • closeIterator

        private static <T> void closeIterator​(Iterator<T> iterator)
        Closes an iterator, if it can be closed.