com.isomorphic.util
Class DataTools

com.isomorphic.util.DataTools

public class DataTools

DataTools is a utility class with the following facilities:

See Also:
DataTools.prettyPrint(Object), DataTools.getProperties(Object), DataTools.setProperties(Map, Object)

Method Summary
static java.util.List addAll(java.util.List target, java.util.Iterator source)
          Adds all the elements of an Iterator to a List.
static java.util.List addAll(java.util.List target, java.util.List source)
          Adds the contents of one List to another.
static java.util.List addAsList(java.util.List targetList, java.lang.Object sourceList)
          Add either a single object or List to another List.
static void addDisjunctionToSet(java.util.List one, java.util.List two)
          Adds elements of the second List that are missing in the first.
static void addToIntInMap(java.util.Map map, java.lang.Object key, int addition)
          Add to the value of an Integer instance stored in the map "map" under key "key".
static java.util.List arrayToList(java.lang.Object[] arr)
          Creates a List from an array.
static java.util.List arrayToList(java.lang.Object[] arr, int from, int length)
          Creates a List containing a subset of an array.
static java.lang.Object[] arrayUnion(java.lang.Object[] one, java.lang.Object[] two)
          Creates a new array containing all the elements of two arrays.
static java.util.List buildList(java.lang.Object one)
          Creates a List of one elements.
static java.util.List buildList(java.lang.Object one, java.lang.Object two)
          Creates a List of two elements.
static java.util.List buildList(java.lang.Object one, java.lang.Object two, java.lang.Object three)
          Creates a List of three elements.
static java.util.List buildList(java.lang.Object one, java.lang.Object two, java.lang.Object three, java.lang.Object four)
          Creates a List of four elements.
static java.util.Map buildMap(java.lang.Object key, java.lang.Object value)
          Creates a Map of one key/value pair.
static java.util.Map buildMap(java.lang.Object key, java.lang.Object value, java.lang.Object key2, java.lang.Object value2)
          Creates a Map of two key/value pairs.
static java.util.Map buildMap(java.lang.Object key, java.lang.Object value, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3)
          Creates a Map of three key/value pairs.
static java.util.Map buildMap(java.lang.Object key, java.lang.Object value, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3, java.lang.Object key4, java.lang.Object value4)
          Creates a Map of four key/value pairs.
static java.util.Map buildMap(java.lang.Object key, java.lang.Object value, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3, java.lang.Object key4, java.lang.Object value4, java.lang.Object key5, java.lang.Object value5)
          Creates a Map of five key/value pairs.
static java.util.Map buildMap(java.lang.Object key, java.lang.Object value, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3, java.lang.Object key4, java.lang.Object value4, java.lang.Object key5, java.lang.Object value5, java.lang.Object key6, java.lang.Object value6)
          Creates a Map of six key/value pairs.
static java.util.Map buildMap(java.lang.Object key, java.lang.Object value, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3, java.lang.Object key4, java.lang.Object value4, java.lang.Object key5, java.lang.Object value5, java.lang.Object key6, java.lang.Object value6, java.lang.Object key7, java.lang.Object value7)
          Creates a Map of six key/value pairs.
static java.lang.Object castValue(java.lang.Object value, java.lang.Class targetType, DataSource ds)
          Converts specified value to targetType.
static int checkSize(java.lang.Object obj)
          Obtains the size of a Map, List or String.
static java.lang.Object combineAsLists(java.lang.Object one, java.lang.Object two)
          Combine two single objects or Lists into one new List, or return a single Object if one argument is single and the other argument is null.
static boolean contains(java.lang.String str, java.lang.String substr)
          Tests if the specified substring is present in the specified string.
static java.lang.Object deepMerge(java.lang.Object source, java.lang.Object target, boolean overwrite)
          Merges one collections data structure into another.
static void deepRemoveEmptyCollections(java.lang.Object data)
          Walks a nested Collection-based datastructure and removes all Collections entities that are zero-sized.
static void deepRemoveKey(java.lang.Object keyToRemove, java.lang.Object data)
          Walks a nested Collection-based datastructure and removes all instances of keyToRemove (and its associated value) from any Map objects found in the data structure.
static void deepRemoveNullValues(java.lang.Object data)
          Walks a nested Collection-based datastructure and removes all entities that have a null value.
static java.util.Map divideMap(java.util.Map origMap, java.util.List retainKeys)
          Alters a Map so that it retains only the specified keys, returning a Map with any keys not retained.
static java.util.List enumToList(java.util.Iterator i)
          Converts an Iterator to a List.
static java.lang.String fastDateFormat(java.util.Date date)
          Returns a String representation of a Date object.
static java.util.List findAll(java.util.List rows, java.util.Map properties)
          Given a List of Maps, return all Maps that have common key/value pairs with another Map.
static java.util.List findAll(java.util.List rows, java.lang.Object propertyName, java.lang.Object value)
          Given a List of Maps, return all Maps that have a certain value for a key.
static java.lang.String formatDate(java.lang.String format, java.util.Date date)
          Returns the parameter date formatted according to the parameter format string.
static java.lang.Class getCachedClass(java.lang.String className)
          Returns loaded Class object for specified name.
static java.lang.reflect.Field[] getClassFields(java.lang.Class c)
          Returns array of all fields declared in specified class and it's supper classes.
static java.lang.reflect.Method[] getClassMethods(java.lang.Class c)
          Returns array of all methods declared in specified class and it's supper classes.
static int getIntInMap(java.util.Map map, java.lang.Object key)
          Get value of an Integer instance stored in the map "map" under key "key", as the primitive type int.
static org.apache.log4j.spi.LoggerRepository getLoggerRepository()
          Returns log4j LoggerRepository instance that was configured from log4j.isc.config.xml.
static java.util.Map getProperties(java.lang.Object bean)
          Gets the properties for a JavaBean and returns them as a Map.

static java.util.List getProperty(java.util.List rows, java.lang.Object column)
          Given a result set, as a List of rows, and a column name, returns the List of values for that column.
