Package org.cip4.jdflib.util
Class ContainerUtil
- java.lang.Object
-
- org.cip4.jdflib.util.ContainerUtil
-
public class ContainerUtil extends java.lang.Objectclass 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> voidadd(java.util.Collection<A> c, A a)null safe addstatic <A> voidaddAll(java.util.Collection<A> c, A[] a)appends all strings of an array tothisstatic <A> java.util.Collection<A>addAll(java.util.Collection<A> c1, java.util.Collection<A> c2)null safe addAllstatic <A> voidappendUnique(java.util.Collection<A> c, A a)AppendUnique - append an object but ignore multiple entriesstatic <A> java.util.Collection<A>appendUnique(java.util.Collection<A> c, java.util.Collection<A> a)AppendUnique - append an object but ignore multiple entriesstatic voidclose(java.io.Closeable w)static intcompare(java.lang.Comparable c0, java.lang.Comparable c1)static implementation of compare for any comparable object that gracefully handles null
null is always the smalleststatic <A> booleancontains(java.util.Collection<A> c, A other)null safe helperstatic <A> booleancontainsAll(java.util.Collection<A> c, java.util.Collection<A> others)null safe helperstatic <A> booleancontainsAny(java.util.Collection<A> c, java.util.Collection<A> others)checks whether at least one of a given vector of strings is contained inthisstatic <A> intcount(java.util.Collection<A> c, A other)null safe helperstatic <A> voidensureSize(int size, java.util.Collection<A> coll)ensure that a collection has at least size elements and fill any newly created entries with nullsstatic booleanequals(java.lang.Object a, java.lang.Object b)return true if a equals b or both are nullstatic <A> Aget(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 keysstatic <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> IMatchesgetMatch(java.util.Collection<? extends IMatches> c, a obj, int iSkip)return a matching element from a collection of IMatchesstatic <A> AgetMatch(IMatches match, java.util.Collection<A> c, int iSkip)return a matching element from a collection of IMatchesstatic <A> java.util.Vector<IMatches>getMatches(java.util.Collection<? extends IMatches> c, A obj)return a matching element from a collection of IMatchesstatic <A> java.util.Vector<A>getMatches(IMatches m, java.util.Collection<A> c)return a matching element from a collectionstatic <A> java.util.List<IMatches>getMatchesList(java.util.Collection<? extends IMatches> c, A obj)return a matching element from a collection of IMatchesstatic <A> java.util.List<A>getMatchesList(IMatches m, java.util.Collection<A> c)return a matching element from a collectionstatic java.util.Collection<?>getNonEmpty(java.util.Collection<?> c)return null if c==null or c.isEmpty=true used to zapp empty collectionsstatic java.util.Map<?,?>getNonEmpty(java.util.Map<?,?> c)return null if c==null or c.isEmpty=true used to zapp empty collectionsstatic 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 collectionsstatic <A> java.util.List<A>getOverlapping(java.util.Collection<A> c, java.util.Collection<A> others)vector of strings that is contained inthisstatic <A> intindex(java.util.List<A> l, int i)static booleanisEmpty(java.util.Collection<?> c)static booleanisEmpty(java.util.Map<?,?> c)static booleanmatches(IMatches a, java.lang.Object b)return true if a matches b or both are nullstatic booleanmatchesExisting(IMatches a, java.lang.Object b)return true if a matches b or one of a or b is nullstatic <A,B>
Bput(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> Aremove(java.util.List<A> l, int i)remove but also implementing the usual neg number syntaxstatic intsize(java.util.Collection<?> c)static intsize(java.util.Map<?,?> c)static <A> java.util.List<A>toArrayList(A[] array)create an ArrayList from an Array, skipping null elementsstatic <A,B>
java.util.List<B>toArrayList(java.util.Map<A,B> m)create a Vector copy of entry values from a mapstatic <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 mapstatic <A> java.util.Set<A>toHashSet(A[] l)create a HashSet from an Arraystatic <A> java.util.Set<A>toHashSet(java.util.Enumeration<A> enumeration)create a HashSet from an enumerationstatic <A> java.util.Set<A>toHashSet(java.util.List<A> list)create a HashSet from a List (Vector...)static <A> java.lang.StringtoString(java.util.Collection<A> c)create a sorted String - never nullstatic <A,B>
java.util.Vector<B>toValueVector(java.util.Map<A,B> m)create a Vector copy of entry values from a mapstatic <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 mapstatic <A> java.util.Vector<A>toVector(A[] array)create a Vector from an Array, skipping null elementsstatic <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)
-
-
-
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(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
-
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
-
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 collectionc2- 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 inthis- 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 inthis- 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 tothis- Parameters:
strings- the array of strings to append tothis
-
add
public static <A> void add(java.util.Collection<A> c, A a)null safe add- Parameters:
strings- the array of strings to append tothis
-
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 searchobj- the search key for matchesiSkip- 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
-
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 searchobj- 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 searchobj- 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 searchobj- 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 searchobj- 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 keyb- type of the map entry- Parameters:
m- the map to dump to an arraysortByKey- , 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 keyB- type of the map entry- Parameters:
m- the map to dump to an arraysortByKey- , 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 keyB- 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 keyB- 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 keyB- 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 keyB- 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 compareb- 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 compareb- 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 compareb- 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:
-
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)
-
-