Package de.xima.fc.utils
Class XIterableUtils
- java.lang.Object
-
- de.xima.fc.utils.XIterableUtils
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
XIterableUtils.Indexed<T>
Represents an item with an index.
-
Constructor Summary
Constructors Constructor Description XIterableUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> boolean
allMatch(Iterable<? extends T> items, Predicate<? super T> predicate)
Similar to thematchesAll
method from Apache Commons, but does not throw on null predicates.static <T> boolean
anyMatch(Iterable<? extends T> items, Predicate<? super T> predicate)
Similar to thematchesAny
method from Apache Commons, but does not throw on null predicates.static <T,A,C>
CcollectIterable(Iterable<? extends T> iterable, Collector<? super T,A,? extends C> collector)
Collects the elements of an iterable using a collector.static <T> T
firstOrNull(Iterable<T> iterable)
Similar to thefirst
method from Apache Commons, but returnsnull
instead of throwing if the iterable is empty.static <T> Collection<T>
iterableToCollection(Iterable<? extends T> iterable)
Collects the elements of an iterable into a collection.static <T,S extends Collection<T>>
SiterableToCollection(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> collectionSupplier)
static <T,L extends Collection<T>>
LiterableToCollection(Iterable<? extends T> iterable, Collector<? super T,?,? extends L> collectionCollector)
Collects the elements of an iterable into a collection.static <T> List<T>
iterableToList(Iterable<? extends T> iterable)
Collects the elements of an iterable into a list.static <T,S extends List<T>>
SiterableToList(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> listSupplier)
static <T,L extends List<T>>
LiterableToList(Iterable<? extends T> iterable, Collector<? super T,?,? extends L> listCollector)
Collects the elements of an iterable into a list.static <T> Set<T>
iterableToSet(Iterable<? extends T> iterable)
Collects the elements of an iterable into a set.static <T,S extends Set<T>>
SiterableToSet(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> setSupplier)
static <T,S extends Set<T>>
SiterableToSet(Iterable<? extends T> iterable, Collector<? super T,?,? extends S> setCollector)
static <T> boolean
noneMatch(Iterable<? extends T> items, Predicate<? super T> predicate)
Similar to thematchesNone
method from Apache Commons, but does not throw on null predicates.static <T> Set<T>
toSet(Iterable<? extends T> iterable)
Deprecated.UseiterableToSet(Iterable)
instead.static <T,S extends Set<T>>
StoSet(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> setSupplier)
Deprecated.UseiterableToList(Iterable, Supplier)
instead.static <T,S extends Set<T>>
StoSet(Iterable<? extends T> iterable, Collector<? super T,?,? extends S> setCollector)
Deprecated.UseiterableToSet(Iterable, Collector)
instead.static <T> Iterable<XIterableUtils.Indexed<T>>
withIndex(Iterable<T> iterable)
Wraps an iterable and adds the index to each element.static <T> Stream<XIterableUtils.Indexed<T>>
withIndexStreamed(Iterable<T> iterable)
Wraps an iterable and adds the index to each element.
-
-
-
Method Detail
-
allMatch
public static <T> boolean allMatch(Iterable<? extends T> items, Predicate<? super T> predicate)
Similar to thematchesAll
method from Apache Commons, but does not throw on null predicates. Returnstrue
if all items match the given predicate. Returnstrue
if the iterable is null or empty. A null predicate is treated as a predicate that always returnsfalse
.- Type Parameters:
T
- Type of the items in the iterable.- Parameters:
items
- Items to check.predicate
- Predicate to check.- Returns:
true
if no item exists which does not match the predicate.
-
anyMatch
public static <T> boolean anyMatch(Iterable<? extends T> items, Predicate<? super T> predicate)
Similar to thematchesAny
method from Apache Commons, but does not throw on null predicates. Returnstrue
if at least one item exists which matches the predicate. Returnsfalse
if the iterable is null or empty. A null predicate is treated as a predicate that always returnsfalse
.- Type Parameters:
T
- Type of the items in the iterable.- Parameters:
items
- Items to check.predicate
- Predicate to check.- Returns:
true
if at least one item exists which matches the predicate.
-
collectIterable
public static <T,A,C> C collectIterable(Iterable<? extends T> iterable, Collector<? super T,A,? extends C> collector)
Collects the elements of an iterable using a collector. This behaves the sames asstreamIterable(emptyIterableIfNull(iterable)).collect(collector);
but is more concise to write.- Type Parameters:
T
- Type of the items.A
- Type of the accumulator.C
- Type of the resulting container.- Parameters:
iterable
- Iterable to collect.collector
- Collector to use.- Returns:
- The result of the collection.
-
firstOrNull
public static <T> T firstOrNull(Iterable<T> iterable)
Similar to thefirst
method from Apache Commons, but returnsnull
instead of throwing if the iterable is empty.- Type Parameters:
T
- Type of the items in the iterable.- Parameters:
iterable
- Iterable to process.- Returns:
- The first item in the iterable, or
null
if either the iterable isnull
or it contains no items.
-
iterableToCollection
public static <T,S extends Collection<T>> S iterableToCollection(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> collectionSupplier)
- Type Parameters:
T
- Type of the items.S
- Type of the collection.- Parameters:
iterable
- Iterable to convert.collectionSupplier
- Supplier for the type of collection implementation to use.- Returns:
- A collection with the given elements.
-
iterableToCollection
public static <T> Collection<T> iterableToCollection(Iterable<? extends T> iterable)
Collects the elements of an iterable into a collection. No guarantees are made regarding the type of the collection. Use one of the other methods if you need a specific type.- Type Parameters:
T
- Type of the items.- Parameters:
iterable
- Iterable to convert.- Returns:
- A collection with the given elements.
-
iterableToCollection
public static <T,L extends Collection<T>> L iterableToCollection(Iterable<? extends T> iterable, Collector<? super T,?,? extends L> collectionCollector)
Collects the elements of an iterable into a collection.- Type Parameters:
T
- Type of the items.L
- Type of the collection.- Parameters:
iterable
- Iterable to convert.collectionCollector
- Collector for the collection implementation to use.- Returns:
- A collection with the given elements.
-
iterableToList
public static <T> List<T> iterableToList(Iterable<? extends T> iterable)
Collects the elements of an iterable into a list. No guarantees are made regarding the type of the list. Use one of the other methods if you need a specific type.- Type Parameters:
T
- Type of the items.- Parameters:
iterable
- Iterable to convert.- Returns:
- A list with the given elements.
-
iterableToList
public static <T,L extends List<T>> L iterableToList(Iterable<? extends T> iterable, Collector<? super T,?,? extends L> listCollector)
Collects the elements of an iterable into a list.- Type Parameters:
T
- Type of the items.L
- Type of the list.- Parameters:
iterable
- Iterable to convert.listCollector
- Collector for the list implementation to use.- Returns:
- A list with the given elements.
-
iterableToList
public static <T,S extends List<T>> S iterableToList(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> listSupplier)
- Type Parameters:
T
- Type of the items.S
- Type of the list.- Parameters:
iterable
- Iterable to convert.listSupplier
- Supplier for the type of list implementation to use.- Returns:
- A list with the given elements.
-
iterableToSet
public static <T> Set<T> iterableToSet(Iterable<? extends T> iterable)
Collects the elements of an iterable into a set. No guarantees are made regarding the type of the list. Use one of the other methods if you need a specific type.- Type Parameters:
T
- Type of the items.- Parameters:
iterable
- Iterable to convert.- Returns:
- A set with the given elements.
-
iterableToSet
public static <T,S extends Set<T>> S iterableToSet(Iterable<? extends T> iterable, Collector<? super T,?,? extends S> setCollector)
- Type Parameters:
T
- Type of the items.S
- Type of the set.- Parameters:
iterable
- Iterable to convert.setCollector
- Collector for the type of set implementation to use.- Returns:
- A set with the given elements.
-
iterableToSet
public static <T,S extends Set<T>> S iterableToSet(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> setSupplier)
- Type Parameters:
T
- Type of the items.S
- Type of the set.- Parameters:
iterable
- Iterable to convert.setSupplier
- Supplier for the type of set implementation to use.- Returns:
- A set with the given elements.
-
noneMatch
public static <T> boolean noneMatch(Iterable<? extends T> items, Predicate<? super T> predicate)
Similar to thematchesNone
method from Apache Commons, but does not throw on null predicates. Returnstrue
if no item matches the given predicate. Returnstrue
if the iterable is null or empty. A null predicate is treated as a predicate that always returnsfalse
.- Type Parameters:
T
- Type of the items in the iterable.- Parameters:
items
- Items to check.predicate
- Predicate to check.- Returns:
true
if no item exists which does not match the predicate.
-
toSet
@Deprecated public static <T> Set<T> toSet(Iterable<? extends T> iterable)
Deprecated.UseiterableToSet(Iterable)
instead.Collects the elements of an iterable into a set. No guarantees are made regarding the type of the list. Use one of the other methods if you need a specific type.- Type Parameters:
T
- Type of the items.- Parameters:
iterable
- Iterable to convert.- Returns:
- A set with the given elements.
-
toSet
@Deprecated public static <T,S extends Set<T>> S toSet(Iterable<? extends T> iterable, Collector<? super T,?,? extends S> setCollector)
Deprecated.UseiterableToSet(Iterable, Collector)
instead.- Type Parameters:
T
- Type of the items.S
- Type of the set.- Parameters:
iterable
- Iterable to convert.setCollector
- Collector for the type of set implementation to use.- Returns:
- A set with the given elements.
-
toSet
@Deprecated public static <T,S extends Set<T>> S toSet(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> setSupplier)
Deprecated.UseiterableToList(Iterable, Supplier)
instead.- Type Parameters:
T
- Type of the items.S
- Type of the set.- Parameters:
iterable
- Iterable to convert.setSupplier
- Supplier for the type of set implementation to use.- Returns:
- A set with the given elements.
-
withIndex
public static <T> Iterable<XIterableUtils.Indexed<T>> withIndex(Iterable<T> iterable)
Wraps an iterable and adds the index to each element.- Type Parameters:
T
- Type of the elements in the iterable.- Parameters:
iterable
- Iterable to wrap.- Returns:
- An iterable that iterates over the same elements, but with a 0-based index added to each element.
-
withIndexStreamed
public static <T> Stream<XIterableUtils.Indexed<T>> withIndexStreamed(Iterable<T> iterable)
Wraps an iterable and adds the index to each element.- Type Parameters:
T
- Type of the elements in the iterable.- Parameters:
iterable
- Iterable to wrap.- Returns:
- A stream that iterates over the same elements, but with a 0-based index added to each element.
-
-