static java.util.Map getPropertyDescriptors(java.lang.Class beanClass)
          Gets the properties for a JavaBean class through introspection and returns them as a Map.

Removes properties added by byte-code instrumentation: interceptFieldCallback property is removed if class implements net.sf.cglib.transform.impl.InterceptFieldEnabled interface; fieldHandler property is removed if class implements org.hibernate.bytecode.javassist.FieldHandled interface;

static java.util.Map getPropertyDescriptors(java.lang.Object bean)
          Gets the property descriptors for a JavaBean and returns them as a Map.

static java.lang.Class getPropertyType(java.lang.Class c, java.lang.String propertyName)
          Returns type of specified property in provided class.
static java.lang.Class getRealClass(java.lang.Object bean)
          Checks if provided bean is a proxy and extracts real class.
static java.lang.Object getSingle(java.lang.Object toFetchFrom)
          Retrieves the element of a single element List, or the key of a single key/value pair Map.
static java.lang.String getStackTrace(java.lang.Throwable t)
          Returns the stack trace of a Throwable object as a String.
static java.lang.String getterName(java.lang.reflect.Method method)
          Tests if method is getter and returns property name.
static java.lang.String hashValue(java.lang.String plaintext)
          Returns the hash of the passed-in string, encrypted using the MD5 algorithm
static java.lang.String hashValue(java.lang.String plaintext, java.lang.String algorithm)
          Returns the hash of the passed-in string, encrypted using the specified algorithm
static java.util.Map identityMap(java.util.List list)
          Stores the elements of a List as the keys and values of a Map.
static void incrementIntInMap(java.util.Map map, java.lang.Object key)
          Add to the value of an Integer instance stored in the map "map" under key "key".
static boolean isNumberType(java.lang.Class clazz)
          Returns true if provided class is either assignable to Number or represents one of primitive number types byte, short, int, long, float, double.
static boolean isTextType(java.lang.Class clazz)
          Returns true if provided class is either assignable to Character or assignable to String or represents primitive char type.
static java.lang.Object jxPathContext(java.lang.Object obj)
          Returns JXPathContext in lenient mode
static java.util.List keysAsList(java.util.Map map)
          Creates a List of the keys from a Map.
static java.util.List keysNotPresent(java.util.Map map, java.util.List keys)
          Given a Map and a List of keys, returns the list of keys in the List that are not present in the Map.
static java.lang.Object[] listToArray(java.util.List list)
          Creates an Object array from a List.
static java.lang.String[] listToStringArray(java.util.Collection list)
          Creates a String array from a List.
static java.util.List makeList(java.lang.Object element)
          Creates a List containing one element.
static java.util.List makeListIfSingle(java.lang.Object obj)
          Creates a List containing one element if the element is not a List.
static java.util.Map mapDisjunction(java.util.Map one, java.util.Map two)
          Creates a new Map containing the key/value pairs whose keys are not present in both Maps supplied.
static java.util.Map mapIntersection(java.util.Map primary, java.util.Map secondary)
          Constructs a Map containing the key/value pairs of the primary Map whose keys are present in the secondary Map.
static java.util.List mapIntersectionKeys(java.util.Map one, java.util.Map two)
          Returns the list of keys that are present in both Maps.
static java.util.Map mapMerge(java.util.Map source, java.util.Map target)
          Copies the key/value pairs of a Map into another Map, replacing the key/value pair of the target Map.
static java.util.Map mapMergeNonNull(java.util.Map source, java.util.Map target)
          Copies the key/value pairs of a Map into another Map, replacing the key/value pair of the target Map if the source value is non-null.
static java.util.Properties mapToProperties(java.util.Map map)
          Creates a Properties object from a Map.
static java.util.Map mapUnion(java.util.Map primary, java.util.Map secondary)
          Constructs a Map containing all the key/value pairs of the Maps supplied as arguments, with the first Map taking precedence.
static java.lang.String md5(java.lang.String plaintext)
          Returns the hash of the passed-in string, encrypted using the MD5 algorithm
static java.lang.Object nestedGet(java.lang.Object struct, java.lang.String fetch)
          Retrieves a object from a series of nested Lists or Map.
static java.util.LinkedHashMap orderedMapUnion(java.util.Map primary, java.util.Map secondary)
          Does the same as mapUnion, but keeps order of key/value pairs.
static java.lang.String prettyPrint(java.lang.Object obj)
          Formats information about an Object for viewing or printing as a String.
static java.util.Map putAllNotPresent(java.util.Map target, java.util.Map source)
          Copies the key/value pairs of a Map into another Map, adding only key/value pairs for which the target Map does not already have a mapping (as determined by the containsKey method).
static java.util.Map putCombinedList(java.util.Map map, java.lang.Object key, java.lang.Object value)
          Put a value into a Map, combining with any existing value as though combineAsLists() had been called on the existing value and new value.
static java.util.Map putMultiple(java.util.Map map, java.lang.Object key, java.lang.Object value)
          Puts a key/value pair into a Map, if there is already a value under that key, create a List to hold the values.
static java.util.Map putMultipleAsList(java.util.Map map, java.lang.Object key, java.lang.Object value)
          Works like the putMultiple method, but in this case values are always encapsualted in a List.
static boolean recursiveFileDelete(java.io.File file)
          Recursively deletes a directory.
static void registerTransformer(java.lang.Class targetType, DataTools.Transformer transformer)
          Register a Transformer.
static java.util.Map remapRow(java.util.Map row, java.util.Map remap)
          Change the keys used in a Map to a new set of keys.
static java.util.Map remapRow(java.util.Map row, java.util.Map remap, boolean keepNonRemapped)
          Change the keys used in a Map to a new set of keys.
static java.util.List remapRows(java.util.List rows, java.util.Map remap)
          Remap the keys in a List of Maps.
static java.util.List remapRows(java.util.List rows, java.util.Map remap, boolean keepNonRemapped)
          Remap the keys in a List of Maps.
static java.util.Map removeEmptyStringValuedKeys(java.util.Map map)
          Removes all keys from the passed-in map where the values are empty strings.
