org.fest.util
Class Collections

java.lang.Object
  extended by org.fest.util.Collections

public final class Collections
extends Object

Utility methods related to collections.

Author:
Yvonne Wang, Alex Ruiz, Joel Costigliola

Method Summary
static
<T> Collection<T>
duplicatesFrom(Collection<T> c)
          Returns any duplicate elements from the given collection.
static
<T> List<T>
filter(Collection<?> target, CollectionFilter<T> filter)
           
static String format(Collection<?> c)
          Returns the String representation of the given collection, or null if the given collection is null.
static boolean hasOnlyNullElements(Iterable<?> iterable)
          Returns true if the given Iterable has only null elements, false otherwise.
static boolean isEmpty(Iterable<?> iterable)
          Returns true if the given Iterable is null or empty.
static
<T> List<T>
list(Iterable<T> iterable)
          Creates a list containing the given Iterable elements.
static
<T> List<T>
list(T... elements)
          Creates a list containing the given elements.
static
<T> Collection<T>
nonNullElements(Collection<T> c)
          Returns a new unmodifiable collection containing the non-null elements of the given collection.
static
<T> List<T>
nonNullElements(List<T> l)
          Returns a new unmodifiable list containing the non-null elements of the given list.
static
<T> Set<T>
set(T... elements)
          Creates a set containing the given elements.
static int sizeOf(Iterable<?> iterable)
          Returns the size of the given Iterable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

list

public static <T> List<T> list(T... elements)
Creates a list containing the given elements.

Type Parameters:
T - the type of elements of the list to create.
Parameters:
elements - the elements to store in the list.
Returns:
the created list.

set

public static <T> Set<T> set(T... elements)
Creates a set containing the given elements.

Type Parameters:
T - the type of elements of the set to create.
Parameters:
elements - the elements to store in the set.
Returns:
the created set.
Since:
1.1.5

duplicatesFrom

public static <T> Collection<T> duplicatesFrom(Collection<T> c)
Returns any duplicate elements from the given collection.

Type Parameters:
T - the generic type of the given collection.
Parameters:
c - the given collection that might have duplicate elements.
Returns:
a collection containing the duplicate elements of the given one. If no duplicates are found, an empty collection is returned.

isEmpty

public static boolean isEmpty(Iterable<?> iterable)
Returns true if the given Iterable is null or empty.

Parameters:
iterable - the Iterable to check.
Returns:
true if the given Iterable is null or empty, otherwise false.

sizeOf

public static int sizeOf(Iterable<?> iterable)
Returns the size of the given Iterable.

Parameters:
iterable - the Iterable to get size.
Returns:
the size of the given Iterable..
Throws:
IllegalArgumentException - if given Iterable is null.

filter

public static <T> List<T> filter(Collection<?> target,
                                 CollectionFilter<T> filter)

format

public static String format(Collection<?> c)
Returns the String representation of the given collection, or null if the given collection is null.

Parameters:
c - the collection to format.
Returns:
the String representation of the given collection.

nonNullElements

public static <T> Collection<T> nonNullElements(Collection<T> c)
Returns a new unmodifiable collection containing the non-null elements of the given collection. This method returns an empty unmodifiable collection if the given collection has only null elements or if it is empty. This method returns null if the given collection is null.

Type Parameters:
T - the type of elements of the collection.
Parameters:
c - the collection we want to extract non null elements from.
Returns:
a new unmodifiable collection containing the non-null elements of the given collection, or null if the given collection is null.
Since:
1.1.3

nonNullElements

public static <T> List<T> nonNullElements(List<T> l)
Returns a new unmodifiable list containing the non-null elements of the given list. This method returns an empty unmodifiable list if the given list has only null elements or if it is empty. This method returns null if the given list is null.

Type Parameters:
T - the type of elements of the list.
Parameters:
l - the list we want to extract non null elements from.
Returns:
a new unmodifiable list containing the non-null elements of the given list, or null if the given list is null.
Since:
1.1.3

hasOnlyNullElements

public static boolean hasOnlyNullElements(Iterable<?> iterable)
Returns true if the given Iterable has only null elements, false otherwise. If given Iterable is empty, this method returns true.

Parameters:
iterable - the given iterable. It must not be null.
Returns:
true if the given iterable has only null elements or is empty, false otherwise.
Throws:
NullPointerException - if the given iterable is null.
Since:
1.1.3

list

public static <T> List<T> list(Iterable<T> iterable)
Creates a list containing the given Iterable elements.

Type Parameters:
T - the type of elements of the list to create.
Parameters:
iterable - the Iterable to get elements from to store in the list.
Returns:
the created list.


Copyright © 2007-2012 FEST (Fixtures for Easy Software Testing). All Rights Reserved.