Class ContainerUtil


  • public class ContainerUtil
    extends java.lang.Object
    class with utilities for containers, e.g. Vectors, sets etc.
    also simple object utilities
    • Constructor Summary

      Constructors 
      Constructor Description
      ContainerUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <A> void add​(java.util.Collection<A> c, A a)
      null safe add
      static <A> void addAll​(java.util.Collection<A> c, A[] a)
      appends all strings of an array to this
      static <A> java.util.Collection<A> addAll​(java.util.Collection<A> c1, java.util.Collection<A> c2)
      null safe addAll
      static <A> void appendUnique​(java.util.Collection<A> c, A a)
      AppendUnique - append an object but ignore multiple entries
      static <A> java.util.Collection<A> appendUnique​(java.util.Collection<A> c, java.util.Collection<A> a)
      AppendUnique - append an object but ignore multiple entries
      static void close​(java.io.Closeable w)  
      static int compare​(java.lang.Comparable c0, java.lang.Comparable c1)
      static implementation of compare for any comparable object that gracefully handles null
      null is always the smallest
      static <A> boolean contains​(java.util.Collection<A> c, A other)
      null safe helper
      static <A> boolean containsAll​(java.util.Collection<A> c, java.util.Collection<A> others)
      null safe helper
      static <A> boolean containsAny​(java.util.Collection<A> c, java.util.Collection<A> others)
      checks whether at least one of a given vector of strings is contained in this
      static <A> int count​(java.util.Collection<A> c, A other)
      null safe helper
      static <A> void ensureSize​(int size, java.util.Collection<A> coll)
      ensure that a collection has at least size elements and fill any newly created entries with nulls
      static boolean equals​(java.lang.Object a, java.lang.Object b)
      return true if a equals b or both are null
      static <A> A get​(java.util.List<A> l, int i)  
      static <A,​B>
      ListMap<B,​A>
      getInvertedListMap​(java.util.Map<A,​B> m)
      create an inverted map with keys and values swapped.
      The new values are vectors since a map may have identical values for different keys
      static <A,​B>
      VectorMap<B,​A>
      getInvertedMap​(java.util.Map<A,​B> m)
      Deprecated. 
      static <A> java.util.Collection<A> getKeyArray​(java.util.Map<A,​?> m)  
      static <A> java.util.List<A> getKeyList​(java.util.Map<A,​?> m)  
      static <A> java.util.Vector<A> getKeyVector​(java.util.Map<A,​?> m)
      Deprecated. 
      static <a> IMatches getMatch​(java.util.Collection<? extends IMatches> c, a obj, int iSkip)
      return a matching element from a collection of IMatches
      static <A> A getMatch​(IMatches match, java.util.Collection<A> c, int iSkip)
      return a matching element from a collection of IMatches
      static <A> java.util.Vector<IMatches> getMatches​(java.util.Collection<? extends IMatches> c, A obj)
      return a matching element from a collection of IMatches
      static <A> java.util.Vector<A> getMatches​(IMatches m, java.util.Collection<A> c)
      return a matching element from a collection
      static <A> java.util.List<IMatches> getMatchesList​(java.util.Collection<? extends IMatches> c, A obj)
      return a matching element from a collection of IMatches
      static <A> java.util.List<A> getMatchesList​(IMatches m, java.util.Collection<A> c)
      return a matching element from a collection
      static java.util.Collection<?> getNonEmpty​(java.util.Collection<?> c)
      return null if c==null or c.isEmpty=true used to zapp empty collections
      static java.util.Map<?,​?> getNonEmpty​(java.util.Map<?,​?> c)
      return null if c==null or c.isEmpty=true used to zapp empty collections
      static java.util.Collection<?> getNonEmptyCollection​(java.util.Collection<? extends java.util.Collection<?>> c)
      return null if c==null or c.isEmpty=true or c contains an empty collection used to zapp empty collections
      static <A> java.util.List<A> getOverlapping​(java.util.Collection<A> c, java.util.Collection<A> others)
      vector of strings that is contained in this
      static <A> int index​(java.util.List<A> l, int i)  
      static boolean isEmpty​(java.util.Collection<?> c)  
      static boolean isEmpty​(java.util.Map<?,​?> c)  
      static boolean matches​(IMatches a, java.lang.Object b)
      return true if a matches b or both are null
      static boolean matchesExisting​(IMatches a, java.lang.Object b)
      return true if a matches b or one of a or b is null
      static <A,​B>
      B
      put​(java.util.Map<A,​B> map, A key, B value)  
      static <A,​B>
      java.util.Map<A,​B>
      putAll​(java.util.Map<A,​B> map, java.util.Map<A,​B> map2)  
      static <A> A remove​(java.util.List<A> l, int i)
      remove but also implementing the usual neg number syntax
      static <A,​B>
      java.util.Map<A,​B>
      retainAll​(java.util.Map<A,​B> map, java.util.Collection<A> keep)  
      static int size​(java.util.Collection<?> c)  
      static int size​(java.util.Map<?,​?> c)  
      static <A> java.util.List<A> toArrayList​(A[] array)
      create an ArrayList from an Array, skipping null elements
      static <A,​B>
      java.util.List<B>
      toArrayList​(java.util.Map<A,​B> m)
      create a Vector copy of entry values from a map
      static <A extends java.lang.Comparable<? super A>,​B>
      java.util.List<B>
      toArrayList​(java.util.Map<A,​B> m, boolean sortByKey)
      create a Vector of entry values from a map
      static <A> java.util.Set<A> toHashSet​(A[] l)
      create a HashSet from an Array
      static <A> java.util.Set<A> toHashSet​(java.util.Collection<A> list)
      create a HashSet from a List (Vector...)
      static <A> java.util.Set<A> toHashSet​(java.util.Enumeration<A> enumeration)
      create a HashSet from an enumeration
      static <A> java.util.Set<A> toHashSet​(java.util.List<A> list)
      create a HashSet from a List (Vector...)
      static <A> java.lang.String toString​(java.util.Collection<A> c)
      create a sorted String - never null
      static <A,​B>
      java.util.Vector<B>
      toValueVector​(java.util.Map<A,​B> m)
      create a Vector copy of entry values from a map
      static <a extends java.lang.Comparable<? super a>,​b>
      java.util.Vector<b>
      toValueVector​(java.util.Map<a,​b> m, boolean sortByKey)
      create a Vector of entry values from a map
      static <A> java.util.Vector<A> toVector​(A[] array)
      create a Vector from an Array, skipping null elements
      static <A> java.util.Collection<A> unify​(java.util.Collection<A> c)
      unify a collection while retaining the initial order (if the input collection is ordered)
      static <A extends IMatches>
      java.util.Collection<A>
      unifyMatches​(java.util.Collection<A> c)
      unify a collection while retaining the initial order (if the input collection is ordered)
      • Methods inherited from class java.lang.Object

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

      • ContainerUtil

        public ContainerUtil()
    • Method Detail

      • toHashSet

        public static <A> java.util.Set<A> toHashSet​(java.util.Enumeration<A> enumeration)
        create a HashSet from an enumeration
        Type Parameters:
        A - the data type of the sets
        Parameters:
        enumeration - the enumeration
        Returns:
        a Set created from list
      • getNonEmpty

        public static java.util.Collection<?> getNonEmpty​(java.util.Collection<?> c)
        return null if c==null or c.isEmpty=true used to zapp empty collections
        Parameters:
        c - the Collection to test
        Returns:
        the converted Collecion
      • getNonEmpty

        public static java.util.Map<?,​?> getNonEmpty​(java.util.Map<?,​?> c)
        return null if c==null or c.isEmpty=true used to zapp empty collections
        Parameters:
        c - the Collection to test
        Returns:
        the converted Collecion
      • getNonEmptyCollection

        public static java.util.Collection<?> getNonEmptyCollection​(java.util.Collection<? extends java.util.Collection<?>> c)
        return null if c==null or c.isEmpty=true or c contains an empty collection used to zapp empty collections
        Parameters:
        c - the Collection to test
        Returns:
        the converted Collecion
      • toHashSet

        public static <A> java.util.Set<A> toHashSet​(java.util.List<A> list)
        create a HashSet from a List (Vector...)
        Type Parameters:
        A - the data type of the sets
        Parameters:
        list - the list
        Returns:
        a Set created from list
      • toHashSet

        public static <A> java.util.Set<A> toHashSet​(java.util.Collection<A> list)
        create a HashSet from a List (Vector...)
        Type Parameters:
        A - the data type of the sets
        Parameters:
        list - the list
        Returns:
        a Set created from list
      • toHashSet

        public static <A> java.util.Set<A> toHashSet​(A[] l)
        create a HashSet from an Array
        Type Parameters:
        A - datatype
        Parameters:
        l - the array
        Returns:
        a Set created from list
      • toVector

        public static <A> java.util.Vector<A> toVector​(A[] array)
        create a Vector from an Array, skipping null elements
        Type Parameters:
        A - the type
        Parameters:
        array - the array to convert
        Returns:
        a Vector
      • toString

        public static <A> java.lang.String toString​(java.util.Collection<A> c)
        create a sorted String - never null
        Type Parameters:
        A -
        Parameters:
        c -
        Returns:
      • toArrayList

        public static <A> java.util.List<A> toArrayList​(A[] array)
        create an ArrayList from an Array, skipping null elements
        Type Parameters:
        A - the type
        Parameters:
        array - the array to convert
        Returns:
        a Vector
      • addAll

        public static <A> java.util.Collection<A> addAll​(java.util.Collection<A> c1,
                                                         java.util.Collection<A> c2)
        null safe addAll
        Type Parameters:
        A - type
        Parameters:
        c1 - first collection
        c2 - second collection
        Returns:
        c1 with c2 added, c2 if c1==null
      • containsAny

        public static <A> boolean containsAny​(java.util.Collection<A> c,
                                              java.util.Collection<A> others)
        checks whether at least one of a given vector of strings is contained in this
        Parameters:
        others - the VSTring of values to test
        Returns:
        true if at least one String in other is in this
      • contains

        public static <A> boolean contains​(java.util.Collection<A> c,
                                           A other)
        null safe helper
        Type Parameters:
        A -
        Parameters:
        c -
        other -
        Returns:
      • count

        public static <A> int count​(java.util.Collection<A> c,
                                    A other)
        null safe helper
        Type Parameters:
        A -
        Parameters:
        c -
        other -
        Returns:
      • containsAll

        public static <A> boolean containsAll​(java.util.Collection<A> c,
                                              java.util.Collection<A> others)
        null safe helper
        Type Parameters:
        A -
        Parameters:
        c -
        others -
        Returns:
      • getOverlapping

        public static <A> java.util.List<A> getOverlapping​(java.util.Collection<A> c,
                                                           java.util.Collection<A> others)
        vector of strings that is contained in this
        Parameters:
        others - the VString of values to test
        Returns:
        true if at least one String in other is in this
      • addAll

        public static <A> void addAll​(java.util.Collection<A> c,
                                      A[] a)
        appends all strings of an array to this
        Parameters:
        strings - the array of strings to append to this
      • add

        public static <A> void add​(java.util.Collection<A> c,
                                   A a)
        null safe add
        Parameters:
        strings - the array of strings to append to this
      • getMatch

        public static <a> IMatches getMatch​(java.util.Collection<? extends IMatches> c,
                                            a obj,
                                            int iSkip)
        return a matching element from a collection of IMatches
        Type Parameters:
        a - the data type
        Parameters:
        c - the collection to search
        obj - the search key for matches
        iSkip - which one to grab, may be negative in which case we count -1=last, -2=second last...
        Returns:
        the matching IMatches
      • getMatch

        public static <A> A getMatch​(IMatches match,
                                     java.util.Collection<A> c,
                                     int iSkip)
        return a matching element from a collection of IMatches
        Type Parameters:
        A - the data type
        Parameters:
        match - the matcher
        c - the Collection
        iSkip - which one to grab, may be negative in which case we count -1=last, -2=second last...
        Returns:
        the matching
      • getMatches

        public static <A> java.util.Vector<IMatches> getMatches​(java.util.Collection<? extends IMatches> c,
                                                                A obj)
        return a matching element from a collection of IMatches
        Type Parameters:
        A - the data type
        Parameters:
        c - the collection to search
        obj - the search key for matches
        Returns:
        Vector of matching a
      • getMatchesList

        public static <A> java.util.List<IMatches> getMatchesList​(java.util.Collection<? extends IMatches> c,
                                                                  A obj)
        return a matching element from a collection of IMatches
        Type Parameters:
        A - the data type
        Parameters:
        c - the collection to search
        obj - the search key for matches
        Returns:
        Vector of matching a
      • getMatches

        public static <A> java.util.Vector<A> getMatches​(IMatches m,
                                                         java.util.Collection<A> c)
        return a matching element from a collection
        Type Parameters:
        A - the data type
        Parameters:
        c - the collection to search
        obj - the matches
        Returns:
        Vector of matching a
      • getMatchesList

        public static <A> java.util.List<A> getMatchesList​(IMatches m,
                                                           java.util.Collection<A> c)
        return a matching element from a collection
        Type Parameters:
        A - the data type
        Parameters:
        c - the collection to search
        obj - the matches
        Returns:
        Vector of matching a
      • toValueVector

        public static <a extends java.lang.Comparable<? super a>,​b> java.util.Vector<b> toValueVector​(java.util.Map<a,​b> m,
                                                                                                            boolean sortByKey)
        create a Vector of entry values from a map
        Type Parameters:
        a - type of the map key
        b - type of the map entry
        Parameters:
        m - the map to dump to an array
        sortByKey - , if true, sort the entries by key
        Returns:
        the vector
      • toArrayList

        public static <A extends java.lang.Comparable<? super A>,​B> java.util.List<B> toArrayList​(java.util.Map<A,​B> m,
                                                                                                        boolean sortByKey)
        create a Vector of entry values from a map
        Type Parameters:
        A - type of the map key
        B - type of the map entry
        Parameters:
        m - the map to dump to an array
        sortByKey - , if true, sort the entries by key
        Returns:
        the vector
      • toValueVector

        public static <A,​B> java.util.Vector<B> toValueVector​(java.util.Map<A,​B> m)
        create a Vector copy of entry values from a map
        Type Parameters:
        A - data type of the map key
        B - data type of the map value
        Parameters:
        m - the map to dump to an array
        Returns:
        the vector
      • toArrayList

        public static <A,​B> java.util.List<B> toArrayList​(java.util.Map<A,​B> m)
        create a Vector copy of entry values from a map
        Type Parameters:
        A - data type of the map key
        B - data type of the map value
        Parameters:
        m - the map to dump to an array
        Returns:
        the vector
      • getInvertedMap

        @Deprecated
        public static <A,​B> VectorMap<B,​A> getInvertedMap​(java.util.Map<A,​B> m)
        Deprecated.
        create an inverted map with keys and values swapped.
        The new values are vectors since a map may have identical values for different keys
        Type Parameters:
        A - data type of the map key
        B - data type of the map value
        Parameters:
        m - the map to invert
        Returns:
        the inverted map
      • appendUnique

        public static <A> void appendUnique​(java.util.Collection<A> c,
                                            A a)
        AppendUnique - append an object but ignore multiple entries
        Parameters:
        a - the A to append, if null nothing is added
      • appendUnique

        public static <A> java.util.Collection<A> appendUnique​(java.util.Collection<A> c,
                                                               java.util.Collection<A> a)
        AppendUnique - append an object but ignore multiple entries
        Parameters:
        a - the A to append, if null nothing is added
      • index

        public static <A> int index​(java.util.List<A> l,
                                    int i)
        Parameters:
        l -
        i -
        Returns:
      • get

        public static <A> A get​(java.util.List<A> l,
                                int i)
        Parameters:
        l -
        i -
        Returns:
      • remove

        public static <A> A remove​(java.util.List<A> l,
                                   int i)
        remove but also implementing the usual neg number syntax
        Parameters:
        index - if 0, or positive count from front, else if negative from back
        See Also:
        Vector.remove(int)
      • getInvertedListMap

        public static <A,​B> ListMap<B,​A> getInvertedListMap​(java.util.Map<A,​B> m)
        create an inverted map with keys and values swapped.
        The new values are vectors since a map may have identical values for different keys
        Type Parameters:
        A - data type of the map key
        B - data type of the map value
        Parameters:
        m - the map to invert
        Returns:
        the inverted map
      • getKeyVector

        @Deprecated
        public static <A> java.util.Vector<A> getKeyVector​(java.util.Map<A,​?> m)
        Deprecated.
        create a Vector of key values from a map
        Type Parameters:
        A - data type of the map key
        Parameters:
        m - the map to dump to an array
        Returns:
        the vector of keys - note that this Vector goes NOT reflect changes to the map
      • equals

        public static boolean equals​(java.lang.Object a,
                                     java.lang.Object b)
        return true if a equals b or both are null
        Parameters:
        a - Object to compare
        b - Object to compare
        Returns:
        boolean true if a equals b or both are null
      • matches

        public static boolean matches​(IMatches a,
                                      java.lang.Object b)
        return true if a matches b or both are null
        Parameters:
        a - Object to compare
        b - Object to compare
        Returns:
        boolean true if a matches b or both are null
      • matchesExisting

        public static boolean matchesExisting​(IMatches a,
                                              java.lang.Object b)
        return true if a matches b or one of a or b is null
        Parameters:
        a - Object to compare
        b - Object to compare
        Returns:
        boolean true if a matches b or both are null
      • compare

        public static int compare​(java.lang.Comparable c0,
                                  java.lang.Comparable c1)
        static implementation of compare for any comparable object that gracefully handles null
        null is always the smallest
        Parameters:
        c0 -
        c1 -
        Returns:
        -1 if c0 < c1, 0 if equal, 1 if c0 > c1;
      • ensureSize

        public static <A> void ensureSize​(int size,
                                          java.util.Collection<A> coll)
        ensure that a collection has at least size elements and fill any newly created entries with nulls
        Type Parameters:
        A - anything - needed for the cast
        Parameters:
        size -
        coll -
      • unify

        public static <A> java.util.Collection<A> unify​(java.util.Collection<A> c)
        unify a collection while retaining the initial order (if the input collection is ordered)
        Type Parameters:
        A - the data type of the collection
        Parameters:
        c - the collection to unify
        Returns:
        the unified collection - always the input collection
      • unifyMatches

        public static <A extends IMatches> java.util.Collection<A> unifyMatches​(java.util.Collection<A> c)
        unify a collection while retaining the initial order (if the input collection is ordered)
        Type Parameters:
        A - the data type of the collection
        Parameters:
        c - the collection to unify
        Returns:
        the unified collection - always the input collection
      • size

        public static int size​(java.util.Collection<?> c)
        Parameters:
        c - the collection to check
        Returns:
        0 for null or size
      • size

        public static int size​(java.util.Map<?,​?> c)
        Parameters:
        c - the collection to check
        Returns:
        0 for null or size
      • isEmpty

        public static boolean isEmpty​(java.util.Collection<?> c)
        Parameters:
        c -
        Returns:
      • isEmpty

        public static boolean isEmpty​(java.util.Map<?,​?> c)
        Parameters:
        c -
        Returns:
      • getKeyArray

        public static <A> java.util.Collection<A> getKeyArray​(java.util.Map<A,​?> m)
      • getKeyList

        public static <A> java.util.List<A> getKeyList​(java.util.Map<A,​?> m)
      • putAll

        public static <A,​B> java.util.Map<A,​B> putAll​(java.util.Map<A,​B> map,
                                                                  java.util.Map<A,​B> map2)
        Type Parameters:
        A -
        B -
        Parameters:
        map -
        map2 -
        Returns:
      • retainAll

        public static <A,​B> java.util.Map<A,​B> retainAll​(java.util.Map<A,​B> map,
                                                                     java.util.Collection<A> keep)
        Type Parameters:
        A -
        B -
        Parameters:
        map -
        map2 -
        Returns:
      • put

        public static <A,​B> B put​(java.util.Map<A,​B> map,
                                        A key,
                                        B value)
        Type Parameters:
        A -
        B -
        Parameters:
        map -
        map2 -
        Returns:
      • close

        public static void close​(java.io.Closeable w)