static java.util.Map removeNullValuedKeys(java.util.Map map)
          Removes all keys from the passed-in map where the values are null.
static java.util.Map reverseMap(java.util.Map origMap)
          Reverses a Map so that values point at keys.
static java.util.List setDisjunction(java.util.Collection one, java.util.Collection two)
          Creates a new List containing all elements from the first Collection that are not in the second Collection.
static java.util.List setIntersection(java.util.Collection one, java.util.Collection two)
          Creates a new List containing the common elements of two Collections.
static java.lang.Object setProperties(java.util.Map propertyMap, java.lang.Object bean)
          As the three-argument version of this method, but passes a null dataSource
static java.lang.Object setProperties(java.util.Map propertyMap, java.lang.Object bean, DataSource ds)
          Sets the properties for a JavaBean provided as key-value pairs in a Map.
static java.lang.Object setProperties(java.util.Map propertyMap, java.lang.Object bean, DataSource ds, DSRequest dsRequest)
          This version of setProperties accepts a DSRequest for use as context by the conversion process.
static java.util.List setUnion(java.util.List one, java.util.List two)
          Creates a new List containing all the elements of two Lists.
static java.lang.String sha(java.lang.String plaintext)
          Returns the hash of the passed-in string, encrypted using the SHA-1 algorithm
static java.util.List subsetByPrefix(java.util.List list, java.lang.String prefix)
          Returns any strings within list which begin with prefix.
static java.util.Map subsetMap(java.util.Map origMap, java.util.List keys)
          Given a Map and a List of keys, returns a subset of the Map containing the key/value pairs as specified by the List of keys.
static java.util.Map substringKeyMap(java.lang.String prefix, java.util.Map source)
          Constructs a Map of key/value pairs from the source Map whose keys start with a specific prefix.
static java.util.List toLowerCaseList(java.util.List list)
          Given a List of strings, returns a new List of lower cased strings.
 

Method Detail

keysNotPresent

public static java.util.List keysNotPresent(java.util.Map map,
                                            java.util.List keys)
Given a Map and a List of keys, returns the list of keys in the List that are not present in the Map.

Parameters:
map - the Map
keys - the List of keys
Returns:
the list of keys in the supplied List that are not present in the Map

subsetMap

public static java.util.Map subsetMap(java.util.Map origMap,
                                      java.util.List keys)
Given a Map and a List of keys, returns a subset of the Map containing the key/value pairs as specified by the List of keys.

Parameters:
origMap - the original Map
keys - the keys denoting the key/value pairs that should be included in the result Map
Returns:
the subset of the original Map containing only the key/value pairs corresponding to the supplied List of keys

subsetByPrefix

public static java.util.List subsetByPrefix(java.util.List list,
                                            java.lang.String prefix)
Returns any strings within list which begin with prefix. Non-string members of list are ignored.

Parameters:
list - the list of strings
prefix - strings in the list with this prefix will be returned
Returns:
a list of the strings in the supplied list that begin with the specified prefix

removeNullValuedKeys

public static java.util.Map removeNullValuedKeys(java.util.Map map)
Removes all keys from the passed-in map where the values are null.

Parameters:
map - the Map on which to operate

removeEmptyStringValuedKeys

public static java.util.Map removeEmptyStringValuedKeys(java.util.Map map)
Removes all keys from the passed-in map where the values are empty strings.

Parameters:
map - the Map on which to operate

divideMap

public static java.util.Map divideMap(java.util.Map origMap,
                                      java.util.List retainKeys)
Alters a Map so that it retains only the specified keys, returning a Map with any keys not retained.

Parameters:
origMap - the original Map
retainKeys - the List of keys corresponding to the key/value pairs that should be retained
Returns:
a Map containing the key/value pairs that were not retained

mapUnion

public static java.util.Map mapUnion(java.util.Map primary,
                                     java.util.Map secondary)
Constructs a Map containing all the key/value pairs of the Maps supplied as arguments, with the first Map taking precedence. If the same key is present in both Maps, the returned Map will contain the key/value pair of the first Map.

Parameters:
one - the first Map
two - the second Map
Returns:
the union of the two Maps

orderedMapUnion

public static java.util.LinkedHashMap orderedMapUnion(java.util.Map primary,
                                                      java.util.Map secondary)
Does the same as mapUnion, but keeps order of key/value pairs. Key/value pairs of primary Map will go first, then key/value pairs from secondary Map ignoring dublicates. Consider using LinkedHashMap for primary and secondary maps to get full value of this method.

Parameters:
one - the first Map
two - the second Map
Returns:
the union of the two Maps

deepMerge

public static java.lang.Object deepMerge(java.lang.Object source,
                                         java.lang.Object target,
                                         boolean overwrite)
                                  throws java.lang.Exception
Merges one collections data structure into another. Any nodes/items not present in the target get created. Those that are present are merged. Note that the initial source and target must be of the same type and their data must not contain conflicting types.

Parameters:
source - the Object to be copied from
target - the Object to be copied into
overwrite - controls how non-collection/non-map collisions are resolved. If true, target values are overwritten with source values. If false, target values are not overwritten.
Returns:
the merged Object
Throws:
java.lang.Exception

deepRemoveKey

public static void deepRemoveKey(java.lang.Object keyToRemove,
                                 java.lang.Object data)
                          throws java.lang.Exception
Walks a nested Collection-based datastructure and removes all instances of keyToRemove (and its associated value) from any Map objects found in the data structure.

Parameters:
keyToRemove - the key to remove
data - the nested data structure to walk
Throws:
java.lang.Exception

deepRemoveNullValues

public static void deepRemoveNullValues(java.lang.Object data)
                                 throws java.lang.Exception
Walks a nested Collection-based datastructure and removes all entities that have a null value. For Maps this means the removal of keys with a value of null, for Collections this means the removel of null elements.

Parameters:
data - the nested data structure to walk
Throws:
java.lang.Exception

deepRemoveEmptyCollections

public static void deepRemoveEmptyCollections(java.lang.Object data)
                                       throws java.lang.Exception
