Class CollectionUtils

java.lang.Object
org.openl.util.CollectionUtils

public class CollectionUtils extends Object
An util class for collections and arrays.
Author:
Yury Molchan
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Defines a functor interface implemented by classes that map one object into another.
    static interface 
    Defines a functor interface implemented by classes that perform a predicate test on an object.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> List<T>
    findAll(Iterable<T> col, CollectionUtils.Predicate<? super T> predicate)
    Selects all elements from input collection which match the given predicate into an output collection.
    static <T> T
    findFirst(Iterable<T> col, CollectionUtils.Predicate<? super T> predicate)
    Finds the first element in the given collection which matches the given predicate.
    static <T> boolean
    hasNull(T[] array)
    Checks an array on null value.
    static boolean
    Return true if a collection is null or is empty.
    static boolean
    isEmpty(Map<?,?> map)
    Return true if a map is null or is empty.
    static <T> boolean
    isEmpty(T array)
    Return true if an array is null or is empty.
    static <T> boolean
    isEmpty(T[] array)
    Return true if an array is null or is empty.
    static boolean
    Return true if a collection contains at least one element.
    static boolean
    isNotEmpty(Map<?,?> map)
    Return true if a map contains at least one element.
    static <T> boolean
    isNotEmpty(T array)
    Return true if an array contains at least one element.
    static <T> boolean
    isNotEmpty(T[] array)
    Return true if an array contains at least one element.
    static <I, O> List<O>
    map(Iterable<I> col, CollectionUtils.Mapper<? super I,? extends O> mapper)
    Returns a new Collection consisting of the elements of the input collection transformed by the given transformer.
    static Object
    toArray(Collection<?> list, Class<?> instanceClass)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CollectionUtils

      public CollectionUtils()
  • Method Details

    • isEmpty

      public static boolean isEmpty(Collection<?> col)
      Return true if a collection is null or is empty.
      Parameters:
      col - the checked collection.
      Returns:
      return true if the collection does not contain any elements.
      See Also:
    • isNotEmpty

      public static boolean isNotEmpty(Collection<?> col)
      Return true if a collection contains at least one element. This method is inverse to isEmpty(Collection).
      Parameters:
      col - the checked collection.
      Returns:
      true if the collection contains at least one element.
    • isEmpty

      public static boolean isEmpty(Map<?,?> map)
      Return true if a map is null or is empty.
      Parameters:
      map - the checked map.
      Returns:
      return true if the map does not contain any elements.
      See Also:
    • isNotEmpty

      public static boolean isNotEmpty(Map<?,?> map)
      Return true if a map contains at least one element. This method is inverse to isEmpty(Map).
      Parameters:
      map - the checked map.
      Returns:
      true if the map contains at least one element.
    • isEmpty

      public static <T> boolean isEmpty(T[] array)
      Return true if an array is null or is empty.
      Parameters:
      array - the checked array.
      Returns:
      return true if the array does not contain any elements.
    • isNotEmpty

      public static <T> boolean isNotEmpty(T[] array)
      Return true if an array contains at least one element. This method is inverse to
      invalid reference
      #isEmpty(T[])
      .
      Parameters:
      array - the checked array.
      Returns:
      true if the array contains at least one element.
    • isEmpty

      public static <T> boolean isEmpty(T array)
      Return true if an array is null or is empty.
      Parameters:
      array - the checked array.
      Returns:
      return true if the array does not contain any elements.
      Throws:
      IllegalArgumentException - if the argument is not an array.
      See Also:
    • isNotEmpty

      public static <T> boolean isNotEmpty(T array)
      Return true if an array contains at least one element. This method is inverse to
      invalid reference
      #isEmpty(T[])
      .
      Parameters:
      array - the checked array.
      Returns:
      true if the array contains at least one element.
      Throws:
      IllegalArgumentException - if the argument is not an array.
    • map

      public static <I, O> List<O> map(Iterable<I> col, CollectionUtils.Mapper<? super I,? extends O> mapper)
      Returns a new Collection consisting of the elements of the input collection transformed by the given transformer.

      Type Parameters:
      I - the type of object in the input collection.
      O - the type of object in the output collection.
      Parameters:
      col - the collection to get the input from, may be null.
      mapper - the mapper to use.
      Returns:
      the transformed result (new list).
      Throws:
      NullPointerException - if the mapper is null.
    • findFirst

      public static <T> T findFirst(Iterable<T> col, CollectionUtils.Predicate<? super T> predicate)
      Finds the first element in the given collection which matches the given predicate.

      Type Parameters:
      T - the type of object the Iterable contains.
      Parameters:
      col - the collection to search, may be null.
      predicate - the predicate to use.
      Returns:
      the first element of the collection which matches the predicate or null if none could be found.
      Throws:
      NullPointerException - if the predicate is null.
    • findAll

      public static <T> List<T> findAll(Iterable<T> col, CollectionUtils.Predicate<? super T> predicate)
      Selects all elements from input collection which match the given predicate into an output collection.

      Type Parameters:
      T - the type of object the Iterable contains.
      Parameters:
      col - the collection to search, may be null.
      predicate - the predicate to use.
      Returns:
      the all the elements of the collection which matches the predicate or [] if none could be found or null if the input collection is null.
      Throws:
      NullPointerException - if the predicate is null.
    • hasNull

      public static <T> boolean hasNull(T[] array)
      Checks an array on null value.
      Type Parameters:
      T - the element type of checked array.
      Parameters:
      array - the checked array.
      Returns:
      true if the array contains null.
    • toArray

      public static Object toArray(Collection<?> list, Class<?> instanceClass)