Class CollectionUtil

java.lang.Object
dev.metaschema.core.util.CollectionUtil

public final class CollectionUtil extends Object
Provides a collection of utilities for handling Java collections and iterators.
  • Method Details

    • toStream

      public static <T> Stream<T> toStream(@NonNull Iterator<T> iterator)
      Get a Stream for the provided Iterable.
      Type Parameters:
      T - the type to iterate on
      Parameters:
      iterator - the iterator
      Returns:
      the stream
    • toIterable

      @NonNull public static <T> Iterable<T> toIterable(@NonNull Stream<T> stream)
      Get an Iterable for the provided Stream.
      Type Parameters:
      T - the type to iterate on
      Parameters:
      stream - the stream to iterate over
      Returns:
      the resulting iterable instance
    • toIterable

      @NonNull public static <T> Iterable<T> toIterable(@NonNull Iterator<T> iterator)
      Get an Iterable for the provided Iterator.
      Type Parameters:
      T - the type to iterate on
      Parameters:
      iterator - the iterator
      Returns:
      the resulting iterable instance
    • toDescendingIterable

      @NonNull public static <T> Iterable<T> toDescendingIterable(@NonNull List<T> list)
      Get a reverse Iterable for the provided List.
      Type Parameters:
      T - the type to iterate on
      Parameters:
      list - the list of items to iterate over
      Returns:
      the resulting iterable instance
    • toList

      @NonNull public static <T> List<T> toList(Iterable<T> iterable)
      Convert the provided Iterable to a list of the same generic type.
      Type Parameters:
      T - the collection item's generic type
      Parameters:
      iterable - the Iterable to convert to a list
      Returns:
      the list
    • toList

      @NonNull public static <T> List<T> toList(Iterator<T> iterator)
      Convert the provided Iterator to a list of the same generic type.
      Type Parameters:
      T - the collection item's generic type
      Parameters:
      iterator - the Iterator to convert to a list
      Returns:
      the list
    • descendingIterator

      @NonNull public static <T> Iterator<T> descendingIterator(@NonNull List<T> list)
      Get a reverse Iterator for the provided List.
      Type Parameters:
      T - the type to iterate on
      Parameters:
      list - the list of items to iterate over
      Returns:
      the resulting Iterator instance
    • requireNonEmpty

      @NonNull public static <T extends Collection<U>, U> T requireNonEmpty(@NonNull T collection)
      Require that the provided collection contains at least a single item.
      Type Parameters:
      T - the Java type of the collection
      U - the Java type of the collection's items
      Parameters:
      collection - the collection to test
      Returns:
      the provided collection
      Throws:
      IllegalStateException - if the collection is empty
    • requireNonEmpty

      @NonNull public static <T extends Collection<U>, U> T requireNonEmpty(@NonNull T collection, @NonNull String message)
      Require that the provided collection contains at least a single item.
      Type Parameters:
      T - the Java type of the collection
      U - the Java type of the collection's items
      Parameters:
      collection - the collection to test
      message - the exception message to use if the collection is empty
      Returns:
      the provided collection
      Throws:
      IllegalStateException - if the collection is empty
    • unmodifiableCollection

      @NonNull public static <T> Collection<T> unmodifiableCollection(@NonNull Collection<T> collection)
      An implementation of Collections.unmodifiableCollection(Collection) that respects non-nullness.
      Type Parameters:
      T - the collection's item type
      Parameters:
      collection - the collection
      Returns:
      an unmodifiable view of the collection
    • singleton

      @NonNull public static <T> Set<T> singleton(@NonNull T instance)
      An implementation of Collections.singleton(Object) that respects non-nullness.
      Type Parameters:
      T - the Java type of the set items
      Parameters:
      instance - the singleton item to use
      Returns:
      an unmodifiable set containing the singleton item
    • emptySet

      @NonNull public static <T> Set<T> emptySet()
      An implementation of Collections.emptySet() that respects non-nullness.
      Type Parameters:
      T - the Java type of the set items
      Returns:
      an unmodifiable empty set
    • unmodifiableSet

      @NonNull public static <T> Set<T> unmodifiableSet(@NonNull Set<T> set)
      An implementation of Collections.unmodifiableSet(Set) that respects non-nullness.
      Type Parameters:
      T - the Java type of the set items
      Parameters:
      set - the set to prevent modification of
      Returns:
      an unmodifiable view of the set
    • listOrEmpty

      @NonNull public static <T> List<T> listOrEmpty(@Nullable List<T> list)
      Provides an unmodifiable list containing the provided list.

      If the provided list is null, an empty list will be provided.

      Type Parameters:
      T - the Java type of the list items
      Parameters:
      list - the list, which may be null
      Returns:
      an unmodifiable list containing the items
    • listOrEmpty

      @SafeVarargs @NonNull public static <T> List<T> listOrEmpty(@Nullable T... array)
      Generates a new unmodifiable list containing the provided items.

      If the provided array is null, an empty list will be provided.

      Type Parameters:
      T - the Java type of the list items
      Parameters:
      array - the array of items to use to populate the list, which may be null
      Returns:
      an unmodifiable list containing the items
    • emptyList

      @NonNull public static <T> List<T> emptyList()
      An implementation of Collections.emptyList() that respects non-nullness.
      Type Parameters:
      T - the Java type of the list items
      Returns:
      an unmodifiable empty list
    • unmodifiableList

      @NonNull public static <T> List<T> unmodifiableList(@NonNull List<T> list)
      An implementation of Collections.unmodifiableList(List) that respects non-nullness.
      Type Parameters:
      T - the Java type of the list items
      Parameters:
      list - the list to prevent modification of
      Returns:
      an unmodifiable view of the list
    • singletonList

      @NonNull public static <T> List<T> singletonList(@NonNull T instance)
      An implementation of Collections.singletonList(Object) that respects non-nullness.
      Type Parameters:
      T - the Java type of the list items
      Parameters:
      instance - the singleton item to use
      Returns:
      an unmodifiable list containing the singleton item
    • emptyMap

      @NonNull public static <K, V> Map<K,V> emptyMap()
      An implementation of Collections.emptyMap() that respects non-nullness.
      Type Parameters:
      K - the Java type of the map's keys
      V - the Java type of the map's values
      Returns:
      an unmodifiable empty map
    • singletonMap

      @NonNull public static <K, V> Map<K,V> singletonMap(@NonNull K key, @NonNull V value)
      An implementation of Collections.singletonMap(Object, Object) that respects non-nullness.
      Type Parameters:
      K - the Java type of the map's keys
      V - the Java type of the map's values
      Parameters:
      key - the singleton key
      value - the singleton value
      Returns:
      an unmodifiable map containing the singleton entry
    • unmodifiableMap

      @NonNull public static <K, V> Map<K,V> unmodifiableMap(@NonNull Map<K,V> map)
      An implementation of Collections.unmodifiableMap(Map) that respects non-nullness.
      Type Parameters:
      K - the Java type of the map's keys
      V - the Java type of the map's values
      Parameters:
      map - the map to prevent modification of
      Returns:
      an unmodifiable view of the map