Walks a nested Collection-based datastructure and removes all Collections entities that are zero-sized.

Parameters:
data - the nested data structure to walk
Throws:
java.lang.Exception

mapMerge

public static java.util.Map mapMerge(java.util.Map source,
                                     java.util.Map target)
Copies the key/value pairs of a Map into another Map, replacing the key/value pair of the target Map.

Parameters:
source - the Map to be copied from
target - the Map to be copied into
Returns:
the merged Map

mapMergeNonNull

public static java.util.Map mapMergeNonNull(java.util.Map source,
                                            java.util.Map target)
Copies the key/value pairs of a Map into another Map, replacing the key/value pair of the target Map if the source value is non-null.

Parameters:
source - the Map to be copied from
target - the Map to be copied into
Returns:
the merged Map

putAllNotPresent

public static java.util.Map putAllNotPresent(java.util.Map target,
                                             java.util.Map source)
Copies the key/value pairs of a Map into another Map, adding only key/value pairs for which the target Map does not already have a mapping (as determined by the containsKey method).

Parameters:
source - the Map to be copied from
target - the Map to be copied into
Returns:
the merged Map

mapIntersection

public static java.util.Map mapIntersection(java.util.Map primary,
                                            java.util.Map secondary)
Constructs a Map containing the key/value pairs of the primary Map whose keys are present in the secondary Map.

Parameters:
primary - the primary Map
secondary - the secondary Map
Returns:
the intersection of the two Map

mapIntersectionKeys

public static java.util.List mapIntersectionKeys(java.util.Map one,
                                                 java.util.Map two)
Returns the list of keys that are present in both Maps.

Parameters:
one - the first Map
two - the second Map
Returns:
the list of keys that is the intersection of the Maps

substringKeyMap

public static java.util.Map substringKeyMap(java.lang.String prefix,
                                            java.util.Map source)
Constructs a Map of key/value pairs from the source Map whose keys start with a specific prefix. The new Map's keys are the origin keys without the prefix.

Parameters:
prefix - the prefix to be matched with the keys
source - the source Map
Returns:
the new Map

identityMap

public static java.util.Map identityMap(java.util.List list)
Stores the elements of a List as the keys and values of a Map. This can serve as a HashSet which disallows duplicates and can quickly determine whether a set has a given member.

Parameters:
list - the List to be stored
Returns:
the Map containing the uniques elements of the list

reverseMap

public static java.util.Map reverseMap(java.util.Map origMap)
Reverses a Map so that values point at keys. If more than one value in the original Map has the same key (where "same" is determined by hashcode), the multiple keys are placed into a List in the reversed Map.

Note: If in the original Map, Lists are mapped to values, and more than one List maps to the same value, this method will work incorrectly since it won't be able to tell Lists it created for multiple values from Lists that were keys in the original Map.

Parameters:
origMap - the original Map
Returns:
the reversed Map

putMultiple

public static java.util.Map putMultiple(java.util.Map map,
                                        java.lang.Object key,
                                        java.lang.Object value)
Puts a key/value pair into a Map, if there is already a value under that key, create a List to hold the values.

Parameters:
map - the Map to which the key/value pair should be added
key - the key in the key/value pair
value - the value in the key/value pair
Returns:
the Map, with the key/value pair added to it

putMultipleAsList

public static java.util.Map putMultipleAsList(java.util.Map map,
                                              java.lang.Object key,
                                              java.lang.Object value)
Works like the putMultiple method, but in this case values are always encapsualted in a List.

Parameters:
map - the Map to which the key/value pair should be added
key - the key in the key/value pair
value - the value in the key/value pair
Returns:
the Map, with the key/value pair added to it

addAll

public static java.util.List addAll(java.util.List target,
                                    java.util.List source)
Adds the contents of one List to another.

Parameters:
target - the List to which the contents of the source List should be added
source - the List to be added to the target List
Returns:
the combined List

addAll

public static java.util.List addAll(java.util.List target,
                                    java.util.Iterator source)
Adds all the elements of an Iterator to a List.

Parameters:
target - the List to which the contents of the source List should be added
source - the List to be added to the target List
Returns:
the combined List

addAsList

public static java.util.List addAsList(java.util.List targetList,
                                       java.lang.Object sourceList)
Add either a single object or List to another List. If sourceList is a List, adds its items, if it's an Object, just adds the Object itself. Returns the modified targetList.


combineAsLists

public static java.lang.Object combineAsLists(java.lang.Object one,
                                              java.lang.Object two)
Combine two single objects or Lists into one new List, or return a single Object if one argument is single and the other argument is null.


putCombinedList

public static java.util.Map putCombinedList(java.util.Map map,
                                            java.lang.Object key,
                                            java.lang.Object value)
Put a value into a Map, combining with any existing value as though combineAsLists() had been called on the existing value and new value.


setUnion

public static java.util.List setUnion(java.util.List one,
                                      java.util.List two)
Creates a new List containing all the elements of two Lists.

Parameters:
one - the first List
two - the second List
Returns:
the combined List

addDisjunctionToSet

public static void addDisjunctionToSet(java.util.List one,
                                       java.util.List two)
Adds elements of the second List that are missing in the first. If the first or second List is null, the first will be unchanged.

Parameters:
one - the first List
two - the second List

arrayUnion

public static java.lang.Object[] arrayUnion(java.lang.Object[] one,
                                            java.lang.Object[] two)
Creates a new array containing all the elements of two arrays.

Parameters:
one - the first array
two - the second array
Returns:
the combined array

setIntersection

public static java.util.List setIntersection(java.util.Collection one,
                                             java.util.Collection two)
Creates a new List containing the common elements of two Collections.

Parameters:
one - the first Collection
two - the second Collection
Returns:
the common elements

mapDisjunction

public static java.util.Map mapDisjunction(java.util.Map one,
                                           java.util.Map two)
Creates a new Map containing the key/value pairs whose keys are not present in both Maps supplied.

Parameters:
one - the first Map
two - the second Map
Returns:
the Map of the disjunction

setDisjunction

