org.cip4.jdflib.util
Class ContainerUtil

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

public class ContainerUtil
extends Object

class with utilities for containers, e.g. Vectors, sets etc.
also simple object utilities

Author:
Dr. Rainer Prosi, Heidelberger Druckmaschinen AG before June 18, 2009

Constructor Summary
ContainerUtil()
           
 
Method Summary
static
<a> Collection<a>
addAll(Collection<a> c1, Collection<a> c2)
          null safe addAll
static int compare(Comparable c0, Comparable c1)
          static implementation of compare for any comparable object that gracefully handles null
null is always the smallest
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
static boolean equals(Object a, Object b)
          return true if a equals b or both are null
static
<a> Vector<a>
getKeyVector(Map<a,?> m)
          create a Vector of key values from a map
static
<a> IMatches
getMatch(Collection<? extends IMatches> c, a obj, int iSkip)
          return a matching element from a collection of IMatches
static
<a> Vector<IMatches>
getMatches(Collection<? extends IMatches> c, a obj)
          return a matching element from a collection of IMatches
static boolean matches(IMatches a, Object b)
          return true if a matches b or both are null
static
<a> Set<a>
toHashSet(a[] l)
          create a HashSet from an Array
static
<a> Set<a>
toHashSet(Enumeration<a> enumeration)
          create a HashSet from an enumeration
static
<a> Set<a>
toHashSet(List<a> list)
          create a HashSet from a List (Vector...)
static
<a,b> Vector<b>
toValueVector(Map<a,b> m)
          create a Vector copy of entry values from a map
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
static
<a> Vector<a>
toVector(a[] array)
          create a Vector from an Array, skipping null elements
static
<a> Collection<a>
unify(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> 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

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(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

addAll

public static <a> Collection<a> addAll(Collection<a> c1,
                                       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

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

getMatches

public static <a> Vector<IMatches> getMatches(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

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

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

getKeyVector

public static <a> Vector<a> getKeyVector(Map<a,?> m)
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 equals 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


Copyright © 2013. All Rights Reserved.