Class CollectionUtils

java.lang.Object
com.nomagic.utils.CollectionUtils

@OpenApiAll public class CollectionUtils extends Object
Collection related utility methods.
  • Constructor Details

    • CollectionUtils

      public CollectionUtils()
  • Method Details

    • convert

      public static <S, T, C extends Collection<T>> C convert(Collection<S> source, C target, Converter<? super S,? super T> converter)
      Convert one (source) collection into another (target) collection. Converts each source element and adds a converted element to a target collection.
      Type Parameters:
      S - source collection element type.
      T - target collection element type.
      C - target collection type.
      Parameters:
      source - source collection.
      target - target collection.
      converter - converts each element.
      Returns:
      target collection.
    • append

      public static <T> Collection<T> append(Collection<T> target, Collection<? extends T> source, boolean allowDuplication)
      Append a source to a target collection.
      Parameters:
      target - target
      source - source
      allowDuplication - do not check for an already added element if true
      Returns:
      appended collection
    • append

      public static <T> Collection<T> append(Collection<T> target, Iterator<? extends T> sourceIterator, boolean allowDuplication)
      Append a source to a target collection.
      Parameters:
      target - target
      sourceIterator - source iterator.
      allowDuplication - do not check for an already added element if true
      Returns:
      appended collection
    • append

      public static <T> Collection<T> append(Collection<T> target, Iterator<? extends T> sourceIterator, boolean allowDuplication, boolean allowNulls)
      Append a source to a target collection.
      Parameters:
      target - target
      sourceIterator - source iterator.
      allowDuplication - do not check for an already added element if true
      allowNulls - allows nulls
      Returns:
      appended collection
    • append

      public static <T> Collection<T> append(Collection<T> target, T[] source, boolean allowDuplication)
      Append source array to a target collection.
      Parameters:
      target - target
      source - source
      allowDuplication - do not check for an already added element if true
      Returns:
      appended vector
    • contains

      public static boolean contains(Iterator it, Object o)
      Checks if given iterator contains a given object.
      Parameters:
      it - the iterator.
      o - the object.
      Returns:
      true, if it contains o.
    • containsAny

      public static boolean containsAny(Collection location, Collection what)
      Returns true if location contains any element from what
      Parameters:
      location - location collection
      what - what collection.
      Returns:
      boolean.
    • createCollection

      public static <C extends Collection<E>, E> C createCollection(Class<C> collection, Iterator<E> it)
      Creates a given type collection and fills it with objects from given iterator
      Parameters:
      collection - type collection.
      it - The given iterator.
      Returns:
      the result collection.
    • notDuplicatedList

      public static <T> Collection<T> notDuplicatedList(Collection<T> collection)
      Removes duplicates from a given collection. After remove, one element will be in collection only once.
      Parameters:
      collection - collection to operate with (makes sense when a collection is a list (sets can't have identical elements)).
      Returns:
      collection.
    • createStringRepresentation

      public static <T> String createStringRepresentation(Collection<T> collection, String delimiter, Converter<T,String> converter)
      Create string representation of a given collection.
      Parameters:
      collection - collection
      delimiter - values delimiter
      converter - convert an object to string
      Returns:
      string representation of a collection
    • createFromStringRepresentation

      public static <T> Collection<T> createFromStringRepresentation(String string, String delimiter, Converter<String,T> converter)
      Create a collection from string.
      Parameters:
      string - string representation.
      delimiter - values delimiter.
      converter - convert string to object.
      Returns:
      collection.
    • leaveElements

      public static void leaveElements(List elements, int numberToLeave)
      Leaves a given number of first elements in the collection.
      Parameters:
      elements - elements to filter.
      numberToLeave - number of elements to leave.
    • mapToString

      public static String mapToString(Map<String,String> map)
      Serializes a map to string.
      Parameters:
      map - map to serialize.
      Returns:
      string representation of the map.
    • stringToMap

      public static Map<String,String> stringToMap(String input)
      Deserialize map from string.
      Parameters:
      input - input string.
      Returns:
      map.
    • filterByClassType

      public static <T> List<T> filterByClassType(Collection<?> input, Class<T> type)
      Filters and casts a collection from one type to another
      Type Parameters:
      T - type
      Parameters:
      input - input collection
      type - desired output collection type
      Returns:
      new typed list
    • filterByClassType

      public static <T> Stream<T> filterByClassType(Stream<?> stream, Class<T> type)
      Filter and map according given class.
      Parameters:
      stream - stream to filer and map
      type - desired output stream type
      Returns:
      filtered and mapped stream
    • findByClassType

      @CheckForNull public static <T> T findByClassType(Collection<?> input, Class<T> type)
      Looks for an object of give class in the given collection.
      Type Parameters:
      T - type
      Parameters:
      input - input collection
      type - desired output collection type
      Returns:
      found an object
    • anyMatch

      public static <T> boolean anyMatch(Collection<T> input, Predicate<T> predicate)
      Looks for any object matching given predicate.
      Parameters:
      input - input collection
      predicate - predicate to test
      Returns:
      true if any object in input collection matches the predicated. False if a collection is empty.
    • allMatch

      public static <T> boolean allMatch(Collection<T> input, Predicate<T> predicate)
      Check if all objects match given predicate.
      Parameters:
      input - input collection
      predicate - predicate to test
      Returns:
      true if all objects in input collection match the predicated. True if a collection is empty.
    • noneMatch

      public static <T> boolean noneMatch(Collection<T> input, Predicate<T> predicate)
      Check if no object matches given predicate.
      Parameters:
      input - input collection
      predicate - predicate to test
      Returns:
      true if input does not contain element matching predicate
    • find

      @CheckForNull public static <T> T find(Collection<T> input, Predicate<T> predicate)
      Looks for object matching given predicate.
      Parameters:
      input - input collection
      predicate - predicate to test
      Returns:
      found an object
    • find

      @CheckForNull public static <T> T find(Collection input, Class<T> castClass)
      Looks for object matching given class.
      Parameters:
      input - input collection
      castClass - class to test
      Returns:
      found an object
    • setSingleValue

      public static <T> void setSingleValue(Collection<T> collection, T value)
      Removes all values from a given collection and adds a new one.
      Parameters:
      collection - collection
      value - a new value
    • setFirstValue

      public static <T> void setFirstValue(Collection<T> collection, @CheckForNull T value)
      Set the first value of the given collection. If a collection is empty, add a new value. If the value is null, remove the first value from a collection.
      Parameters:
      collection - collection
      value - a new value
    • toOptimizedContainsCollection

      public static <T> Collection<T> toOptimizedContainsCollection(Collection<T> collection)
      Wrap the specified collection into a containable collection that ensures optimized {{Collection.contains()}} performance. The provided collection should not change while the wrapped collection is used.
      Parameters:
      collection - a collection to wrap into a containable collection
      Returns:
      return wrapped collection.
    • toOptimizedCacheCollection

      public static <T> Collection<T> toOptimizedCacheCollection(Collection<T> collection)
      Optimized for 1) minimal memory footprint, for the stored cache data to be as small as possible 2) contains() performance, while preserving iteration order and performance

      Returned collection in unmodifiable

    • equals

      public static boolean equals(Collection<?> collection1, Collection<?> collection2)
      Checks if both collections are equal - size the same and elements by order are the same
      Parameters:
      collection1 - first collection
      collection2 - second collection
      Returns:
      true if size the same and elements by order are the same
    • addIfNotNull

      public static <T> void addIfNotNull(@CheckForNull T element, Collection<T> collection)
      Adds the given element to the collection unless it is null.
      Parameters:
      element - nullable element to add
      collection - modifiable collection
    • copyAndAdd

      public static <T> List<T> copyAndAdd(@CheckForNull List<T> list, T element)
      Creates copy of the given list, or new list if null is provided. Then adds the given element and returns the copy.
      Parameters:
      list - nullable list to create copy of
      element - element to add
      Returns:
      copied and appended list
    • copyAndAdd

      public static <T> List<T> copyAndAdd(@CheckForNull List<T> list, Collection<T> toAdd)
      Creates copy of the given list, or new list if null is provided. Then adds the given collection at the end and returns the copy.
      Parameters:
      list - nullable list to create copy of
      toAdd - collection to add
      Returns:
      copied and appended list
    • startsWith

      public static <T> boolean startsWith(Collection<T> prefixCollection, Collection<T> collection)
      Checks if a collection starts with prefixCollection Example: a collection is "A.B.C", prefixCollection is "A.B", the result would be true.
      Parameters:
      prefixCollection - collection that is used to check if another collection starts with it
      collection - collection to check
      Returns:
      true if starts with prefixCollection, else false
    • listOf

      public static <T> List<T> listOf(@CheckForNull T element)
      Creates unmodifiable List with a given element. Or empty unmodifiable list if element is null.
      Type Parameters:
      T - any object
      Parameters:
      element - element
      Returns:
      list
    • listOf

      public static <T> List<T> listOf(@CheckForNull T element, List<T> list)
      Appends a given element to new unmodifiable List constructed from the given list. Returns the given list if an element is null.
      Type Parameters:
      T - any object
      Parameters:
      element - element
      list - list
      Returns:
      list
    • removeIf

      public static <T> List<T> removeIf(List<T> list, Predicate<T> predicate)
      Removes elements from the given list that don't match the predicate. Returns unmodifiable list and supports an unmodifiable list as input.
      Type Parameters:
      T - any object
      Parameters:
      list - list to operate with
      predicate - predicate
      Returns:
      list
    • checkOrder

      public static <T> boolean checkOrder(List<T> list1, List<T> list2)
      This method checks if the elements in both lists have the same order. It doesn't matter if one list is bigger than the other, but the smaller list's order should match the larger list's order.
      Type Parameters:
      T - any object
      Parameters:
      list1 - list
      list2 - list
      Returns:
      Return true if any of the lists is empty or equal or if the order of the elements is the same. Return false if the order of elements is different.
    • find

      @CheckForNull public static <T> T find(Supplier<Collection<?>> collection1, Supplier<Collection<?>> collection2, Predicate<T> predicate, Class<T> type)
      Looks for object matching given predicate in the first collection and then in the second.
      Parameters:
      collection1 - first collection
      collection2 - second collection
      predicate - predicate to test
      type - object type
      Returns:
      found an object