public static java.util.List setDisjunction(java.util.Collection one,
                                            java.util.Collection two)
Creates a new List containing all elements from the first Collection that are not in the second Collection.

Parameters:
one - the first Collection
two - the second Collection
Returns:
the List of elements present in the first List that are not in the second List

enumToList

public static java.util.List enumToList(java.util.Iterator i)
Converts an Iterator to a List.

Parameters:
i - the Iterator
Returns:
the List of Iterator elements

arrayToList

public static java.util.List arrayToList(java.lang.Object[] arr,
                                         int from,
                                         int length)
Creates a List containing a subset of an array.

Parameters:
arr - the source array
from - the array index of the first element of the subset
length - the number of elements in the subset
Returns:
the List of elements from the subset of the source array

arrayToList

public static java.util.List arrayToList(java.lang.Object[] arr)
Creates a List from an array.

Parameters:
arr - the source array
Returns:
the List of elements from the source array

listToArray

public static java.lang.Object[] listToArray(java.util.List list)
Creates an Object array from a List.

Parameters:
list - the source List
Returns:
the Object array containing the elements of the source list

listToStringArray

public static java.lang.String[] listToStringArray(java.util.Collection list)
Creates a String array from a List.

Parameters:
list - the source List
Returns:
the array of Strings from the source List

mapToProperties

public static java.util.Properties mapToProperties(java.util.Map map)
Creates a Properties object from a Map. The keys and values in the map must be strings.

Parameters:
map - the source Map
Returns:
the Properties object containing the keys/values of the source Map as properties

keysAsList

public static java.util.List keysAsList(java.util.Map map)
Creates a List of the keys from a Map.

Parameters:
map - the source Map
Returns:
the list of keys from the source Map

makeList

public static java.util.List makeList(java.lang.Object element)
Creates a List containing one element.

Parameters:
element - the element
Returns:
the List containing the element

makeListIfSingle

public static java.util.List makeListIfSingle(java.lang.Object obj)
Creates a List containing one element if the element is not a List.

Parameters:
obj - the element
Returns:
the List

contains

public static boolean contains(java.lang.String str,
                               java.lang.String substr)
Tests if the specified substring is present in the specified string.

Parameters:
str - the enclosing string
substr - the substring
Returns:
true if the substring is contained in the string, false otherwise

jxPathContext

public static java.lang.Object jxPathContext(java.lang.Object obj)
Returns JXPathContext in lenient mode

Parameters:
obj - the context bean
Returns:
JXPathContext instance

nestedGet

public static java.lang.Object nestedGet(java.lang.Object struct,
                                         java.lang.String fetch)
Retrieves a object from a series of nested Lists or Map. The location of the structure is described by a string of Map keys or List indexes, separated by periods.

For example, specifying the string "dog.2" would retrieve the object with key "dog" from the supplied Map; this object should be a List - the second element of this List would be retrieved and returned.

Parameters:
struct - the enclosing List or Map
fetch - the string describing the location of the desired object
Returns:
the desired object
Throws:
java.lang.ClassCastException - if any of the enclosing data structures is neither a Map nor a List

buildList

public static java.util.List buildList(java.lang.Object one)
Creates a List of one elements. This method is useful because new ArrayList().add() does not return the List.

Parameters:
one - the first element
two - the second element
Returns:
the List containing the two elements

buildList

public static java.util.List buildList(java.lang.Object one,
                                       java.lang.Object two)
Creates a List of two elements.

Parameters:
one - the first element
two - the second element
Returns:
the List containing the two elements

buildList

public static java.util.List buildList(java.lang.Object one,
                                       java.lang.Object two,
                                       java.lang.Object three)
Creates a List of three elements.

Parameters:
one - the first element
two - the second element
three - the third element
Returns:
the List containing the three elements

buildList

public static java.util.List buildList(java.lang.Object one,
                                       java.lang.Object two,
                                       java.lang.Object three,
                                       java.lang.Object four)
Creates a List of four elements.

Parameters:
one - the first element
two - the second element
three - the third element
four - the fourth element
Returns:
the List containing the four elements

buildMap

public static java.util.Map buildMap(java.lang.Object key,
                                     java.lang.Object value)
Creates a Map of one key/value pair.

Parameters:
key - the key
value - the value
Returns:
the Map containing the key/value pair

buildMap

public static java.util.Map buildMap(java.lang.Object key,
                                     java.lang.Object value,
                                     java.lang.Object key2,
                                     java.lang.Object value2)
Creates a Map of two key/value pairs.

Parameters:
key - the first key
value - the first value
key2 - the second key
value2 - the second value
Returns:
the Map containing the two key/value pairs

buildMap

public static java.util.Map buildMap(java.lang.Object key,
                                     java.lang.Object value,
                                     java.lang.Object key2,
                                     java.lang.Object value2,
                                     java.lang.Object key3,
                                     java.lang.Object value3)
Creates a Map of three key/value pairs.

Parameters:
key - the first key
value - the first value
key2 - the second key
value2 - the second value
key3 - the third key
value3 - the third value
Returns:
the Map containing the three key/value pairs

buildMap

public static java.util.Map buildMap(java.lang.Object key,
                                     java.lang.Object value,
                                     java.lang.Object key2,
                                     java.lang.Object value2,
                                     java.lang.Object key3,
                                     java.lang.Object value3,
                                     java.lang.Object key4,
                                     java.lang.Object value4)
Creates a Map of four key/value pairs.

Parameters:
key - the first key
value - the first value
key2 - the second key
value2 - the second value
key3 - the third key
value3 - the third value
key4 - the fourth key
value4 - the fourth value
Returns:
the Map containing the four key/value pairs

buildMap

public static java.util.Map buildMap(java.lang.Object key,
                                     java.lang.Object value,
                                     java.lang.Object key2,
                                     java.lang.Object value2,
                                     java.lang.Object key3,
                                     java.lang.Object value3,
                                     java.lang.Object key4,
                                     java.lang.Object value4,
                                     java.lang.Object key5,
                                     java.lang.Object value5)
Creates a Map of five key/value pairs.

