com.atlassian.jira.util.collect
Class CollectionUtil

java.lang.Object
  extended by com.atlassian.jira.util.collect.CollectionUtil

public final class CollectionUtil
extends java.lang.Object


Constructor Summary
CollectionUtil()
           
 
Method Summary
static
<T> boolean
contains(java.lang.Iterable<? extends T> iterable, Predicate<T> predicate)
          Does the supplied Iterable contain anything that matches the predicate?
static
<T> boolean
contains(java.util.Iterator<? extends T> iterator, Predicate<T> predicate)
          Does the supplied Iterator contain anything that matches the predicate?
static
<T> java.util.List<T>
copyAsImmutableList(java.util.Collection<? extends T> copy)
          Return an immutable list copy of the passed collection.
static
<K,V> java.util.Map<K,V>
copyAsImmutableMap(java.util.Map<? extends K,? extends V> copy)
          Return an immutable copy of the passed map.
static
<T> java.util.Set<T>
copyAsImmutableSet(java.util.Collection<? extends T> copy)
          Return an immutable set copy of the passed collection.
static
<T> java.util.Collection<T>
filter(java.util.Collection<T> collection, Predicate<? super T> predicate)
          Create a filtered Collection.
static
<T> java.lang.Iterable<T>
filter(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
          Create a filtered Iterable.
static
<T> java.util.Iterator<T>
filter(java.util.Iterator<T> iterator, Predicate<? super T> predicate)
          Create a filtered Iterator.
static
<T,R extends T>
java.util.Collection<R>
filterByType(java.lang.Iterable<T> iterable, java.lang.Class<R> subclass)
          Filter a Collection for the specified subtype.
static
<T> T
findFirstMatch(java.lang.Iterable<? extends T> iterable, Predicate<T> predicate)
          Return the first found element that the predicate matches.
static
<T> T
first(java.lang.Iterable<? extends T> iterable)
          Get the first element of a an Iterable in iteration order, or null if empty.
static
<T> void
foreach(java.lang.Iterable<T> iterable, Consumer<T> consumer)
          For each element in the iterator, consume the contents.
static
<T> void
foreach(java.util.Iterator<? extends T> iterator, Consumer<T> consumer)
          For each element in the iterator, consume the contents.
static
<T> int
indexOf(java.lang.Iterable<? extends T> iterable, Predicate<? super T> predicate)
          Returns the index of the first element that matches the predicate.
static
<K,R,S> java.util.Map<K,S>
map(java.util.Map<K,R> map, Function<R,S> mapper)
          Take a map and eagerly transform all values into a new, immutable Map.
static
<T> java.util.List<T>
sort(java.util.Collection<? extends T> collection, java.util.Comparator<T> comparator)
          Copy and sort the passed collection and return an unmodifiable List of the elements.
static
<T> java.util.List<T>
toList(java.util.Enumeration<? extends T> enumeration)
          Turn the enumeration into a list.
static
<T> java.util.List<T>
toList(java.lang.Iterable<? extends T> iterable)
          Turn the iterable into a list.
static
<T> java.util.List<T>
toList(java.util.Iterator<? extends T> iterator)
          Turn the iterator into a list.
static
<T> java.util.Set<T>
toSet(java.lang.Iterable<? extends T> iterable)
          Turn the iterable into a Set.
static
<T> java.util.Set<T>
toSet(java.util.Iterator<? extends T> iterator)
          Turn the iterable into a Set.
static
<T,R> java.util.List<R>
transform(java.lang.Iterable<? extends T> iterable, Function<T,R> transformer)
          Return a List that is transformed from elements of the input type to elements of the output type by a transformer function.
static
<T,R> java.util.List<R>
transform(java.util.Iterator<? extends T> iterator, Function<T,R> transformer)
          Return a List that is transformed from elements of the input type to elements of the output type by a transformer function.
static
<T,R> java.lang.Iterable<R>
transformAndFilter(java.lang.Iterable<T> iterable, Function<T,R> transformer, Predicate<R> predicate)
           
static
<T,R> java.util.Iterator<R>
transformIterator(java.util.Iterator<? extends T> iterator, Function<T,R> transformer)
          Return an Iterator that is transformed from elements of the input type to elements of the output type by a transformer function.
static
<T,R> java.util.Set<R>
transformSet(java.lang.Iterable<T> iterable, Function<T,R> transformer)
          Return a Set that is transformed from elements of the input type to elements of the output type by a transformer function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtil

public CollectionUtil()
Method Detail

foreach

public static <T> void foreach(@NotNull
                               java.util.Iterator<? extends T> iterator,
                               @NotNull
                               Consumer<T> consumer)
For each element in the iterator, consume the contents.

Type Parameters:
T - the type of element to consume
Parameters:
iterator - to iterate over the elements
consumer - to consume the elements

foreach

public static <T> void foreach(java.lang.Iterable<T> iterable,
                               @NotNull
                               Consumer<T> consumer)
For each element in the iterator, consume the contents.

Type Parameters:
T - the element type
Parameters:
iterable - to iterate over the elements, may be null
consumer - to consume the elements

toList

public static <T> java.util.List<T> toList(@NotNull
                                           java.util.Iterator<? extends T> iterator)
Turn the iterator into a list.

Type Parameters:
T - the element type
Parameters:
iterator - to iterate over the elements
Returns:
an unmodifiable List of the elements in the iterator

toList

public static <T> java.util.List<T> toList(@NotNull
                                           java.lang.Iterable<? extends T> iterable)
Turn the iterable into a list.

Type Parameters:
T - the element type
Parameters:
iterable - to iterate over the elements
Returns:
an unmodifiable List of the elements in the iterator

toList

public static <T> java.util.List<T> toList(@NotNull
                                           java.util.Enumeration<? extends T> enumeration)
Turn the enumeration into a list.

Type Parameters:
T - the element type
Parameters:
enumeration - to enumerate over the elements
Returns:
an unmodifiable List of the elements in the iterator

toSet

public static <T> java.util.Set<T> toSet(@NotNull
                                         java.lang.Iterable<? extends T> iterable)
Turn the iterable into a Set.

Type Parameters:
T - the element type
Parameters:
iterable - to iterate over the elements
Returns:
an unmodifiable Set of the elements in the iterator

toSet

public static <T> java.util.Set<T> toSet(@NotNull
                                         java.util.Iterator<? extends T> iterator)
Turn the iterable into a Set.

Type Parameters:
T - the element type
Parameters:
iterator - to iterate over the elements
Returns:
an unmodifiable Set of the elements in the iterator

transform

public static <T,R> java.util.List<R> transform(@NotNull
                                                java.util.Iterator<? extends T> iterator,
                                                @NotNull
                                                Function<T,R> transformer)
Return a List that is transformed from elements of the input type to elements of the output type by a transformer function.

Type Parameters:
T - the input type
R - the out type
Parameters:
iterator - to iterate over the contents
transformer - the function that performs the transformation
Returns:
an unmodifiable List of the transformed type

transform

public static <T,R> java.util.List<R> transform(@NotNull
                                                java.lang.Iterable<? extends T> iterable,
                                                @NotNull
                                                Function<T,R> transformer)
Return a List that is transformed from elements of the input type to elements of the output type by a transformer function.

Note, this performs a copy and applies the transform to all elements. If you want a lazily applied function, see Transform

Type Parameters:
T - the input type
R - the out type
Parameters:
iterable - the contents
transformer - the function that performs the transformation
Returns:
an unmodifiable List of the transformed type

transformIterator

public static <T,R> java.util.Iterator<R> transformIterator(@NotNull
                                                            java.util.Iterator<? extends T> iterator,
                                                            @NotNull
                                                            Function<T,R> transformer)
Return an Iterator that is transformed from elements of the input type to elements of the output type by a transformer function.

Type Parameters:
T - the input type
R - the out type
Parameters:
iterator - the contents
transformer - the function that performs the transformation
Returns:
an Iterator of the transformed type

transformSet

public static <T,R> java.util.Set<R> transformSet(@NotNull
                                                  java.lang.Iterable<T> iterable,
                                                  @NotNull
                                                  Function<T,R> transformer)
Return a Set that is transformed from elements of the input type to elements of the output type by a transformer function.

Note, this performs a copy and applies the transform to all elements.

Type Parameters:
T - the input type
R - the output type
Parameters:
iterable - the contents
transformer - the function that performs the transformation
Returns:
an unmodifiable Set of the transformed type

contains

public static <T> boolean contains(@NotNull
                                   java.util.Iterator<? extends T> iterator,
                                   @NotNull
                                   Predicate<T> predicate)
Does the supplied Iterator contain anything that matches the predicate?

Type Parameters:
T - the element type
Parameters:
iterator - containing elements
predicate - the matcher
Returns:
true if the predicate returns true for any elements.

contains

public static <T> boolean contains(@NotNull
                                   java.lang.Iterable<? extends T> iterable,
                                   @NotNull
                                   Predicate<T> predicate)
Does the supplied Iterable contain anything that matches the predicate?

Type Parameters:
T - the element type
Parameters:
iterable - containing elements
predicate - the matcher
Returns:
true if the predicate returns true for any elements.

filter

public static <T> java.util.Iterator<T> filter(@NotNull
                                               java.util.Iterator<T> iterator,
                                               @NotNull
                                               Predicate<? super T> predicate)
Create a filtered Iterator.

Type Parameters:
T - the element type
Parameters:
iterator - an iterator that only returns elements approved by the predicate
predicate - for checking the elements
Returns:
a filtered Iterator

filter

public static <T> java.lang.Iterable<T> filter(@NotNull
                                               java.lang.Iterable<T> iterable,
                                               @NotNull
                                               Predicate<? super T> predicate)
Create a filtered Iterable.

Type Parameters:
T - the element type
Parameters:
iterable - an iterable whose iterators only returns elements approved by the predicate
predicate - for checking the elements
Returns:
a filtered Iterable

transformAndFilter

public static <T,R> java.lang.Iterable<R> transformAndFilter(java.lang.Iterable<T> iterable,
                                                             Function<T,R> transformer,
                                                             Predicate<R> predicate)

filter

public static <T> java.util.Collection<T> filter(@NotNull
                                                 java.util.Collection<T> collection,
                                                 @NotNull
                                                 Predicate<? super T> predicate)
Create a filtered Collection.

Type Parameters:
T - the element type
Parameters:
collection - an iterable whose iterators only returns elements approved by the predicate
predicate - for checking the elements
Returns:
a filtered Iterable

filterByType

public static <T,R extends T> java.util.Collection<R> filterByType(@NotNull
                                                                   java.lang.Iterable<T> iterable,
                                                                   @NotNull
                                                                   java.lang.Class<R> subclass)
Filter a Collection for the specified subtype.

Type Parameters:
T - the incoming type
R - the result type
Parameters:
iterable - an iterable whose values are of the source type
subclass - the result type, only return elements if they are of this type
Returns:
a filtered Collection of the subtype

sort

public static <T> java.util.List<T> sort(@NotNull
                                         java.util.Collection<? extends T> collection,
                                         @NotNull
                                         java.util.Comparator<T> comparator)
Copy and sort the passed collection and return an unmodifiable List of the elements.

Type Parameters:
T - the element type
Parameters:
collection - the collection to copy
comparator - for sorting
Returns:
an unmodifiable List view of the elements.

copyAsImmutableList

public static <T> java.util.List<T> copyAsImmutableList(@NotNull
                                                        java.util.Collection<? extends T> copy)
Return an immutable list copy of the passed collection.

Type Parameters:
T - the type of elements for the returned collection.
Parameters:
copy - the collection to copy.
Returns:
an immutable list copy of the passed collection.

copyAsImmutableSet

public static <T> java.util.Set<T> copyAsImmutableSet(@NotNull
                                                      java.util.Collection<? extends T> copy)
Return an immutable set copy of the passed collection.

Type Parameters:
T - the type of elements for the returned collection.
Parameters:
copy - the collection to copy.
Returns:
an immutable set copy of the passed collection.

copyAsImmutableMap

public static <K,V> java.util.Map<K,V> copyAsImmutableMap(@NotNull
                                                          java.util.Map<? extends K,? extends V> copy)
Return an immutable copy of the passed map. The type of the reurned map is not gaurenteed to match the type of the passed map. If this is important, than do it yourself.

Type Parameters:
K - the type of key in the returned map.
V - the type of value in the returned map.
Parameters:
copy - the map to copy.
Returns:
the copied and immutable map.

findFirstMatch

public static <T> T findFirstMatch(@NotNull
                                   java.lang.Iterable<? extends T> iterable,
                                   Predicate<T> predicate)
Return the first found element that the predicate matches.

Type Parameters:
T - the type of element to return
Parameters:
iterable - that may contain the element to return
predicate - to match the desired element
Returns:
the first matched element or null if none found

indexOf

public static <T> int indexOf(@NotNull
                              java.lang.Iterable<? extends T> iterable,
                              @NotNull
                              Predicate<? super T> predicate)
Returns the index of the first element that matches the predicate.

Type Parameters:
T - the type of the elements
Parameters:
iterable - collection of elements
predicate - to match the desired element
Returns:
the 0-based index of the first element that matches the predicate or -1 if none found

first

public static <T> T first(@NotNull
                          java.lang.Iterable<? extends T> iterable)
Get the first element of a an Iterable in iteration order, or null if empty.

Type Parameters:
T - the type
Parameters:
iterable - the thing to get something from.
Returns:
the first thing the iterator spits out. May

map

public static <K,R,S> java.util.Map<K,S> map(java.util.Map<K,R> map,
                                             Function<R,S> mapper)
Take a map and eagerly transform all values into a new, immutable Map.

Type Parameters:
K - the key type
R - the original value type
S - the new value type
Parameters:
map - the original map
mapper - the mapping function
Returns:
a new immutable map
See Also:
for a lazy version.


Copyright © 2002-2010 Atlassian. All Rights Reserved.