Class Linq4j
- java.lang.Object
-
- org.apache.calcite.linq4j.Linq4j
-
public abstract class Linq4j extends java.lang.Object
Utility and factory methods for Linq4j.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Linq4j.CartesianProductListEnumerator<E>
Enumerates over the cartesian product of the given lists, returning a list for each row.(package private) static class
Linq4j.CollectionEnumerable<T>
Collection enumerable.(package private) static class
Linq4j.CompositeEnumerable<E>
Composite enumerable.private static class
Linq4j.EnumeratorIterator<T>
Iterator that reads from an underlyingEnumerator
.(package private) static class
Linq4j.IterableEnumerable<T>
Iterable enumerable.(package private) static class
Linq4j.IterableEnumerator<T>
Iterable enumerator.(package private) static class
Linq4j.ListEnumerable<T>
List enumerable.private static class
Linq4j.ListEnumerator<V>
Enumerator optimized for random-access list.private static class
Linq4j.SingletonEnumerator<E>
Enumerator that returns one element.private static class
Linq4j.SingletonNullEnumerator<E>
Enumerator that returns one null element.
-
Field Summary
Fields Modifier and Type Field Description static QueryProvider
DEFAULT_PROVIDER
Query provider that simply executes aQueryable
by calling its enumerator method; does not attempt optimization.private static java.lang.Object
DUMMY
static Enumerable<?>
EMPTY_ENUMERABLE
private static Enumerator<java.lang.Object>
EMPTY_ENUMERATOR
-
Constructor Summary
Constructors Modifier Constructor Description private
Linq4j()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> Enumerable<T>
asEnumerable(java.lang.Iterable<T> iterable)
Adapter that converts anIterable
into anEnumerable
.static <T> Enumerable<T>
asEnumerable(java.util.Collection<T> collection)
Adapter that converts anCollection
into anEnumerable
.static <T> Enumerable<T>
asEnumerable(java.util.List<T> list)
Adapter that converts anList
into anEnumerable
.static <T> Enumerable<T>
asEnumerable(T[] ts)
Adapter that converts an array into an enumerable.static <TSource,TResult>
Enumerable<TResult>cast(java.lang.Iterable<TSource> source, java.lang.Class<TResult> clazz)
Converts the elements of a given Iterable to the specified type.private static <T> void
closeIterator(java.util.Iterator<T> iterator)
Closes an iterator, if it can be closed.static <E> Enumerable<E>
concat(java.util.List<Enumerable<E>> enumerableList)
Concatenates two or moreEnumerable
s to form a composite enumerable that contains the union of their elements.static <T> Enumerable<T>
emptyEnumerable()
Returns anEnumerable
that has no elements.static <T> Enumerator<T>
emptyEnumerator()
Returns anEnumerator
that has no elements.static <V> Enumerator<V>
enumerator(java.util.Collection<? extends V> values)
Adapter that converts a collection into an enumerator.static <T> java.util.Iterator<T>
enumeratorIterator(Enumerator<T> enumerator)
Adapter that converts an enumerator into an iterator.static <T> boolean
equals(T t0, T t1)
Deprecated.static java.lang.reflect.Method
getMethod(java.lang.String className, java.lang.String methodName, java.lang.Class... parameterTypes)
static <T> Enumerator<T>
iterableEnumerator(java.lang.Iterable<? extends T> iterable)
Adapter that converts an iterable into an enumerator.private static <V> Enumerator<V>
listEnumerator(java.util.List<? extends V> list)
static <TSource,TResult>
Enumerable<TResult>ofType(java.lang.Iterable<TSource> source, java.lang.Class<TResult> clazz)
Returns elements of a givenIterable
that are of the specified type.static <T> java.lang.Iterable<java.util.List<T>>
product(java.lang.Iterable<? extends java.lang.Iterable<T>> iterables)
Returns the cartesian product of an iterable of iterables.static <T> Enumerator<java.util.List<T>>
product(java.util.List<Enumerator<T>> enumerators)
Returns an enumerator that is the cartesian product of the given enumerators.static <T> T
requireNonNull(T o)
Deprecated.static <T> Enumerable<T>
singletonEnumerable(T element)
Returns anEnumerable
that has one element.static <T> Enumerator<T>
singletonEnumerator(T element)
Returns anEnumerator
that has one element.static <T> Enumerator<T>
singletonNullEnumerator()
Returns anEnumerator
that has one null element.static <F,E>
Enumerator<E>transform(Enumerator<F> enumerator, Function1<F,E> func)
Applies a function to each element of an Enumerator.
-
-
-
Field Detail
-
DUMMY
private static final java.lang.Object DUMMY
-
DEFAULT_PROVIDER
public static final QueryProvider DEFAULT_PROVIDER
Query provider that simply executes aQueryable
by calling its enumerator method; does not attempt optimization.
-
EMPTY_ENUMERATOR
private static final Enumerator<java.lang.Object> EMPTY_ENUMERATOR
-
EMPTY_ENUMERABLE
public static final Enumerable<?> EMPTY_ENUMERABLE
-
-
Method Detail
-
getMethod
public static java.lang.reflect.Method getMethod(java.lang.String className, java.lang.String methodName, java.lang.Class... parameterTypes)
-
enumeratorIterator
public static <T> java.util.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(java.lang.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(java.util.List<T> list)
Adapter that converts anList
into anEnumerable
.- Type Parameters:
T
- Element type- Parameters:
list
- List- Returns:
- enumerable
-
asEnumerable
public static <T> Enumerable<T> asEnumerable(java.util.Collection<T> collection)
Adapter that converts anCollection
into anEnumerable
.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(java.lang.Iterable<T> iterable)
Adapter that converts anIterable
into anEnumerable
.It uses more efficient implementations if the iterable happens to be a
Collection
or aList
.- 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(java.util.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(java.util.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 typeE
- Element type- Parameters:
enumerator
- Backing enumeratorfunc
- Transform function- Returns:
- Enumerator
-
cast
public static <TSource,TResult> Enumerable<TResult> cast(java.lang.Iterable<TSource> source, java.lang.Class<TResult> clazz)
Converts the elements of a given Iterable to 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 usingfor (... in ...)
.Since standard Java
Collection
objects implement theIterable
interface, thecast
method enables the standard query operators to be invoked on collections (includingList
andSet
) by supplying the necessary type information. For example,ArrayList
does not implementEnumerable
<F>, but you can invokeLinq4j.cast(list, Integer.class)
to convert the list of an enumerable that can be queried using the standard query operators.
If an element cannot be cast to type <TResult>, this method will throw a
ClassCastException
. To obtain only those elements that can be cast to type TResult, use theofType(java.lang.Iterable<TSource>, java.lang.Class<TResult>)
method instead.
-
ofType
public static <TSource,TResult> Enumerable<TResult> ofType(java.lang.Iterable<TSource> source, java.lang.Class<TResult> clazz)
Returns elements of a givenIterable
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 usingfor (... 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, usecast(Iterable, Class)
.Since standard Java
Collection
objects implement theIterable
interface, thecast
method enables the standard query operators to be invoked on collections (includingList
andSet
) by supplying the necessary type information. For example,ArrayList
does not implementEnumerable
<F>, but you can invokeLinq4j.ofType(list, Integer.class)
to convert the list of an enumerable that can be queried using the standard query operators.
-
singletonEnumerable
public static <T> Enumerable<T> singletonEnumerable(T element)
Returns anEnumerable
that has one element.- Type Parameters:
T
- Element type- Returns:
- Singleton enumerable
-
singletonEnumerator
public static <T> Enumerator<T> singletonEnumerator(T element)
Returns anEnumerator
that has one element.- Type Parameters:
T
- Element type- Returns:
- Singleton enumerator
-
singletonNullEnumerator
public static <T> Enumerator<T> singletonNullEnumerator()
Returns anEnumerator
that has one null element.- Type Parameters:
T
- Element type- Returns:
- Singleton enumerator
-
emptyEnumerable
public static <T> Enumerable<T> emptyEnumerable()
Returns anEnumerable
that has no elements.- Type Parameters:
T
- Element type- Returns:
- Empty enumerable
-
emptyEnumerator
public static <T> Enumerator<T> emptyEnumerator()
Returns anEnumerator
that has no elements.- Type Parameters:
T
- Element type- Returns:
- Empty enumerator
-
concat
public static <E> Enumerable<E> concat(java.util.List<Enumerable<E>> enumerableList)
Concatenates two or moreEnumerable
s 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<java.util.List<T>> product(java.util.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> java.lang.Iterable<java.util.List<T>> product(java.lang.Iterable<? extends java.lang.Iterable<T>> iterables)
Returns the cartesian product of an iterable of iterables.
-
equals
@Deprecated public static <T> boolean equals(T t0, T t1)
Deprecated.Returns whether the arguments are equal to each other.Equivalent to
Objects.equals(java.lang.Object, java.lang.Object)
in JDK 1.7 and above.
-
requireNonNull
@Deprecated public static <T> T requireNonNull(T o)
Deprecated.ThrowsNullPointerException
if argument is null, otherwise returns argument.Equivalent to
Objects.requireNonNull(T)
in JDK 1.7 and above.
-
closeIterator
private static <T> void closeIterator(java.util.Iterator<T> iterator)
Closes an iterator, if it can be closed.
-
-