Parameters:
key - the first key
value - the first value
key2 - the second key
value2 - the second value
key3 - the third key
value3 - the third value
key4 - the fourth key
value4 - the fourth value
key5 - the fifth key
value5 - the fifth value
Returns:
the Map containing the five key/value pairs

buildMap

public static java.util.Map buildMap(java.lang.Object key,
                                     java.lang.Object value,
                                     java.lang.Object key2,
                                     java.lang.Object value2,
                                     java.lang.Object key3,
                                     java.lang.Object value3,
                                     java.lang.Object key4,
                                     java.lang.Object value4,
                                     java.lang.Object key5,
                                     java.lang.Object value5,
                                     java.lang.Object key6,
                                     java.lang.Object value6)
Creates a Map of six key/value pairs.

Parameters:
key - the first key
value - the first value
key2 - the second key
value2 - the second value
key3 - the third key
value3 - the third value
key4 - the fourth key
value4 - the fourth value
key5 - the fifth key
value5 - the fifth value
key6 - the sixth key
value6 - the sixth value
Returns:
the Map containing the six key/value pairs

buildMap

public static java.util.Map buildMap(java.lang.Object key,
                                     java.lang.Object value,
                                     java.lang.Object key2,
                                     java.lang.Object value2,
                                     java.lang.Object key3,
                                     java.lang.Object value3,
                                     java.lang.Object key4,
                                     java.lang.Object value4,
                                     java.lang.Object key5,
                                     java.lang.Object value5,
                                     java.lang.Object key6,
                                     java.lang.Object value6,
                                     java.lang.Object key7,
                                     java.lang.Object value7)
Creates a Map of six key/value pairs.

Parameters:
key - the first key
value - the first value
key2 - the second key
value2 - the second value
key3 - the third key
value3 - the third value
key4 - the fourth key
value4 - the fourth value
key5 - the fifth key
value5 - the fifth value
key6 - the sixth key
value6 - the sixth value
key7 - the seventh key
value7 - the seventh value
Returns:
the Map containing the six key/value pairs

getSingle

public static java.lang.Object getSingle(java.lang.Object toFetchFrom)
Retrieves the element of a single element List, or the key of a single key/value pair Map. Returns null if the toFetchFrom argument is not a List or Map, or if toFetchFrom does not contain a single element.

Parameters:
toFetchFrom - the source List or Map
Returns:
the single element or key

checkSize

public static int checkSize(java.lang.Object obj)
Obtains the size of a Map, List or String.

Parameters:
obj - the Map, List or String
Returns:
the size of the Map, List or String, as an int or 0 if the object is of another class

fastDateFormat

public static java.lang.String fastDateFormat(java.util.Date date)
Returns a String representation of a Date object. This method is faster than using the SimpleDateFormat methods.

Parameters:
date - the date to be formatted
Returns:
the formatted date as a String

getStackTrace

public static java.lang.String getStackTrace(java.lang.Throwable t)
Returns the stack trace of a Throwable object as a String.

Parameters:
t - the Throwable object
Returns:
the stack trace for t as a String

remapRow

public static java.util.Map remapRow(java.util.Map row,
                                     java.util.Map remap,
                                     boolean keepNonRemapped)
Change the keys used in a Map to a new set of keys. Given an existing Map and a mapping from old keys to new keys, create a new Map that uses the new keys.

Parameters:
row - the row, a Map
remap - the map of original to new column names
keepNonRemapped - boolean indicating whether to keep data under keys that are not remapped
Returns:
the row with remapped keys, or the original row, if remap is null

remapRow

public static java.util.Map remapRow(java.util.Map row,
                                     java.util.Map remap)
Change the keys used in a Map to a new set of keys. Given an existing Map and a mapping from old keys to new keys, create a new Map that uses the new keys. Columns from the original row that were not remapped will be retained.

Parameters:
row - the row, a Map
remap - the Map of original to new column names
Returns:
the row with remapped keys, or the original row, if remap is null

remapRows

public static java.util.List remapRows(java.util.List rows,
                                       java.util.Map remap,
                                       boolean keepNonRemapped)
Remap the keys in a List of Maps.

Parameters:
rows - the List of rows
remap - the Map of original to new column names
keepNonRemapped - boolean indicating whether to keep data under keys that are not remapped
Returns:
the result set with remapped columns, or the original List, if remap is null

remapRows

public static java.util.List remapRows(java.util.List rows,
                                       java.util.Map remap)
Remap the keys in a List of Maps. Columns from the original row that were not remapped will be retained.

Parameters:
rows - the List of rows
remap - the Map of original to new column names
Returns:
the result set with remapped columns, or the original List, if remap is null

getProperty

public static java.util.List getProperty(java.util.List rows,
                                         java.lang.Object column)
Given a result set, as a List of rows, and a column name, returns the List of values for that column.

The result set is represented as a List of Map objects, with each Map representing a row of data. The keys and values in each row correspond to the column names and data, respectively.

For rows that do not have a value for the specified column, no value will be present in the return list, in other words, an empty object will not be added for that row.

Parameters:
rows - the List of rows
column - the column name or key
Returns:
the List of values for column

findAll

public static java.util.List findAll(java.util.List rows,
                                     java.lang.Object propertyName,
                                     java.lang.Object value)
Given a List of Maps, return all Maps that have a certain value for a key.

Parameters:
rows - the List of Maps
propertyName - the propertyName (key in the Map)
value - expected value of the propertyName (may be null)
Returns:
a List of matching Maps, or an empty list if no matches

findAll

public static java.util.List findAll(java.util.List rows,
                                     java.util.Map properties)
Given a List of Maps, return all Maps that have common key/value pairs with another Map.

Parameters:
rows - the List of Maps
properties - Map of key/value pairs to match. May not have null values.
Returns:
a List of matching Maps, or an empty list if no matches

toLowerCaseList

public static java.util.List toLowerCaseList(java.util.List list)
Given a List of strings, returns a new List of lower cased strings.

Parameters:
list - the List of strings
Returns:
the List of lower cased strings

prettyPrint

