com.atlassian.jira.util.collect
Class CollectionUtil

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

public final class CollectionUtil
extends Object


Constructor Summary
CollectionUtil()
           
 
Method Summary
static
<T> boolean
contains(Iterable<? extends T> iterable, Predicate<T> predicate)
          Does the supplied Iterable contain anything that matches the predicate?
static
<T> boolean
contains(Iterator<? extends T> iterator, Predicate<T> predicate)
          Does the supplied Iterator contain anything that matches the predicate?
static
<T> List<T>
copyAsImmutableList(Collection<? extends T> copy)
          Return an immutable list copy of the passed collection.
static
<K,V> Map<K,V>
copyAsImmutableMap(Map<? extends K,? extends V> copy)
          Return an immutable copy of the passed map.
static
<T> Set<T>
copyAsImmutableSet(Collection<? extends T> copy)
          Return an immutable set copy of the passed collection.
static
<T> Collection<T>
filter(Collection<T> collection, Predicate<? super T> predicate)
          Create a filtered Collection.
static
<T> Iterable<T>
filter(Iterable<T> iterable, Predicate<? super T> predicate)
          Create a filtered Iterable.
static
<T> Iterator<T>
filter(Iterator<T> iterator, Predicate<? super T> predicate)
          Create a filtered Iterator.
static
<T,R extends T>
Collection<R>
filterByType(Iterable<T> iterable, Class<R> subclass)
          Filter a Collection for the specified subtype.
static
<T> T
findFirstMatch(Iterable<? extends T> iterable, Predicate<T> predicate)
          Return the first found element that the predicate matches.
static
<T> T
first(Iterable<? extends T> iterable)
          Get the first element of a an Iterable in iteration order, or null if empty.
static
<T> void
foreach(Iterable<T> iterable, Consumer<T> consumer)
          For each element in the iterator, consume the contents.
static
<T> void
foreach(Iterator<? extends T> iterator, Consumer<T> consumer)
          For each element in the iterator, consume the contents.
static
<T> int
indexOf(Iterable<? extends T> iterable, Predicate<? super T> predicate)
          Returns the index of the first element that matches the predicate.
static
<K,R,S> Map<K,S>
map(Map<K,R> map, Function<R,S> mapper)
          Take a map and eagerly transform all values into a new, immutable Map.
static
<T> List<T>
sort(Collection<? extends T> collection, Comparator<T> comparator)
          Copy and sort the passed collection and return an unmodifiable List of the elements.
static
<T> List<T>
toList(Enumeration<? extends T> enumeration)
          Turn the enumeration into a list.
static
<T> List<T>
toList(Iterable<? extends T> iterable)
          Turn the iterable into a list.
static
<T> List<T>
toList(Iterator<? extends T> iterator)
          Turn the iterator into a list.
static
<T> Set<T>
toSet(Iterable<? extends T> iterable)
          Turn the iterable into a Set.
static
<T> Set<T>
toSet(Iterator<? extends T> iterator)
          Turn the iterable into a Set.
static
<T,R> List<R>
transform(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> List<R>
transform(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> Iterable<R>
transformAndFilter(Iterable<T> iterable, Function<T,R> transformer, Predicate<R> predicate)
           
static
<T,R> Iterator<R>
transformIterator(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> Set<R>
transformSet(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
                               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(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> List<T> toList(@NotNull
                                 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> List<T> toList(@NotNull
                                 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> List<T> toList(@NotNull
                                 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> Set<T> toSet(@NotNull
                               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> Set<T> toSet(@NotNull
                               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> List<R> transform(@NotNull
                                      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> List<R> transform(@NotNull
                                      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> Iterator<R> transformIterator(@NotNull
                                                  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> Set<R> transformSet(@NotNull
                                        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
                                   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
                                   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> Iterator<T> filter(@NotNull
                                     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> Iterable<T> filter(@NotNull
                                     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> Iterable<R> transformAndFilter(Iterable<T> iterable,
                                                   Function<T,R> transformer,
                                                   Predicate<R> predicate)

filter

public static <T> Collection<T> filter(@NotNull
                                       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> Collection<R> filterByType(@NotNull
                                                         Iterable<T> iterable,
                                                         @NotNull
                                                         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> List<T> sort(@NotNull
                               Collection<? extends T> collection,
                               @NotNull
                               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> List<T> copyAsImmutableList(@NotNull
                                              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> Set<T> copyAsImmutableSet(@NotNull
                                            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> Map<K,V> copyAsImmutableMap(@NotNull
                                                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
                                   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
                              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
                          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> Map<K,S> map(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-2012 Atlassian. All Rights Reserved.