Package org.fest.util

Class Collections

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

public final class Collections extends Object
Understands utility methods related to collections.
  • Constructor Details

    • Collections

      private Collections()
  • Method Details

    • 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:
      a list containing the given elements.
    • 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(Collection<?> c)
      Returns true if the given collection is null or empty.
      Parameters:
      c - the collection to check.
      Returns:
      true if the given collection is null or empty, otherwise false.
    • 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(Collection<?> c)
      Returns true if the given collection has only null elements, false otherwise. If given collection is empty, this method returns true.
      Parameters:
      c - the given collection. It must not be null.
      Returns:
      true if the given collection has only null elements or is empty, false otherwise.
      Throws:
      NullPointerException - if the given collection is null.
      Since:
      1.1.3