public static java.lang.String prettyPrint(java.lang.Object obj)
Formats information about an Object for viewing or printing as a String.

Parameters:
obj - the Object to be formatted for output
Returns:
the information String to be used for the Object in viewing or printing

addToIntInMap

public static void addToIntInMap(java.util.Map map,
                                 java.lang.Object key,
                                 int addition)
Add to the value of an Integer instance stored in the map "map" under key "key".

If no Integer is currently stored under "key", creates a new Integer with value 0 before adding to it.

This method exists because of the horrific chain of typecasts otherwise necessary to achieve this effect.


incrementIntInMap

public static void incrementIntInMap(java.util.Map map,
                                     java.lang.Object key)
Add to the value of an Integer instance stored in the map "map" under key "key".

If no Integer is currently stored under "key", creates a new Integer with value 0 before adding to it.

This method exists because of the horrific chain of typecasts otherwise necessary to achieve this effect.


getIntInMap

public static int getIntInMap(java.util.Map map,
                              java.lang.Object key)
Get value of an Integer instance stored in the map "map" under key "key", as the primitive type int.

Returns 0 if no Integer is currently stored in the Map.


getterName

public static java.lang.String getterName(java.lang.reflect.Method method)
Tests if method is getter and returns property name. Returns null if provided method is not getter.

Parameters:
method - Method Getter to extract property name.
Returns:
String Getter property name or null if provided method is not getter.

getClassFields

public static java.lang.reflect.Field[] getClassFields(java.lang.Class c)
Returns array of all fields declared in specified class and it's supper classes. Array contains fields with all modifiers. Returns empty array if provided parameter is null.

Parameters:
c - Class to be inspected.
Returns:
Field[] Array of class fields.

getClassMethods

public static java.lang.reflect.Method[] getClassMethods(java.lang.Class c)
Returns array of all methods declared in specified class and it's supper classes. Array contains methods with all modifiers as well as overriden methods from super classes. Returns empty array if provided parameter is null.

Parameters:
c - Class to be inspected.
Returns:
Method[] Array of class methods.

getPropertyType

public static java.lang.Class getPropertyType(java.lang.Class c,
                                              java.lang.String propertyName)
                                       throws java.beans.IntrospectionException
Returns type of specified property in provided class. Property name can be composed of sub-properties separated by dot for example: Property will be looked up in specified order:
  1. Among fields declared in specified class
  2. Among methods declared in specified class
  3. Among fields declared in all supper classes
  4. Among methods declared in all supper classes
Only getter methods are tested: method has no parameters and it's name starts with either "get" or "is".
Throws NullPointerException if either of parameters is null.

Parameters:
c - Class to be inspected.
propertyName - String Name of property.
Returns:
Class Type of property.
Throws:
java.beans.IntrospectionException - when specified class has no such property.

getCachedClass

public static java.lang.Class getCachedClass(java.lang.String className)
Returns loaded Class object for specified name. Cached class is returned if exists otherwise class object is loaded and cached. Returns null if class can not be loaded (not in class path).

Parameters:
className - String qualified class name to load.
Returns:
Class object of null if class can not be loaded (not in class path).

getRealClass

public static java.lang.Class getRealClass(java.lang.Object bean)
Checks if provided bean is a proxy and extracts real class. If bean is not a proxy class then its getClass() is returned. Current implementation checks only for Hibernate proxies. Real class is extracted via realClass = (Class) ((HibernateProxy) bean).getHibernateLazyInitializer().getPersistentClass();

Parameters:
bean - Object
Returns:
Class

getProperties

public static java.util.Map getProperties(java.lang.Object bean)
                                   throws java.lang.Exception
Gets the properties for a JavaBean and returns them as a Map.

Parameters:
bean - the JavaBean object
Returns:
the JavaBean properties as a Map
Throws:
java.lang.Exception

getPropertyDescriptors

public static java.util.Map getPropertyDescriptors(java.lang.Object bean)
                                            throws java.lang.Exception
Gets the property descriptors for a JavaBean and returns them as a Map.

Parameters:
bean - the JavaBean object
Returns:
the JavaBean property descriptors as a Map
Throws:
java.lang.Exception

getPropertyDescriptors

public static java.util.Map getPropertyDescriptors(java.lang.Class beanClass)
                                            throws java.lang.Exception
Gets the properties for a JavaBean class through introspection and returns them as a Map.

Removes properties added by byte-code instrumentation:

Parameters:
beanClass - the JavaBean class
Returns:
the JavaBean class's property descriptors as a Map
Throws:
java.lang.Exception

setProperties

public static java.lang.Object setProperties(java.util.Map propertyMap,
                                             java.lang.Object bean)
                                      throws java.lang.Exception
As the three-argument version of this method, but passes a null dataSource

Parameters:
propertyMap - a Map providing the key-value pairs
bean - the JavaBean object
Returns:
the JavaBean properties as a Map
Throws:
java.lang.IllegalArgumentException - if a value provided in the Map cannot be converted to an appropriate type
java.lang.Exception

setProperties

public static java.lang.Object setProperties(java.util.Map propertyMap,
                                             java.lang.Object bean,
                                             DataSource ds)
                                      throws java.lang.Exception
Sets the properties for a JavaBean provided as key-value pairs in a Map. This method performs automatic type detection and conversion where possible. Conversion is done via standard bean semantics. Additionally, some standard conversions are performed automatically - for example inbound java.util.Date object will be auto-converted to java.sql.Date or java.sql.Timestamp.

Keys for which conversion fails or for which there are no corresponding Bean properties are ignored.

For properties that have primitive value on the target bean (eg properties of int type), generally, the static "valueOf(String)" method for the primitive type (eg Boolean.valueOf(String)) will be invoked on the result of "toString()" for the Object being assigned to the primitive property.

As special cases the null string ("") and the null value will become 0 when assigned to numeric primitive types (which cannot be null).

If the null string is assigned to a non-primitive numeric type (eg Long), the property will be assigned null.

