messif.utility
Class Convert

java.lang.Object
  extended by messif.utility.Convert

public abstract class Convert
extends java.lang.Object

Utility class that provides methods for type conversions and instantiation.


Constructor Summary
Convert()
           
 
Method Summary
static
<T> T[]
addToArray(T[] original, java.lang.Class<T> componentType, T item)
          Adds an item to the end of a specified static array (enlarging its size by one).
static
<T> T[]
createGenericArray(java.lang.Class<T> componentType, int size)
          Returns a new instance of a static array.
static
<T> T[]
createGenericArray(T[] array, int size)
          Returns a new instance of a static array.
static
<E extends java.lang.Enum<E>>
java.util.Map<java.lang.String,E>
enumToMap(java.lang.Class<E> values)
          Returns a table of all values from the specified enum keyed by the enum value's name.
static
<E> java.lang.Class<E>
genericCastToClass(java.lang.Object classObject, java.lang.Class<E> checkClass)
          Cast the provided object to Class with generics typing.
static
<E> java.lang.Class<E>
getClassForName(java.lang.String name, java.lang.Class<E> checkClass)
          Class loader with type check.
static
<E> java.lang.reflect.Constructor<E>[]
getConstructors(java.lang.Class<? extends E> objectClass)
          Returns type-safe public constructors of the given class.
static java.lang.Class<?>[] getObjectTypes(java.lang.Object... objects)
          Retrieves types of the objects in parameters
static
<T> T
getParameterValue(java.util.Map<java.lang.String,?> parameters, java.lang.String paramName, java.lang.Class<T> paramClass, T defaultValue)
          Returns an object from parameter table.
static int getPrimitiveTypeSize(java.lang.Class<?> type)
          Returns the number of bits used to represent the specified primitive class.
static java.lang.Object getRandomValue(java.lang.Object minVal, java.lang.Object maxVal)
          Returns a random value from the interval between minVal and maxVal.
static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types)
          Parses array of strings into array of objects accoring to the types provided in the second argument.
static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, int argStartIndex)
          Parses array of strings into array of objects accoring to the types provided in the second argument.
static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, int argStartIndex, int argEndIndex)
          Parses array of strings into array of objects accoring to the types provided in the second argument.
static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, int argStartIndex, int argEndIndex, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Parses array of strings into array of objects accoring to the types provided in the second argument.
static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, int argStartIndex, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Parses array of strings into array of objects accoring to the types provided in the second argument.
static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Parses array of strings into array of objects accoring to the types provided in the second argument.
static
<E> void
putStringIntoMap(java.lang.String string, java.util.Map<? super java.lang.String,? super E> map, java.lang.Class<E> valueType)
          Parses string key-value pairs from the specified string and adds them to the map.
static java.lang.Object readPrimitiveTypeFromDataStream(java.io.ObjectInputStream stream, java.lang.Class<?> type)
          Read a value of a primitive type (or string) from the specified input stream.
static
<T> T[]
removeFromArray(T[] original, T item)
          Removes an item from the specified static array (shrinking its size by one).
static
<T> T[]
resizeArray(T[] original, int newLength, java.lang.Class<T> componentType)
          Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
static
<K,V> java.util.Map<K,V>
safeGenericCastMap(java.lang.Object object, java.lang.Class<K> keysClass, java.lang.Class<V> valuesClass)
          Return generic-safe Map type.
static
<T> int
searchArray(T[] array, T item, boolean backwards)
          Search the array for the specified item.
static
<T> T
stringAndClassToType(java.lang.String classAndData, char separator, java.lang.Class<? extends T> checkClass)
          Creates a new instance of a class specified in the classAndData.
static java.util.Map<java.lang.String,java.lang.String> stringToMap(java.lang.String string)
          Returns a map of string key-value pairs parsed from the specified string.
static
<E> java.util.Map<java.lang.String,E>
stringToMap(java.lang.String string, java.lang.Class<E> valueType)
          Returns a map of string key-value pairs parsed from the specified string.
static
<E> E
stringToType(java.lang.String string, java.lang.Class<E> type)
          Converts a string into object of the specified type.
