Package de.renebergelt.quiterables
Interface Queriable<T>
- Type Parameters:
T- Type of elements
- All Superinterfaces:
Iterable<T>
- All Known Subinterfaces:
GroupedQueriable<T>,OrderedGroupedQueriable<T>,OrderedQueriable<T>
An Iterable which can be queried
- Author:
- René Bergelt
-
Method Summary
Modifier and TypeMethodDescriptionbooleanReturn if all elements in the enumeration fulfill the given predicateaverage()Return the average from the enumeration (Elements will be cast to java.lang.Number)average(NumberFunc<T> valFunc)Return the average from the enumeration where the value for each item is calculated by the given function<TOut> Queriable<TOut>Cast each element of the enumeration to the given type and return an enumeration of this type (This is an unchecked cast, so if any element in the enumeration cannot be cast to the given type an exception will be thrown)Return an enumeration which contains all elements of the current enumeration and all elements of the enumeration given as argumentbooleanReturns if this enumeration contains the given element (Equality is checked using Object.equals())booleancontains(T element, Equivalence<T> equalityComparer)Returns if this enumeration contains the given element (Equality is checked using the given Equivalence function)intcount()Return the amount of elements in the enumeration if it is finiteintReturn the amount of elements in the (finite) enumeration which satisfy the given predicate This is basically the same as .where(predicate).count()defaultIfEmpty(T defaultValue)Returns this enumeration itself if it contains any elements or if it is empty an enumeration only containing the given defaultElementdistinct()Returns an enumeration where each item of the enumeration appears exactly once (Equality is checked using the equals(...) method)distinct(Equivalence<T> equalityComparer)Returns an enumeration where each item of the enumeration appears exactly once (Equality is checked using the given equalityComparer)elementAt(int index)Return the element at the given position or throw NoSuchElementException if no element at this index exits (i.e. the enumeration contains less than (index+1) elements)elementAtOrDefault(int index)Return the element at the given position or return null if no element at this index exits (i.e. the enumeration contains less than (index+1) elements)elementAtOrDefault(int index, T defaultValue)Return the element at the given position or return the given default value if no element at this index exits (i.e. the enumeration contains less than (index+1) elements)Returns an iterable which contains only the elements of this enumeration which do not exist in the given Iterable (Equality is checked using the equals(...) method)except(Iterable<T> elementsToSubtract, Equivalence<T> equalityComparer)Returns an iterable which contains only the elements of this enumeration which do not exist in the given Iterable (Equality is checked using the given equalityComparer)booleanTest if at least one element of the enumeration fulfills the conditionfirst()Return the first element of the enumeration or throw NoSuchElementException if the enumeration is emptyReturn the first element of the enumeration for which the predicate is true or throw NoSuchElementException if no corresponding element existsReturn the first element of the enumeration or null if the enumeration is emptyfirstOrDefault(Predicate<T> predicate)Return the first element in the enumeration for which the predicate is true or null if no corresponding element existsfirstOrDefault(Predicate<T> predicate, T defaultValue)Return the first element in the enumeration for which the predicate is true or the given default value if no corresponding element existsfirstOrDefault(T defaultValue)Return the first element of the enumeration or the passed defaultValue if the enumeration is emptygroup(GroupFunction<T> func)Groups the elements of the enumeration according to the given grouping functiongroupSingle(SingleKeyGroupFunction<T> func)Groups the elements of the enumeration according to the given grouping function (Convenience function for grouping with only one group key element)Returns an iterable which contains the elements of this enumeration which also exist in the given Iterable (Equality is checked using the equals(...) method)intersect(Iterable<T> intersectWith, Equivalence<T> equalityComparer)Returns an iterable which contains the elements of this enumeration which also exist in the given Iterable (Equality is checked using the given equalityComparer)booleanisEmpty()Return if the enumeration is emptylast()Return the last element of the enumeration or throw NoSuchElementException if the enumeration is emptyReturn the last element of the enumeration for which the predicate is true or throw NoSuchElementException if no corresponding element existsReturn the last element of the enumeration or null if the list is emptylastOrDefault(Predicate<T> predicate)Return the last element in the enumeration for which the predicate is true or null if no corresponding element exists This is basically the same as .where(predicate).lastOrDefault()lastOrDefault(Predicate<T> predicate, T defaultValue)Return the last element in the enumeration for which the predicate is true or the given default value if no corresponding element exists This is basically the same as .where(predicate).lastOrDefault()max()Returns the maximum element of the enumeration (Elements have to implement Comparable)max(NumberFunc<T> valFunc)Return the maximum from the enumeration where the value for each item is calculated by the given functionmin()Returns the minimum element of the enumeration (Elements have to implement Comparable)min(NumberFunc<T> valFunc)Return the minimum from the enumeration where the value for each item is calculated by the given function*<TOut> Queriable<TOut>Return the elements of the enumeration which are of the requested type or can be cast to itorderBy(ItemFunc<T,Comparable> func)Order the elements of this enumeration according to the values returned by the order function<TComparable>
OrderedQueriable<T>orderBy(ItemFunc<T,TComparable> valueFunc, Comparator<TComparable> comparator)Order the elements of this enumeration according to the values returned by the value function function using the given comparatororderByDescending(ItemFunc<T,Comparable> func)Order the elements of this enumeration according the values returned by the order function in descending order<TComparable>
OrderedQueriable<T>orderByDescending(ItemFunc<T,TComparable> valueFunc, Comparator<TComparable> comparator)Order the elements of this enumeration according to the values returned by the value function function using the given comparator in descending orderreverse()Reverse the order of elements of this enumeration<TOut> Queriable<TOut>Transforms each element of the enumeration using a selector<TOut> Queriable<TOut>selectMany(Selector<T,Iterable<TOut>> selector)Transform each element of the enumeration into another enumeration and combine all results to a single listbooleansequenceEquals(Iterable<T> iterable)Checks whether this enumeration and the given iterable contain the same elements and the same position and have the same length (Equality is checked using Object.equals())booleansequenceEquals(Iterable<T> iterable, Equivalence<T> equalityComparer)Checks whether this enumeration and the given iterable contain the same elements and the same position and have the same length (Equality is checked using the given custom Equivalence comparer)single()If the iterable contains only one element return this element.If the iterable contains only one element which satisfies the condition return this element.If the iterable contains only one element return this element.singleOrDefault(Predicate<T> predicate)If the iterable contains only one element which satisfies the condition return this element.skip(int amount)Skip the given amount of elements in the list and return a Queriable which starts at the (amount+1)th element or is empty if the enumeration did not contain more than amount elementsReturn a queriable which starts at the first element of the enumeration which does not satisfy the condition, if no such element exists the returned Queriable is emptysum()Return the sum from the enumeration where the value for each item (Elements will be cast to java.lang.Number)sum(NumberFunc<T> valFunc)Return the sum from the enumeration where the value for each item is calculated by the given functiontake(int amount)Take the given amount of elements from the enumeration or take elements until the enumeration endsTake elements from the enumeration as long as they satisfy the condition or until the enumeration endsT[]Return an array which contains all elements of this enumeration (This will evaluate the whole enumeration, if it is infinite this will block forever.)toList()Return a list which contains all elements of this enumeration (This will evaluate the whole enumeration, if it is infinite this will block forever.)Constructs a map from the elements of this Queriable where each element is assigned a key<TKey, TValue>
Map<TKey,TValue>COnstructs a map from the elements of this QueriableReturn an object which allows to transform this Iterable to a primitive-type arraytoSet()Return a list which contains all elements of this enumeration but no duplicates Returns a set which contains the elements which are also returned by distinct()Return an enumeration which contains all elements of the current enumeration and all elements of the enumeration given as argument without duplicates (Equality is checked using the equals(...) method)union(Iterable<T> toUnite, Equivalence<T> equalityComparer)Return an enumeration which contains all elements of the current enumeration and all elements of the enumeration given as argument without duplicates (Equality is checked using the given equalityComparer)Return all elements from the enumeration for which the predicate holds true or return an empty list if no such elements existMethods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
isEmpty
boolean isEmpty()Return if the enumeration is empty- Returns:
- True if the enumeration is empty
-
defaultIfEmpty
Returns this enumeration itself if it contains any elements or if it is empty an enumeration only containing the given defaultElement- Parameters:
defaultValue- The element to return if the enumeration is empty- Returns:
- this enumeration or an enumeration containing defaultValue if this is empty
-
elementAt
Return the element at the given position or throw NoSuchElementException if no element at this index exits (i.e. the enumeration contains less than (index+1) elements)- Parameters:
index- Element index- Returns:
- the element at the given index
- Throws:
NoSuchElementException
-
elementAtOrDefault
Return the element at the given position or return null if no element at this index exits (i.e. the enumeration contains less than (index+1) elements)- Parameters:
index- Element index- Returns:
- the element at the given index or null if none exists
-
elementAtOrDefault
Return the element at the given position or return the given default value if no element at this index exits (i.e. the enumeration contains less than (index+1) elements)- Parameters:
index- Index of the elemt to returndefaultValue- The value to return when no element at the given index exists- Returns:
- The element at the index or defaultValue
-
sequenceEquals
Checks whether this enumeration and the given iterable contain the same elements and the same position and have the same length (Equality is checked using Object.equals())- Parameters:
iterable- The iterable to compare with- Returns:
- whether this and the given Iterable are considered equal
-
sequenceEquals
Checks whether this enumeration and the given iterable contain the same elements and the same position and have the same length (Equality is checked using the given custom Equivalence comparer)- Parameters:
iterable- The iterable to compare withequalityComparer- The comparison function to use- Returns:
- whether this and the given Iterable are considered equal
-
all
Return if all elements in the enumeration fulfill the given predicate- Parameters:
predicate- The predicate to evaluate- Returns:
- whether all elements satisfy the given predicate
-
exists
Test if at least one element of the enumeration fulfills the condition- Parameters:
predicate- Condition- Returns:
- whether at least one element satisfies the given predicate
-
contains
Returns if this enumeration contains the given element (Equality is checked using Object.equals())- Parameters:
element- The element to check for- Returns:
- whether the given element is contained in this sequence
-
contains
Returns if this enumeration contains the given element (Equality is checked using the given Equivalence function)- Parameters:
element- The element to check forequalityComparer- The function to decide equality- Returns:
- whether the given element is contained in this sequence
-
first
Return the first element of the enumeration or throw NoSuchElementException if the enumeration is empty- Returns:
- The first element
- Throws:
NoSuchElementException- Thrown if there is no element present
-
firstOrDefault
T firstOrDefault()Return the first element of the enumeration or null if the enumeration is empty- Returns:
- The first element or null
-
firstOrDefault
Return the first element of the enumeration or the passed defaultValue if the enumeration is empty- Parameters:
defaultValue- The deafult value to use- Returns:
- The first element or the given default value
-
first
Return the first element of the enumeration for which the predicate is true or throw NoSuchElementException if no corresponding element exists- Parameters:
predicate- The predicate to evaluate- Returns:
- The first element which satisfies the predicate
- Throws:
NoSuchElementException- Thrown if there is no element present which satisfies the predicate
-
firstOrDefault
Return the first element in the enumeration for which the predicate is true or null if no corresponding element exists- Parameters:
predicate- The predicate to evaluate- Returns:
- The first element which satisfies the predicate or null
-
firstOrDefault
Return the first element in the enumeration for which the predicate is true or the given default value if no corresponding element exists- Parameters:
predicate- The predicate to evaluatedefaultValue- The deafult value to use- Returns:
- The first element which satisfies the predicate or defaultValue
-
last
Return the last element of the enumeration or throw NoSuchElementException if the enumeration is empty- Returns:
- the last element of this sequence
- Throws:
NoSuchElementException- Thrown if there is no element present which satisfies the predicate
-
lastOrDefault
T lastOrDefault()Return the last element of the enumeration or null if the list is empty- Returns:
- the last element of this sequence or null
-
last
Return the last element of the enumeration for which the predicate is true or throw NoSuchElementException if no corresponding element exists- Parameters:
predicate- The predicate to evaluate- Returns:
- The last element which satisfies the predicate or defaultValue
- Throws:
NoSuchElementException- Thrown if there is no element present which satisfies the predicate
-
lastOrDefault
Return the last element in the enumeration for which the predicate is true or null if no corresponding element exists This is basically the same as .where(predicate).lastOrDefault()- Parameters:
predicate- The predicate to evaluate- Returns:
- The last element which satisfies the predicate or null
-
lastOrDefault
Return the last element in the enumeration for which the predicate is true or the given default value if no corresponding element exists This is basically the same as .where(predicate).lastOrDefault()- Parameters:
predicate- The predicate to evaluatedefaultValue- The deafult value to use- Returns:
- The last element which satisfies the predicate or defaultValue
-
single
If the iterable contains only one element return this element. Otherwise throw NoSuchElementException (i.e. if list is empty or contains more than 1 element)- Returns:
- the only element in this sequence
- Throws:
NoSuchElementException- Thrown if there is not exactly one element present
-
single
If the iterable contains only one element which satisfies the condition return this element. Otherwise throw NoSuchElementException (i.e. if no or multiple elements satisfies the condition)- Parameters:
predicate- The predicate to evaluate- Returns:
- the only element in this sequence which fulfills the predicate
- Throws:
NoSuchElementException- Thrown if there is not exactly one element which satisfies the predicate
-
singleOrDefault
T singleOrDefault()If the iterable contains only one element return this element. Otherwise return null (i.e. if list is empty or contains more than 1 element)- Returns:
- the only element in this sequence or null
-
singleOrDefault
If the iterable contains only one element which satisfies the condition return this element. Otherwise return null (i.e. if no or multiple elements satisfies the condition)- Parameters:
predicate- The predicate to evaluate- Returns:
- the only element in this sequence which fulfills the predicate or null
-
count
int count()Return the amount of elements in the enumeration if it is finite- Returns:
- Count of elements in this sequence
-
count
Return the amount of elements in the (finite) enumeration which satisfy the given predicate This is basically the same as .where(predicate).count()- Parameters:
predicate- The predicate to evaluate- Returns:
- Count of elements in this sequence
-
max
T max()Returns the maximum element of the enumeration (Elements have to implement Comparable)- Returns:
- the maximum element
- Throws:
IllegalStateException- if there are no elements
-
max
Return the maximum from the enumeration where the value for each item is calculated by the given function- Parameters:
valFunc- The function to calculate the value which shall be maximized for a single list item, the return value must always be of a single type (so for one list all return values for instance have to be either integers or doubles, but mixing is not possible)- Returns:
- the maximum value or null if the list is empty
- Throws:
IllegalStateException- if there are no elements
-
min
T min()Returns the minimum element of the enumeration (Elements have to implement Comparable)- Returns:
- the minimum element
- Throws:
IllegalStateException- if there are no elements
-
min
Return the minimum from the enumeration where the value for each item is calculated by the given function*- Parameters:
valFunc- The function to calculate the value which shall be minimized for a single list item, the return value must always be of a single type (so for one list all return values for instance have to be either integers or doubles, but mixing is not possible)- Returns:
- the minimum value
- Throws:
IllegalStateException- if there are no elements
-
average
Number average()Return the average from the enumeration (Elements will be cast to java.lang.Number)- Returns:
- the average value
- Throws:
IllegalStateException- if there are no elements
-
average
Return the average from the enumeration where the value for each item is calculated by the given function- Parameters:
valFunc- The function to calculate the values which shall be averagem, the return value must always be of a single type (so for one list all return values for instance have to be either integers or doubles, but mixing is not possible)- Returns:
- the average value
- Throws:
IllegalStateException- if there are no elements
-
sum
Number sum()Return the sum from the enumeration where the value for each item (Elements will be cast to java.lang.Number)- Returns:
- the sum or 0 (integer zero) if the list is empty
-
sum
Return the sum from the enumeration where the value for each item is calculated by the given function- Parameters:
valFunc- The function to calculate the values which shall be summed up for a single list item, the return value must always be of a single type (so for one list all return values for instance have to either integers or doubles, but mixing is not possible)- Returns:
- the sum or 0 (integer zero) if the list is empty
-
where
Return all elements from the enumeration for which the predicate holds true or return an empty list if no such elements exist- Parameters:
predicate- The predicate to evaluate- Returns:
- A queriable sequence of the elements which satisfy the given predicate
-
select
Transforms each element of the enumeration using a selector- Type Parameters:
TOut- The target type of the selection- Parameters:
selector- The transformation function- Returns:
- A queriable sequence of the transformed elements
-
selectMany
Transform each element of the enumeration into another enumeration and combine all results to a single list- Type Parameters:
TOut- The target type of the selection- Parameters:
selector- The transformation function- Returns:
- A queriable sequence of the transformed elements
-
cast
Cast each element of the enumeration to the given type and return an enumeration of this type (This is an unchecked cast, so if any element in the enumeration cannot be cast to the given type an exception will be thrown)- Type Parameters:
TOut- The target type of the selection (equals targetType)- Parameters:
targetType- The type to cast to- Returns:
- A queriable sequence of type-cast elements
-
ofType
Return the elements of the enumeration which are of the requested type or can be cast to it- Type Parameters:
TOut- The target type of the filtering (equals targetType)- Parameters:
targetType- The type to filter- Returns:
- A queriable sequence of elements of the given type
-
concat
Return an enumeration which contains all elements of the current enumeration and all elements of the enumeration given as argument- Parameters:
toConcat- Elements to concatenate- Returns:
- A queriable sequence which contains the elements of this sequence and of the elements from toConcat
-
union
Return an enumeration which contains all elements of the current enumeration and all elements of the enumeration given as argument without duplicates (Equality is checked using the equals(...) method)- Parameters:
toUnite- Elements to unite with- Returns:
- A queriable sequence which contains the union of this sequence and the given Iterable
-
union
Return an enumeration which contains all elements of the current enumeration and all elements of the enumeration given as argument without duplicates (Equality is checked using the given equalityComparer)- Parameters:
toUnite- Elements to unite withequalityComparer- Comparer to determine if two elements are equal- Returns:
- A queriable sequence which contains the union of this sequence and the given Iterable
-
intersect
Returns an iterable which contains the elements of this enumeration which also exist in the given Iterable (Equality is checked using the equals(...) method)- Parameters:
intersectWith- Elements to intersect with- Returns:
- A queriable sequence which contains the intersection of this sequence and the given Iterable
-
intersect
Returns an iterable which contains the elements of this enumeration which also exist in the given Iterable (Equality is checked using the given equalityComparer)- Parameters:
intersectWith- Elements to intersect withequalityComparer- Comparer to determine if two elements are equal- Returns:
- A queriable sequence which contains the union of this sequence and the given Iterable
-
except
Returns an iterable which contains only the elements of this enumeration which do not exist in the given Iterable (Equality is checked using the equals(...) method)- Parameters:
elementsToSubtract- Elements to exclude- Returns:
- A queriable sequence which contains only elements of this sequence which are not containedin the given Iterable
-
except
Returns an iterable which contains only the elements of this enumeration which do not exist in the given Iterable (Equality is checked using the given equalityComparer)- Parameters:
elementsToSubtract- Elements to excludeequalityComparer- Comparer to determine if two elements are equal- Returns:
- A queriable sequence which contains only elements of this sequence which are not containedin the given Iterable
-
distinct
Returns an enumeration where each item of the enumeration appears exactly once (Equality is checked using the equals(...) method)- Returns:
- all elements of this sequence without duplicates
-
distinct
Returns an enumeration where each item of the enumeration appears exactly once (Equality is checked using the given equalityComparer)- Parameters:
equalityComparer- Comparer to determine if two elements are equal- Returns:
- all elements of this sequence without duplicates
-
take
Take the given amount of elements from the enumeration or take elements until the enumeration ends- Parameters:
amount- max. amount of elements to take- Returns:
- The first 'amount' of elements of this sequence (or less if fewer elements are present)
-
takeWhile
Take elements from the enumeration as long as they satisfy the condition or until the enumeration ends- Parameters:
condition- Condition to take an element- Returns:
- The taken elements
-
skip
Skip the given amount of elements in the list and return a Queriable which starts at the (amount+1)th element or is empty if the enumeration did not contain more than amount elements- Parameters:
amount- Number of elements to skip- Returns:
- The elements of this sequence after the first 'amount' elements
-
skipWhile
Return a queriable which starts at the first element of the enumeration which does not satisfy the condition, if no such element exists the returned Queriable is empty- Parameters:
condition- skip condition- Returns:
- The elements after the skipped elements
-
group
Groups the elements of the enumeration according to the given grouping function- Parameters:
func- Function to group elements by- Returns:
- The grouped elements
-
groupSingle
Groups the elements of the enumeration according to the given grouping function (Convenience function for grouping with only one group key element)- Parameters:
func- Function to group elements by- Returns:
- The grouped elements
-
orderBy
Order the elements of this enumeration according to the values returned by the order function- Parameters:
func- Function to retrieve the value to compare from an element- Returns:
- The ordered elements
-
orderBy
<TComparable> OrderedQueriable<T> orderBy(ItemFunc<T,TComparable> valueFunc, Comparator<TComparable> comparator)Order the elements of this enumeration according to the values returned by the value function function using the given comparator- Type Parameters:
TComparable- The type to compare with- Parameters:
valueFunc- Function to retrieve the value to compare from an elementcomparator- Comparator to compare values- Returns:
- The ordered elements
-
orderByDescending
Order the elements of this enumeration according the values returned by the order function in descending order- Parameters:
func- Function to retrieve the value to compare from an element- Returns:
- The ordered elements
-
orderByDescending
<TComparable> OrderedQueriable<T> orderByDescending(ItemFunc<T,TComparable> valueFunc, Comparator<TComparable> comparator)Order the elements of this enumeration according to the values returned by the value function function using the given comparator in descending order- Type Parameters:
TComparable- The type to compare with- Parameters:
valueFunc- Function to retrieve the value to compare from an elementcomparator- Comparator to compare values- Returns:
- The ordered elements
-
reverse
Reverse the order of elements of this enumeration- Returns:
- A reversed sequence
-
toList
Return a list which contains all elements of this enumeration (This will evaluate the whole enumeration, if it is infinite this will block forever.)- Returns:
- List of the elements ot this sequence
-
toSet
Return a list which contains all elements of this enumeration but no duplicates Returns a set which contains the elements which are also returned by distinct()- Returns:
- Set of the elements ot this sequence
-
toMap
COnstructs a map from the elements of this Queriable- Type Parameters:
TKey- The type of the map keysTValue- The type of the map values- Parameters:
keyFunc- The function to retrieve the map key from an elementvalueFunc- The function to retrieve the map value from an element- Returns:
- The map
-
toMap
Constructs a map from the elements of this Queriable where each element is assigned a key- Type Parameters:
TKey- The type of the map keys- Parameters:
keyFunc- The function to retrieve the map key from an element*- Returns:
- The map
-
toArray
Return an array which contains all elements of this enumeration (This will evaluate the whole enumeration, if it is infinite this will block forever.)- Parameters:
classType- The class type of T- Returns:
- An array of the elements ot this sequence
-
toPrimitiveArray
PrimitiveArrayTransformer<T> toPrimitiveArray()Return an object which allows to transform this Iterable to a primitive-type array- Returns:
- An instance of PrimitiveArrayTransformer
-