If the target bean property is an enum, this method attempts to set an appropriate value by use of the enum translate strategy set by calling setEnumTranslateStrategy() on the dataSource, if one is provided. See DataSource.setEnumTranslateStrategy(String). If there is no DataSource, or it cannot set a value by cleanly using the enumTranslateStrategy, it will attempt to infer a value by considering the incoming value first as a String to match against the enumerated type's value names, and failing that as an integer to match against the enumerated type's values as an ordinal value. If that fails, it sets the target bean property to null.

Otherwise, if the type accepted by a setter method on the target bean cannot accept the type of the value in the propertyMap, this method will attempt type conversion by looking for a constructor on the setter method type that accepts instances of the value's type.

Also, if a Map value is found in the values of the passed in propertyMap, and the recipient field on the bean is not a Map, this method will treat the target field as a bean, attempting to create the bean via newInstance() and populating it via a recursive call to this method. In other words, this method will auto-create and populate inner beans.

Similarly if a Collection is found in the propertyMap, it will be applied to a target field of Collection type. If the target field is of an abstract Collection type, the default types used are:

Java Generics declarations will be automatically used to determine the type of each member of any collection. For example, a bean property declared of type List will be populated automatically with instances of SomeBean.

A DataSource can be optionally passed as the third argument and can modify some of the above behaviors, including choice of concrete type when populating bean properties that declare only an abstract type or base type - See DataSource.setProperties(Map, Object) for details.

Parameters:
propertyMap - a Map providing the key-value pairs
bean - the JavaBean object
dataSource - a context DataSource, if applicable. Can be null
Returns:
the JavaBean object
Throws:
java.lang.IllegalArgumentException - if a value provided in the Map cannot be converted to an appropriate type
java.lang.Exception

setProperties

public static java.lang.Object setProperties(java.util.Map propertyMap,
                                             java.lang.Object bean,
                                             DataSource ds,
                                             DSRequest dsRequest)
                                      throws java.lang.Exception
This version of setProperties accepts a DSRequest for use as context by the conversion process. In particular, the DSRequest is passed to the getDataSource() method of a DynamicDSGenerator.

Parameters:
propertyMap - a Map providing the key-value pairs
bean - the JavaBean object
dataSource - a context DataSource, if applicable
dsRequest - a context DSRequest, if applicable
Returns:
the JavaBean object
Throws:
java.lang.IllegalArgumentException - if a value provided in the Map cannot be converted to an appropriate type
java.lang.Exception

registerTransformer

public static void registerTransformer(java.lang.Class targetType,
                                       DataTools.Transformer transformer)
Register a Transformer. The Transformer interface specifies a single transform() method. The transform() method must be capable of creating an object of type targetType from the input Object. In a simple case, this could typically be done by making use of Object.toString.

Parameters:
targetType - the Class that this Transformer is capable of transforming to
transformer - the Transformer object itself

castValue

public static java.lang.Object castValue(java.lang.Object value,
                                         java.lang.Class targetType,
                                         DataSource ds)
Converts specified value to targetType.
Implemented conversion rules:

Parameters:
value - Object value to be converted.
targetType - Class conversion target type.
ds - DataSource initiating conversion.
Returns:
Object converted value.
Throws:
java.lang.ClassCastException - if value can not be converted or parsing exception occurs.

isTextType

public static boolean isTextType(java.lang.Class clazz)
Returns true if provided class is either assignable to Character or assignable to String or represents primitive char type.

Parameters:
clazz - Class for testing.
Returns:
boolean true if specified class represents class for text; false - for all other classes.

isNumberType

public static boolean isNumberType(java.lang.Class clazz)
Returns true if provided class is either assignable to Number or represents one of primitive number types byte, short, int, long, float, double.

Parameters:
clazz - Class for testing.
Returns:
boolean true if specified class represents class for numbers; false - for all other classes.

recursiveFileDelete

public static boolean recursiveFileDelete(java.io.File file)
Recursively deletes a directory.

Parameters:
file - file or directory to delete
Returns:
true if successful, false otherwise cannot be converted to an appropriate type

md5

public static java.lang.String md5(java.lang.String plaintext)
                            throws java.lang.Exception
Returns the hash of the passed-in string, encrypted using the MD5 algorithm

Parameters:
plaintext - the text to encrypt
Returns:
the encrypted (MD5) hash of the supplied plaintext
Throws:
java.lang.Exception

sha

public static java.lang.String sha(java.lang.String plaintext)
                            throws java.lang.Exception
Returns the hash of the passed-in string, encrypted using the SHA-1 algorithm

Parameters:
plaintext - the text to encrypt
Returns:
the encrypted (SHA) hash of the supplied plaintext
Throws:
java.lang.Exception

hashValue

public static java.lang.String hashValue(java.lang.String plaintext)
                                  throws java.lang.Exception
Returns the hash of the passed-in string, encrypted using the MD5 algorithm

Parameters:
plaintext - the text to encrypt
Returns:
the encrypted (MD5) hash of the supplied plaintext
Throws:
java.lang.Exception

hashValue

public static java.lang.String hashValue(java.lang.String plaintext,
                                         java.lang.String algorithm)
                                  throws java.lang.Exception
Returns the hash of the passed-in string, encrypted using the specified algorithm

Parameters:
plaintext - the text to encrypt
algorithm - the encryption algorithm to use. Search the client-side documentation for "HashAlgorithm" for details.
Returns:
the encrypted hash of the supplied plaintext
Throws:
java.lang.Exception

formatDate

public static java.lang.String formatDate(java.lang.String format,
                                          java.util.Date date)
Returns the parameter date formatted according to the parameter format string. This method is trivial - it just creates a SimpleDateFormat object and calls its format() method - but it is useful for formatting dates in Velocity expressions

Parameters:
format - A valid SimpleDateFormat format string
date - The date to format
Returns:
The date formatted according to the format string

getLoggerRepository

public static org.apache.log4j.spi.LoggerRepository getLoggerRepository()
Returns log4j LoggerRepository instance that was configured from log4j.isc.config.xml. Use this method to access log4j loggers for custom categories defined in this file.

Returns:
configured log4j LoggerRepository instance