|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmessif.utility.Convert
public abstract class Convert
Utility class that provides methods for type conversions and instantiation.
Constructor Summary | |
---|---|
Convert()
|
Method Summary | ||
---|---|---|
static
|
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
|
createGenericArray(java.lang.Class<T> componentType,
int size)
Returns a new instance of a static array. |
|
static
|
createGenericArray(T[] array,
int size)
Returns a new instance of a static array. |
|
static
|
enumToMap(java.lang.Class<E> values)
Returns a table of all values from the specified enum keyed by the enum value's name. |
|
static
|
genericCastToClass(java.lang.Object classObject,
java.lang.Class<E> checkClass)
Cast the provided object to Class with generics typing. |
|
static
|
getClassForName(java.lang.String name,
java.lang.Class<E> checkClass)
Class loader with type check. |
|
static
|
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
|
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
|
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
|
removeFromArray(T[] original,
T item)
Removes an item from the specified static array (shrinking its size by one). |
|
static
|
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
|
safeGenericCastMap(java.lang.Object object,
java.lang.Class<K> keysClass,
java.lang.Class<V> valuesClass)
Return generic-safe Map type. |
|
static
|
searchArray(T[] array,
T item,
boolean backwards)
Search the array for the specified item. |
|
static
|
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
|
stringToMap(java.lang.String string,
java.lang.Class<E> valueType)
Returns a map of string key-value pairs parsed from the specified string. |
|
static
|
stringToType(java.lang.String string,
java.lang.Class<E> type)
Converts a string into object of the specified type. |
|
static
|
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
|
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 |
---|
public Convert()
Method Detail |
---|
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
Currently supported types are:
String
Class
AbstractStreamObjectIterator
- parameter represents the name of an opened stream from objectStreams
AbstractObjectList
- parameter represents the name of an opened stream from objectStreams
, the number of objects to read can be specified after a colonLocalAbstractObject
- parameter represents the name of an opened stream from objectStreams
, the next object is acquiredstringToType(java.lang.String, java.lang.Class, java.util.Map)
into the array's itemsMap
with String
key and value - parameter should be comma-separated key=value pairs (possibly quoted)String
parametervalueOf(String parameter)
factory method, e.g., Integer.valueOf(java.lang.String)
E
- the type of the value to returnstring
- the string value to be convertedtype
- the class of the valuenamedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the map
java.lang.InstantiationException
- if the type cannot be created from the string valuepublic static <E> E stringToType(java.lang.String string, java.lang.Class<E> type) throws java.lang.InstantiationException
E
- the type of the value to returnstring
- the string value to be convertedtype
- the class of the value
java.lang.InstantiationException
- if the type cannot be created from the string valuepublic static <T> T stringAndClassToType(java.lang.String classAndData, char separator, java.lang.Class<? extends T> checkClass) throws java.lang.IllegalArgumentException
classAndData
.
The class name is separated from the data using separator
.
The data are fed into public constructor with one String
argument.
T
- the class, super class, or the interface of the created instanceclassAndData
- the string containing the class name followed by the dataseparator
- separates the class name from the datacheckClass
- the class, super class, or the interface of the created instance
java.lang.IllegalArgumentException
- if the instance cannot be created (see the message to get the reason)public static <T> java.lang.Class<T> wrapPrimitiveType(java.lang.Class<T> type)
T
- a primitive type classtype
- a primitive type class
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
one = 1, "two"=2,"three"="3", four=null
The values are converted using the stringToType
method to the specified valueType
.
E
- the class of values in the mapstring
- the string value to be convertedmap
- a table to which the string key-value pairs are addedvalueType
- the class of values in the map
java.lang.InstantiationException
- if the conversion of a value has failedpublic static <E> java.util.Map<java.lang.String,E> stringToMap(java.lang.String string, java.lang.Class<E> valueType) throws java.lang.InstantiationException
one = 1, "two"=2,"three"="3", four=null
The values are converted using the stringToType
method to the specified valueType
.
E
- the class of values in the mapstring
- the string value to be convertedvalueType
- the class of values in the map
java.lang.InstantiationException
- if the conversion of a value has failedpublic static java.util.Map<java.lang.String,java.lang.String> stringToMap(java.lang.String string)
one = 1, "two"=2,"three"="3", four=null
string
- the string value to be converted
public static java.lang.Object getRandomValue(java.lang.Object minVal, java.lang.Object maxVal)
minVal
- the minimal value (interval's lower bound)maxVal
- the maximal value (interval's upper bound)
public static int getPrimitiveTypeSize(java.lang.Class<?> type) throws java.lang.IllegalArgumentException
type
- the primitive type for which to get the size
java.lang.IllegalArgumentException
- if the specified class is not a primitive typepublic static void writePrimitiveTypeToDataStream(java.io.ObjectOutputStream stream, java.lang.Object value, java.lang.Class<?> type) throws java.io.IOException
stream
- the stream to write the data tovalue
- the value to write to the streamtype
- the type of the data to be written
java.io.IOException
- if there was an error writing to the streampublic static java.lang.Object readPrimitiveTypeFromDataStream(java.io.ObjectInputStream stream, java.lang.Class<?> type) throws java.io.IOException
stream
- the stream to read the data fromtype
- the type of data to be read
java.io.IOException
- if there was an error writing to the streampublic static <E> java.lang.Class<E> getClassForName(java.lang.String name, java.lang.Class<E> checkClass) throws java.lang.ClassNotFoundException
E
- the type of the returned classname
- the fully qualified name of the classcheckClass
- the superclass of the returned class for the generics check
Class
object associated with the class or
interface with the given string name
java.lang.ClassNotFoundException
- if the class cannot be locatedpublic static <E> java.lang.Class<E> genericCastToClass(java.lang.Object classObject, java.lang.Class<E> checkClass) throws java.lang.ClassCastException
ClassCastException
is thrown
even if the provided classObject
is a valid Class
.
E
- the type of the returned objectclassObject
- the class object to be castcheckClass
- the generics typed class that is returned
Class
object
java.lang.ClassCastException
- if passed classObject
is not subclass of checkClass
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
argStartIndex
to argEndIndex
of strings
array will be used.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted toargStartIndex
- index in the strings array which denotes the first changable argumentargEndIndex
- index in the strings array which denotes the last changable argumentnamedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the map
java.lang.InstantiationException
- if there was a type that cannot be created from the provided string valuepublic static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, int argStartIndex, int argEndIndex) throws java.lang.InstantiationException
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.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted toargStartIndex
- index in the strings array which denotes the first changable argumentargEndIndex
- index in the strings array which denotes the last changable argument
java.lang.InstantiationException
- if there was a type that cannot be created from the provided string valuepublic static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, int argStartIndex) throws java.lang.InstantiationException
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.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted toargStartIndex
- index in the strings array which denotes the first changable argument
java.lang.InstantiationException
- if there was a type that cannot be created from the provided string valuepublic 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
argStartIndex
till the end of strings
array will
be used.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted toargStartIndex
- index in the strings array which denotes the first changable argumentnamedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the map
java.lang.InstantiationException
- if there was a type that cannot be created from the provided string valuepublic static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types) throws java.lang.InstantiationException
strings
array will be used.
Note that LocalAbstractObject
parameters will not be converted. For this fuctionality,
use the full parseTypesFromString
method instead.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted to
java.lang.InstantiationException
- if there was a type that cannot be created from the provided string valuepublic 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
strings
array will be used.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted tonamedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the map
java.lang.InstantiationException
- if there was a type that cannot be created from the provided string valuepublic static java.lang.Class<?>[] getObjectTypes(java.lang.Object... objects)
objects
- list of objects to get the types for
public static <E> java.lang.reflect.Constructor<E>[] getConstructors(java.lang.Class<? extends E> objectClass)
E
- the class for which to get the constructorsobjectClass
- the class for which to get the constructors
public static <T> T[] createGenericArray(java.lang.Class<T> componentType, int size)
T
- the type of components of the new arraycomponentType
- the class of components of the new arraysize
- the number of elements of the new array
public static <T> T[] createGenericArray(T[] array, int size)
T
- the type of components of the new arrayarray
- a generic array according to which to get the component typesize
- the number of elements of the new array
public static <T> T[] resizeArray(T[] original, int newLength, java.lang.Class<T> componentType) throws java.lang.NegativeArraySizeException
T
- the type of objects in the arrayoriginal
- the array to be copiednewLength
- the length of the copy to be returnedcomponentType
- the class of array components
java.lang.NegativeArraySizeException
- if newLength is negativepublic static <T> T[] addToArray(T[] original, java.lang.Class<T> componentType, T item)
T
- the type of objects in the arrayoriginal
- the array where the item is addedcomponentType
- the class of array componentsitem
- the item to add
public static <T> int searchArray(T[] array, T item, boolean backwards)
T
- the type of objects in the arrayarray
- the array to searchitem
- the item to search forbackwards
- if set to true, the search is started from the last element
public static <T> T[] removeFromArray(T[] original, T item)
T
- the type of objects in the arrayoriginal
- the array from which the item is removeditem
- the item to remove
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
Map
type.
Note that only non-typed arguments can be passed as key/value class to be
generic-safe.
K
- the type of keys in the returned mapV
- the type of values in the returned mapobject
- the object that sould be cast to other typekeysClass
- the class to cast the map keys tovaluesClass
- the class to cast the map values to
java.lang.ClassCastException
- if there was an incompatible key or value or the object is not Map
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
String
), the stringToType(java.lang.String, java.lang.Class, java.util.Map)
conversion is tried.
T
- the type of the returned parameter valueparameters
- the parameter tableparamName
- the name of parameter to get from the table; if there is no parameter of thatparamClass
- the class to cast the value todefaultValue
- the deault value to return if there is no parameter
java.lang.ClassCastException
- if there was an incompatible key or value or the object cannot be converted from stringpublic 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)
string
- the string to be modifiedvariableRegex
- regular expression that matches variablesvariableRegexGroup
- parenthesis group within regular expression
that holds the variable namedefaultValueRegexGroup
- parenthesis group within regular expression
that holds the default value for a variable that is not present
in the variables
mapvariables
- the variable names with their values
public static long timeToMiliseconds(java.lang.String time) throws java.lang.NumberFormatException
time
- the time in "hh:mm:ss.iii" format (seconds and miliseconds are optional)
java.lang.NumberFormatException
- if the specified time has invalid formatpublic static <E extends java.lang.Enum<E>> java.util.Map<java.lang.String,E> enumToMap(java.lang.Class<E> values)
E
- the enum typevalues
- the class of the enum for which to get the map
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |