Class ContainerUtil

java.lang.Object
org.cip4.jdflib.util.ContainerUtil

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

    • ContainerUtil

      public ContainerUtil()
  • Method Details

    • toHashSet

      public static <A> Set<A> toHashSet(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 Collection<?> getNonEmpty(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 Map<?,?> getNonEmpty(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 Collection<?> getNonEmptyCollection(Collection<? extends 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> Set<A> toHashSet(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> Set<A> toHashSet(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> 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> 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

      create a sorted String - never null
      Type Parameters:
      A -
      Parameters:
      c -
      Returns:
    • toArrayList

      public static <A> 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

      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(Collection<A> c, 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(Collection<A> c, A other)
      null safe helper
      Type Parameters:
      A -
      Parameters:
      c -
      other -
      Returns:
    • count

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

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

      public static <A> List<A> getOverlapping(Collection<A> c, 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> Collection<A> addAll(Collection<A> c, A[] a)
      appends all strings of an array to this
      Parameters:
      strings - the array of strings to append to this
    • removeAll

      public static <A> Collection<A> removeAll(Collection<A> c, Collection<A> a)
      appends all strings of an array to this
      Parameters:
      strings - the array of strings to append to this
    • removeAll

      public static <A> Collection<A> removeAll(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> Collection<A> add(Collection<A> c, A a)
      null safe add
      Parameters:
      strings - the array of strings to append to this
    • getMatch

      public static <a> IMatches getMatch(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, 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> Vector<IMatchesgetMatches(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> List<IMatches> getMatchesList(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> Vector<A> getMatches(IMatches m, 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> List<A> getMatchesList(IMatches m, 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 Comparable<? super a>, b> Vector<b> toValueVector(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 Comparable<? super A>, B> List<B> toArrayList(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> Vector<B> toValueVector(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> List<B> toArrayList(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(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> Collection<A> appendUnique(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> Collection<A> appendUnique(Collection<A> c, 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(List<A> l, int i)
      Parameters:
      l -
      i -
      Returns:
    • get

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

      public static <A> A remove(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:
    • getInvertedListMap

      public static <A, B> ListMap<B,A> getInvertedListMap(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> Vector<A> getKeyVector(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(Object a, 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, 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, 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(Comparable c0, 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, 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> Collection<A> unify(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> Collection<A> unifyMatches(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
    • length

      public static int length(Object[] c)
      Parameters:
      c - the collection to check
      Returns:
      0 for null or size
    • isEmpty

      public static boolean isEmpty(Object[] c)
      Parameters:
      c - the collection to check
      Returns:
      0 for null or size
    • size

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

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

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

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

      public static <A> Collection<A> getKeyArray(Map<A,?> m)
    • getKeyList

      public static <A> List<A> getKeyList(Map<A,?> m)
    • putAll

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

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

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

      public static void close(Closeable w)