static
<E> E
stringToType(java.lang.String string, java.lang.Class<E> type, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Converts a string into object of the specified type.
static java.lang.String substituteVariables(java.lang.String string, java.util.regex.Pattern variableRegex, int variableRegexGroup, int defaultValueRegexGroup, java.util.Map<java.lang.String,java.lang.String> variables)
          Replace all occurances of the variable pattern with the value from the hash table.
static long timeToMiliseconds(java.lang.String time)
          Returns the today's specified time in miliseconds.
static
<T> java.lang.Class<T>
wrapPrimitiveType(java.lang.Class<T> type)
          Returns a wrapper class for primitive type.
static void writePrimitiveTypeToDataStream(java.io.ObjectOutputStream stream, java.lang.Object value, java.lang.Class<?> type)
          Write a value of a primitive type (or string) to the specified output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Convert

public Convert()
Method Detail

stringToType

public static <E> E stringToType(java.lang.String string,
                                 java.lang.Class<E> type,
                                 java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                      throws java.lang.InstantiationException
Converts a string into object of the specified type.

Currently supported types are:

Type Parameters:
E - the type of the value to return
Parameters:
string - the string value to be converted
type - the class of the value
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
the converted value
Throws:
java.lang.InstantiationException - if the type cannot be created from the string value

stringToType

public static <E> E stringToType(java.lang.String string,
                                 java.lang.Class<E> type)
                      throws java.lang.InstantiationException
Converts a string into object of the specified type.

Type Parameters:
E - the type of the value to return
Parameters:
string - the string value to be converted
type - the class of the value
Returns:
the converted value
Throws:
java.lang.InstantiationException - if the type cannot be created from the string value

stringAndClassToType

public static <T> T stringAndClassToType(java.lang.String classAndData,
                                         char separator,
                                         java.lang.Class<? extends T> checkClass)
                              throws java.lang.IllegalArgumentException
Creates a new instance of a class specified in the classAndData. The class name is separated from the data using separator. The data are fed into public constructor with one String argument.

Type Parameters:
T - the class, super class, or the interface of the created instance
Parameters:
classAndData - the string containing the class name followed by the data
separator - separates the class name from the data
checkClass - the class, super class, or the interface of the created instance
Returns:
a new instance of the specified class
Throws:
java.lang.IllegalArgumentException - if the instance cannot be created (see the message to get the reason)

wrapPrimitiveType

public static <T> java.lang.Class<T> wrapPrimitiveType(java.lang.Class<T> type)
Returns a wrapper class for primitive type. If the type is not primitive, it is returned as is.

Type Parameters:
T - a primitive type class
Parameters:
type - a primitive type class
Returns:
a wrapper class for primitive type

putStringIntoMap

public static <E> void putStringIntoMap(java.lang.String string,
                                        java.util.Map<? super java.lang.String,? super E> map,
                                        java.lang.Class<E> valueType)
                             throws java.lang.InstantiationException
Parses string key-value pairs from the specified string and adds them to the map. String contains key=value pairs (key, value or both can be quoted) that are separated by commas. For example:
one = 1, "two"=2,"three"="3", four=null

The values are converted using the stringToType method to the specified valueType.

Type Parameters:
E - the class of values in the map
Parameters:
string - the string value to be converted
map - a table to which the string key-value pairs are added
valueType - the class of values in the map
Throws:
java.lang.InstantiationException - if the conversion of a value has failed

stringToMap

public static <E> java.util.Map<java.lang.String,E> stringToMap(java.lang.String string,
                                                                java.lang.Class<E> valueType)
                                                     throws java.lang.InstantiationException
Returns a map of string key-value pairs parsed from the specified string. String contains key=value pairs (key, value or both can be quoted) that are separated by commas. For example:
one = 1, "two"=2,"three"="3", four=null

The values are converted using the stringToType method to the specified valueType.

Type Parameters:
E - the class of values in the map
Parameters:
string - the string value to be converted
valueType - the class of values in the map
Returns:
a map of string key-value pairs
Throws:
java.lang.InstantiationException - if the conversion of a value has failed

stringToMap

public static java.util.Map<java.lang.String,java.lang.String> stringToMap(java.lang.String string)
Returns a map of string key-value pairs parsed from the specified string. String contains key=value pairs (key, value or both can be quoted) that are separated by commas. For example:
one = 1, "two"=2,"three"="3", four=null

Parameters:
string - the string value to be converted
Returns:
a map of string key-value pairs

getRandomValue

public static java.lang.Object getRandomValue(java.lang.Object minVal,
                                              java.lang.Object maxVal)
Returns a random value from the interval between minVal and maxVal. The minVal and maxVal must be primitive types (or their wrapper classes).

Parameters:
minVal - the minimal value (interval's lower bound)
maxVal - the maximal value (interval's upper bound)
Returns:
a random value from the interval between minVal and maxVal

getPrimitiveTypeSize

public static int getPrimitiveTypeSize(java.lang.Class<?> type)
                                throws java.lang.IllegalArgumentException
Returns the number of bits used to represent the specified primitive class.

Parameters:
type - the primitive type for which to get the size
Returns:
the number of bits used to represent the specified primitive class
Throws:
java.lang.IllegalArgumentException - if the specified class is not a primitive type

writePrimitiveTypeToDataStream

public static void writePrimitiveTypeToDataStream(java.io.ObjectOutputStream stream,
                                                  java.lang.Object value,
                                                  java.lang.Class<?> type)
                                           throws java.io.IOException
Write a value of a primitive type (or string) to the specified output stream.

Parameters:
stream - the stream to write the data to
value - the value to write to the stream
type - the type of the data to be written
Throws:
java.io.IOException - if there was an error writing to the stream

readPrimitiveTypeFromDataStream

public static java.lang.Object readPrimitiveTypeFromDataStream(java.io.ObjectInputStream stream,
                                                               java.lang.Class<?> type)
                                                        throws java.io.IOException
Read a value of a primitive type (or string) from the specified input stream.

Parameters:
stream - the stream to read the data from
type - the type of data to be read
Returns:
the value read from the stream
Throws:
java.io.IOException - if there was an error writing to the stream

getClassForName

public static <E> java.lang.Class<E> getClassForName(java.lang.String name,
                                                     java.lang.Class<E> checkClass)
                                          throws java.lang.ClassNotFoundException
Class loader with type check.

Type Parameters:
E - the type of the returned class
Parameters:
name - the fully qualified name of the class
checkClass - the superclass of the returned class for the generics check
Returns:
the Class object associated with the class or interface with the given string name
Throws:
java.lang.ClassNotFoundException - if the class cannot be located

genericCastToClass

public static <E> java.lang.Class<E> genericCastToClass(java.lang.Object classObject,
                                                        java.lang.Class<E> checkClass)
                                             throws java.lang.ClassCastException
Cast the provided object to Class with generics typing. If the generics type check fails, the ClassCastException is thrown even if the provided classObject is a valid Class.

Type Parameters:
E - the type of the returned object
Parameters:
classObject - the class object to be cast
checkClass - the generics typed class that is returned
Returns:
the generics-typed Class object
Throws:
java.lang.ClassCastException - if passed classObject is not subclass of checkClass

parseTypesFromString

public static java.lang.Object[] parseTypesFromString(java.lang.String[] strings,
                                                      java.lang.Class<?>[] types,
                                                      int argStartIndex,
                                                      int argEndIndex,
                                                      java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                                               throws java.lang.InstantiationException
Parses array of strings into array of objects accoring to the types provided in the second argument. Only parameters from argStartIndex to argEndIndex of strings array will be used.

Parameters:
strings - array of strings that hold the values
types - array of classes that the strings should be converted to
argStartIndex - index in the strings array which denotes the first changable argument
argEndIndex - index in the strings array which denotes the last changable argument
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
the array of converted values
Throws:
java.lang.InstantiationException - if there was a type that cannot be created from the provided string value

parseTypesFromString

public static java.lang.Object[] parseTypesFromString(java.lang.String[] strings,
                                                      java.lang.Class<?>[] types,
                                                      int argStartIndex,
                                                      int argEndIndex)
                                               throws java.lang.InstantiationException
Parses array of strings into array of objects accoring to the types provided in the second argument. Only parameters from argStartIndex to argEndIndex of strings array will be used.

Note that LocalAbstractObject parameters will not be converted. For this fuctionality, use the full parseTypesFromString method instead.

Parameters:
strings - array of strings that hold the values
types - array of classes that the strings should be converted to
argStartIndex - index in the strings array which denotes the first changable argument
argEndIndex - index in the strings array which denotes the last changable argument
Returns:
the array of converted values
Throws:
java.lang.InstantiationException - if there was a type that cannot be created from the provided string value

parseTypesFromString

public static java.lang.Object[] parseTypesFromString(java.lang.String[] strings,
                                                      java.lang.Class<?>[] types,
                                                      int argStartIndex)
                                               throws java.lang.InstantiationException
Parses array of strings into array of objects accoring to the types provided in the second argument. All the parameters from argStartIndex till the end of strings array will be used.

Note that LocalAbstractObject parameters will not be converted. For this fuctionality, use the full parseTypesFromString method instead.

Parameters:
strings - array of strings that hold the values
types - array of classes that the strings should be converted to
argStartIndex - index in the strings array which denotes the first changable argument
Returns:
the array of converted values
Throws:
java.lang.InstantiationException - if there was a type that cannot be created from the provided string value

parseTypesFromString

public static java.lang.Object[] parseTypesFromString(java.lang.String[] strings,
                                                      java.lang.Class<?>[] types,
                                                      int argStartIndex,
                                                      java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                                               throws java.lang.InstantiationException
Parses array of strings into array of objects accoring to the types provided in the second argument. All the parameters from argStartIndex till the end of strings array will be used.

Parameters:
strings - array of strings that hold the values
types - array of classes that the strings should be converted to
argStartIndex - index in the strings array which denotes the first changable argument
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
the array of converted values
Throws:
java.lang.InstantiationException - if there was a type that cannot be created from the provided string value

parseTypesFromString

public static java.lang.Object[] parseTypesFromString(java.lang.String[] strings,
                                                      java.lang.Class<?>[] types)
                                               throws java.lang.InstantiationException
Parses array of strings into array of objects accoring to the types provided in the second argument. All the parameters from strings array will be used.

Note that LocalAbstractObject parameters will not be converted. For this fuctionality, use the full parseTypesFromString method instead.

Parameters:
strings - array of strings that hold the values
types - array of classes that the strings should be converted to
Returns:
the array of converted values
Throws:
java.lang.InstantiationException - if there was a type that cannot be created from the provided string value

parseTypesFromString

public static java.lang.Object[] parseTypesFromString(java.lang.String[] strings,
                                                      java.lang.Class<?>[] types,
                                                      java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                                               throws java.lang.InstantiationException
Parses array of strings into array of objects accoring to the types provided in the second argument. All the parameters from strings array will be used.

Parameters:
strings - array of strings that hold the values
types - array of classes that the strings should be converted to
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
the array of converted values
Throws:
java.lang.InstantiationException - if there was a type that cannot be created from the provided string value

getObjectTypes

public static java.lang.Class<?>[] getObjectTypes(java.lang.Object... objects)
Retrieves types of the objects in parameters

Parameters:
objects - list of objects to get the types for
Returns:
list of types of the arguments

getConstructors

public static <E> java.lang.reflect.Constructor<E>[] getConstructors(java.lang.Class<? extends E> objectClass)
Returns type-safe public constructors of the given class.

Type Parameters:
E - the class for which to get the constructors
Parameters:
objectClass - the class for which to get the constructors
Returns:
all public constructors of the given class

createGenericArray

public static <T> T[] createGenericArray(java.lang.Class<T> componentType,
                                         int size)
Returns a new instance of a static array.

Type Parameters:
T - the type of components of the new array
Parameters:
componentType - the class of components of the new array
size - the number of elements of the new array
Returns:
a new instance of a static array

createGenericArray

public static <T> T[] createGenericArray(T[] array,
                                         int size)
Returns a new instance of a static array.

Type Parameters:
T - the type of components of the new array
Parameters:
array - a generic array according to which to get the component type
size - the number of elements of the new array
Returns:
a new instance of a static array

resizeArray

public static <T> T[] resizeArray(T[] original,
                                  int newLength,
                                  java.lang.Class<T> componentType)
                       throws java.lang.NegativeArraySizeException
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of the class newType.

Type Parameters:
T - the type of objects in the array
Parameters:
original - the array to be copied
newLength - the length of the copy to be returned
componentType - the class of array components
Returns:
a copy of the original array, truncated or padded with nulls to obtain the specified length
Throws:
java.lang.NegativeArraySizeException - if newLength is negative

addToArray

public static <T> T[] addToArray(T[] original,
                                 java.lang.Class<T> componentType,
                                 T item)
Adds an item to the end of a specified static array (enlarging its size by one).

Type Parameters:
T - the type of objects in the array
Parameters:
original - the array where the item is added
componentType - the class of array components
item - the item to add
Returns:
a copy of the original array with added item

searchArray

public static <T> int searchArray(T[] array,
                                  T item,
                                  boolean backwards)
Search the array for the specified item.

Type Parameters:
T - the type of objects in the array
Parameters:
array - the array to search
item - the item to search for
backwards - if set to true, the search is started from the last element
Returns:
index of the array element, where the item was found, or -1 if it was not

removeFromArray

public static <T> T[] removeFromArray(T[] original,
                                      T item)
Removes an item from the specified static array (shrinking its size by one). The removed array element is the last one that is equal to the specified item. If the element is not found, the same array (original) is returned. If the removed element was the last one, null is returned.

Type Parameters:
T - the type of objects in the array
Parameters:
original - the array from which the item is removed
item - the item to remove
Returns:
a copy of the original array with added item

safeGenericCastMap

public static <K,V> java.util.Map<K,V> safeGenericCastMap(java.lang.Object object,
                                                          java.lang.Class<K> keysClass,
                                                          java.lang.Class<V> valuesClass)
                                             throws java.lang.ClassCastException
Return generic-safe Map type. Note that only non-typed arguments can be passed as key/value class to be generic-safe.

Type Parameters:
K - the type of keys in the returned map
V - the type of values in the returned map
Parameters:
object - the object that sould be cast to other type
keysClass - the class to cast the map keys to
valuesClass - the class to cast the map values to
Returns:
a map with generic-safe key/values
Throws:
java.lang.ClassCastException - if there was an incompatible key or value or the object is not Map

getParameterValue

public static <T> T getParameterValue(java.util.Map<java.lang.String,?> parameters,
                                      java.lang.String paramName,
                                      java.lang.Class<T> paramClass,
                                      T defaultValue)
                           throws java.lang.ClassCastException
Returns an object from parameter table. If the parameter table is null or if it does not contain the parameter with the specified name, the default value is returned. Otherwise, the class of the parametr is checked and if it is not the requested class (but it is a String), the stringToType(java.lang.String, java.lang.Class, java.util.Map) conversion is tried.

Type Parameters:
T - the type of the returned parameter value
Parameters:
parameters - the parameter table
paramName - the name of parameter to get from the table; if there is no parameter of that
paramClass - the class to cast the value to
defaultValue - the deault value to return if there is no parameter
Returns:
a value from the parameter table
Throws:
java.lang.ClassCastException - if there was an incompatible key or value or the object cannot be converted from string

substituteVariables

public static java.lang.String substituteVariables(java.lang.String string,
                                                   java.util.regex.Pattern variableRegex,
                                                   int variableRegexGroup,
                                                   int defaultValueRegexGroup,
                                                   java.util.Map<java.lang.String,java.lang.String> variables)
Replace all occurances of the variable pattern with the value from the hash table. The specified pattern group is used the the variable name.

Parameters:
string - the string to be modified
variableRegex - regular expression that matches variables
variableRegexGroup - parenthesis group within regular expression that holds the variable name
defaultValueRegexGroup - parenthesis group within regular expression that holds the default value for a variable that is not present in the variables map
variables - the variable names with their values
Returns:
the original string with all variables replaced

timeToMiliseconds

public static long timeToMiliseconds(java.lang.String time)
                              throws java.lang.NumberFormatException
Returns the today's specified time in miliseconds.

Parameters:
time - the time in "hh:mm:ss.iii" format (seconds and miliseconds are optional)
Returns:
the today's specified time in miliseconds
Throws:
java.lang.NumberFormatException - if the specified time has invalid format

enumToMap

public static <E extends java.lang.Enum<E>> java.util.Map<java.lang.String,E> enumToMap(java.lang.Class<E> values)
Returns a table of all values from the specified enum keyed by the enum value's name.

Type Parameters:
E - the enum type
Parameters:
values - the class of the enum for which to get the map
Returns:
a table of all values from the specified enum