Class ImmutableCollections

java.lang.Object
org.djutils.immutablecollections.ImmutableCollections

public final class ImmutableCollections extends Object
Static methods operating on immutable collections, or a mix of an immutable collection and a mutable collection.

Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.

Author:
Alexander Verbraeck, Peter Knoppers
  • Method Details

    • emptyImmutableSet

      public static <T> ImmutableSet<T> emptyImmutableSet()
      Return an immutable empty set.
      Type Parameters:
      T - the data type of the ImmutableSet
      Returns:
      ImmutableSet<T>; an immutable empty set
    • emptyImmutableMap

      public static <T, V> ImmutableMap<T,V> emptyImmutableMap()
      Return an immutable empty map.
      Type Parameters:
      T - the key type of the ImmutableMap
      V - the value type of the ImmutableMap
      Returns:
      ImmutableMap<T, V>; an immutable empty map
    • emptyImmutableList

      public static <T> ImmutableList<T> emptyImmutableList()
      Return an immutable empty list.
      Type Parameters:
      T - the data type of the ImmutableList
      Returns:
      ImmutableList<T>; an immutable empty list
    • binarySearch

      public static <T> int binarySearch(ImmutableList<? extends Comparable<? super T>> il, T key)
      Search the immutable list for the specified object using the binary search algorithm. The list must be ordered according to the natural ordering of its elements.
      Type Parameters:
      T - the data type of the ImmutableList
      Parameters:
      il - ImmutableList<? extends Comparable<? super T>>; the list (must be ordered according to the natural ordering of <code>T</code>
      key - T; the element to search for.
      Returns:
      int; if key is present in the list, the index of key, or, when key is not present in the list (-(insertion point) - 1) where insertion point is the index of key if it were contained in the list.
    • binarySearch

      public static <T> int binarySearch(ImmutableList<? extends T> il, T key, Comparator<? super T> comparator)
      Search the immutable list for the specified object using the binary search algorithm. The list must be ordered according to the comparator.
      Type Parameters:
      T - the object type in the list
      Parameters:
      il - ImmutableList<? extends T>; the list (must be ordered according to the <code>comparator</code>
      key - T; the element to search for.
      comparator - Comparator<? super T>; a comparator for T
      Returns:
      int; if key is present in the list, the index of key, or, when key is not present in the list (-(insertion point) - 1) where insertion point is the index of key if it were contained in the list.
    • disjoint

      public static boolean disjoint(ImmutableCollection<?> ic1, ImmutableCollection<?> ic2)
      Determine if two immutable collections have no common members.
      Parameters:
      ic1 - ImmutableCollection<?>; ImmutableCollection<?> one immutable collection
      ic2 - ImmutableCollection<?>; ImmutableCollection<?> another immutable collection
      Returns:
      boolean; true if the collections have no common members; false if the collections have at least one member in common
    • disjoint

      public static boolean disjoint(ImmutableCollection<?> ic1, Collection<?> c2)
      Determine if an immutable collection and a (non immutable) collection have no common members.
      Parameters:
      ic1 - ImmutableCollection<?>; ImmutableCollection<?> an immutable collection
      c2 - Collection<?>; Collection<?> a (mutable) collection
      Returns:
      boolean; true if the collections have no common members; false if the collections have at least one member in common
    • disjoint

      public static boolean disjoint(Collection<?> c1, ImmutableCollection<?> ic2)
      Determine if an immutable collection and a (non immutable) collection have no common members.
      Parameters:
      c1 - Collection<?>; Collection<?> a (mutable) collection
      ic2 - ImmutableCollection<?>; ImmutableCollection<?> an immutable collection
      Returns:
      boolean; true if the collections have no common members; false if the collections have at least one member in common
    • indexOfSubList

      public static int indexOfSubList(ImmutableList<?> source, ImmutableList<?> target)
      Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
      Parameters:
      source - ImmutableList<?>; the list in which to find the first occurrence of target
      target - ImmutableList<?>; the pattern to find in source
      Returns:
      int; the index in source of the first occurrence of target or -1 if target does not occur anywhere in source
    • indexOfSubList

      public static int indexOfSubList(ImmutableList<?> source, List<?> target)
      Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
      Parameters:
      source - ImmutableList<?>; the list in which to find the first occurrence of target
      target - List<?>; the pattern to find in source
      Returns:
      int; the index in source of the first occurrence of target or -1 if target does not occur anywhere in source
    • indexOfSubList

      public static int indexOfSubList(List<?> source, ImmutableList<?> target)
      Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
      Parameters:
      source - List<?>; the list in which to find the first occurrence of target
      target - ImmutableList<?>; the pattern to find in source
      Returns:
      int; the index in source of the first occurrence of target or -1 if target does not occur anywhere in source
    • lastIndexOfSubList

      public static int lastIndexOfSubList(ImmutableList<?> source, ImmutableList<?> target)
      Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
      Parameters:
      source - ImmutableList<?>; the list in which to find the last occurrence of target
      target - ImmutableList<?>; the pattern to find in source
      Returns:
      int; the index in source of the last occurrence of target or -1 if target does not occur anywhere in source
    • lastIndexOfSubList

      public static int lastIndexOfSubList(ImmutableList<?> source, List<?> target)
      Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
      Parameters:
      source - ImmutableList<?>; the list in which to find the last occurrence of target
      target - List<?>; the pattern to find in source
      Returns:
      int; the index in source of the last occurrence of target or -1 if target does not occur anywhere in source
    • lastIndexOfSubList

      public static int lastIndexOfSubList(List<?> source, ImmutableList<?> target)
      Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
      Parameters:
      source - List<?>; the list in which to find the last occurrence of target
      target - ImmutableList<?>; the pattern to find in source
      Returns:
      int; the index in source of the last occurrence of target or -1 if target does not occur anywhere in source
    • max

      public static <T extends Object & Comparable<? super T>> T max(ImmutableCollection<? extends T> ic)
      Returns the maximum element of an immutable collection according to the natural ordering of its elements.
      Type Parameters:
      T - the object type in the collection
      Parameters:
      ic - ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collection
      Returns:
      T <T extends Object & Comparable<? super T>>; the maximum element in the immutable collection
      Throws:
      NoSuchElementException - if the immutable collection is empty.
    • max

      public static <T> T max(ImmutableCollection<? extends T> ic, Comparator<? super T> comparator)
      Returns the maximum element of an immutable collection according to the natural ordering of its elements.
      Type Parameters:
      T - the object type in the collection
      Parameters:
      ic - ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collection
      comparator - Comparator<? super T>; a comparator for T
      Returns:
      T <T extends Object & Comparable<? super T>>; the maximum element in the immutable collection
      Throws:
      NoSuchElementException - if the immutable collection is empty.
    • min

      public static <T extends Object & Comparable<? super T>> T min(ImmutableCollection<? extends T> ic)
      Returns the minimum element of an immutable collection according to the natural ordering of its elements.
      Type Parameters:
      T - the object type in the collection
      Parameters:
      ic - ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collection
      Returns:
      T <T extends Object & Comparable<? super T>>; the minimum element in the immutable collection
      Throws:
      NoSuchElementException - if the immutable collection is empty.
    • min

      public static <T> T min(ImmutableCollection<? extends T> ic, Comparator<? super T> comparator)
      Returns the minimum element of an immutable collection according to the natural ordering of its elements.
      Type Parameters:
      T - the object type in the collection
      Parameters:
      ic - ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collection
      comparator - Comparator<? super T>; a comparator for T
      Returns:
      T <T extends Object & Comparable<? super T>>; the minimum element in the immutable collection
      Throws:
      NoSuchElementException - if the immutable collection is empty.
    • frequency

      public static int frequency(ImmutableCollection<?> ic, Object o)
      Return the number of occurrences of an object in an immutable collection.
      Parameters:
      ic - ImmutableCollection<?>; the immutable collection
      o - Object; the object to count the number occurrences of
      Returns:
      int; the number of occurrences of o in ic