Class CollectionUtilities


  • public final class CollectionUtilities
    extends java.lang.Object
    Utilities that ought to be on collections, but aren't
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T,​U extends java.util.Collection<T>>
      U
      addAll​(java.util.Iterator<T> source, U target)
      Add all items in iterator to target collection
      static <T> java.util.Map<T,​T> asMap​(T[][] source)  
      static <T> java.util.Map<T,​T> asMap​(T[][] source, java.util.Map<T,​T> target, boolean reverse)
      Utility like Arrays.asList()
      static <T extends java.lang.Comparable>
      int
      compare​(java.util.Iterator<T> iterator1, java.util.Iterator<T> iterator2)
      Compare iterators
      static <T extends java.lang.Comparable>
      int
      compare​(T a, T b)
      Compare, allowing nulls and putting them first
      static <T extends java.lang.Comparable,​U extends java.util.Collection<T>>
      int
      compare​(U o1, U o2)
      Compare, with shortest first, and otherwise lexicographically
      static <K extends java.lang.Comparable,​V extends java.lang.Comparable,​T extends java.util.Map.Entry<K,​V>>
      int
      compareEntrySets​(java.util.Collection<T> o1, java.util.Collection<T> o2)  
      static boolean containsAll​(java.util.Collection a, java.util.Collection b)  
      static boolean containsNone​(java.util.Collection a, java.util.Collection b)  
      static boolean containsSome​(java.util.Collection a, java.util.Collection b)  
      static <T> boolean equals​(T a, T b)
      Compare, allowing nulls
      static <T,​U extends java.util.Collection<T>>
      T
      getBest​(U c, java.util.Comparator<T> comp, int direction)
      Get the "best" in collection.
      static int getContainmentRelation​(java.util.Collection a, java.util.Collection b)
      Assesses all the possible containment relations between collections A and B with one call.
      Returns an int with bits set, according to a "Venn Diagram" view of A vs B.
      NOT_A_SUPERSET_B: a - b != {}
      NOT_A_DISJOINT_B: a * b != {} // * is intersects
      NOT_A_SUBSET_B: b - a != {}
      Thus the bits can be used to get the following relations:
      for A_SUPERSET_B, use (x & CollectionUtilities.NOT_A_SUPERSET_B) == 0
      for A_SUBSET_B, use (x & CollectionUtilities.NOT_A_SUBSET_B) == 0
      for A_EQUALS_B, use (x & CollectionUtilities.NOT_A_EQUALS_B) == 0
      for A_DISJOINT_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) == 0
      for A_OVERLAPS_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) != 0
      <T,​U extends java.util.Collection<T>>
      T
      getFirst​(U c)
      Get first item in collection, or null if there is none.
      static <T> java.lang.String join​(T[] array, java.lang.String separator)
      Join an array of items.
      static <T,​U extends java.lang.Iterable<T>>
      java.lang.String
      join​(U collection, java.lang.String separator)
      Join a collection of items.
      static int matchesAt​(java.lang.CharSequence text, int offset, java.lang.CharSequence other)
      Does one string contain another, starting at a specific offset?
      static <K,​V>
      java.util.Map<K,​V>
      removeAll​(java.util.Map<K,​V> m, java.util.Collection<K> itemsToRemove)
      Utility that ought to be on Map
      static <T,​U extends java.util.Collection<T>>
      U
      removeAll​(U c, CollectionUtilities.ObjectMatcher<T> f)
      Remove matching items
      static <T,​U extends java.util.Collection<T>>
      U
      retainAll​(U c, CollectionUtilities.ObjectMatcher<T> f)
      Retain matching items
      static int size​(java.util.Iterator source)
      Get the size of an iterator (number of items in it).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ALL_EMPTY

        public static final int ALL_EMPTY
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
      • NOT_A_SUPERSET_B

        public static final int NOT_A_SUPERSET_B
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
      • NOT_A_DISJOINT_B

        public static final int NOT_A_DISJOINT_B
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
      • NOT_A_SUBSET_B

        public static final int NOT_A_SUBSET_B
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
      • NOT_A_EQUALS_B

        public static final int NOT_A_EQUALS_B
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
      • A_PROPER_SUBSET_OF_B

        public static final int A_PROPER_SUBSET_OF_B
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
      • A_PROPER_SUPERSET_B

        public static final int A_PROPER_SUPERSET_B
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
      • A_PROPER_OVERLAPS_B

        public static final int A_PROPER_OVERLAPS_B
        Used for results of getContainmentRelation
        See Also:
        Constant Field Values
    • Constructor Detail

      • CollectionUtilities

        public CollectionUtilities()
    • Method Detail

      • join

        public static <T> java.lang.String join​(T[] array,
                                                java.lang.String separator)
        Join an array of items.
        Type Parameters:
        T -
        Parameters:
        array -
        separator -
        Returns:
        string
      • join

        public static <T,​U extends java.lang.Iterable<T>> java.lang.String join​(U collection,
                                                                                      java.lang.String separator)
        Join a collection of items.
        Type Parameters:
        T -
        U -
        Parameters:
        collection -
        array -
        separator -
        Returns:
        string
      • asMap

        public static <T> java.util.Map<T,​T> asMap​(T[][] source,
                                                         java.util.Map<T,​T> target,
                                                         boolean reverse)
        Utility like Arrays.asList()
        Type Parameters:
        T -
        Parameters:
        source -
        target -
        reverse -
        Returns:
      • addAll

        public static <T,​U extends java.util.Collection<T>> U addAll​(java.util.Iterator<T> source,
                                                                           U target)
        Add all items in iterator to target collection
        Type Parameters:
        T -
        U -
        Parameters:
        source -
        target -
        Returns:
      • size

        public static int size​(java.util.Iterator source)
        Get the size of an iterator (number of items in it).
        Parameters:
        source -
        Returns:
      • asMap

        public static <T> java.util.Map<T,​T> asMap​(T[][] source)
        Type Parameters:
        T -
        Parameters:
        source -
        Returns:
      • removeAll

        public static <K,​V> java.util.Map<K,​V> removeAll​(java.util.Map<K,​V> m,
                                                                     java.util.Collection<K> itemsToRemove)
        Utility that ought to be on Map
        Type Parameters:
        K -
        V -
        Parameters:
        m -
        itemsToRemove -
        Returns:
        map passed in
      • getFirst

        public <T,​U extends java.util.Collection<T>> T getFirst​(U c)
        Get first item in collection, or null if there is none.
        Type Parameters:
        T -
        U -
        Parameters:
        c -
        Returns:
        first item
      • getBest

        public static <T,​U extends java.util.Collection<T>> T getBest​(U c,
                                                                            java.util.Comparator<T> comp,
                                                                            int direction)
        Get the "best" in collection. That is the least if direction is < 0, otherwise the greatest. The first is chosen if there are multiples.
        Type Parameters:
        T -
        U -
        Parameters:
        c -
        comp -
        direction -
        Returns:
      • removeAll

        public static <T,​U extends java.util.Collection<T>> U removeAll​(U c,
                                                                              CollectionUtilities.ObjectMatcher<T> f)
        Remove matching items
        Type Parameters:
        T -
        U -
        Parameters:
        c -
        f -
        Returns:
      • retainAll

        public static <T,​U extends java.util.Collection<T>> U retainAll​(U c,
                                                                              CollectionUtilities.ObjectMatcher<T> f)
        Retain matching items
        Type Parameters:
        T -
        U -
        Parameters:
        c -
        f -
        Returns:
      • containsSome

        public static boolean containsSome​(java.util.Collection a,
                                           java.util.Collection b)
        Parameters:
        a -
        b -
        Returns:
      • containsAll

        public static boolean containsAll​(java.util.Collection a,
                                          java.util.Collection b)
      • containsNone

        public static boolean containsNone​(java.util.Collection a,
                                           java.util.Collection b)
      • getContainmentRelation

        public static int getContainmentRelation​(java.util.Collection a,
                                                 java.util.Collection b)
        Assesses all the possible containment relations between collections A and B with one call.
        Returns an int with bits set, according to a "Venn Diagram" view of A vs B.
        NOT_A_SUPERSET_B: a - b != {}
        NOT_A_DISJOINT_B: a * b != {} // * is intersects
        NOT_A_SUBSET_B: b - a != {}
        Thus the bits can be used to get the following relations:
        for A_SUPERSET_B, use (x & CollectionUtilities.NOT_A_SUPERSET_B) == 0
        for A_SUBSET_B, use (x & CollectionUtilities.NOT_A_SUBSET_B) == 0
        for A_EQUALS_B, use (x & CollectionUtilities.NOT_A_EQUALS_B) == 0
        for A_DISJOINT_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) == 0
        for A_OVERLAPS_B, use (x & CollectionUtilities.NOT_A_DISJOINT_B) != 0
      • matchesAt

        public static int matchesAt​(java.lang.CharSequence text,
                                    int offset,
                                    java.lang.CharSequence other)
        Does one string contain another, starting at a specific offset?
        Parameters:
        text -
        offset -
        other -
        Returns:
      • equals

        public static <T> boolean equals​(T a,
                                         T b)
        Compare, allowing nulls
        Parameters:
        a -
        b -
        Returns:
      • compare

        public static <T extends java.lang.Comparable> int compare​(T a,
                                                                   T b)
        Compare, allowing nulls and putting them first
        Parameters:
        a -
        b -
        Returns:
      • compare

        public static <T extends java.lang.Comparable> int compare​(java.util.Iterator<T> iterator1,
                                                                   java.util.Iterator<T> iterator2)
        Compare iterators
        Parameters:
        iterator1 -
        iterator2 -
        Returns:
      • compare

        public static <T extends java.lang.Comparable,​U extends java.util.Collection<T>> int compare​(U o1,
                                                                                                           U o2)
        Compare, with shortest first, and otherwise lexicographically
        Parameters:
        a -
        b -
        Returns:
      • compare

        public static <T extends java.lang.Comparable,​U extends java.util.Set<T>> int compare​(U o1,
                                                                                                    U o2)
        Compare, with shortest first, and otherwise lexicographically
        Parameters:
        a -
        b -
        Returns:
      • compare

        public static <K extends java.lang.Comparable,​V extends java.lang.Comparable,​T extends java.util.Map.Entry<K,​V>> int compare​(T a,
                                                                                                                                                       T b)
        Compare, allowing nulls and putting them first
        Parameters:
        a -
        b -
        Returns:
      • compareEntrySets

        public static <K extends java.lang.Comparable,​V extends java.lang.Comparable,​T extends java.util.Map.Entry<K,​V>> int compareEntrySets​(java.util.Collection<T> o1,
                                                                                                                                                                java.util.Collection<T> o2)