public abstract class Convert
extends java.lang.Object
Constructor and Description |
---|
Convert() |
Modifier and Type | Method and Description |
---|---|
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 java.lang.String |
arrayToString(java.lang.Object array,
boolean deep)
Returns a string serialization of the given
array . |
static <K,V> java.util.Map<K,V> |
copyAllMapValues(java.util.Map<? extends K,?> sourceMap,
java.util.Map<K,V> destinationMap,
java.lang.Class<? extends V> valuesClass)
Copies all values from the
sourceMap to destinationMap . |
static <T,U extends T> |
copyGenericArray(U[] original,
int offset,
int length,
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> |
copyMapValues(java.util.Map<?,?> sourceMap,
java.lang.Iterable<? extends K> keysToRetrieve,
java.util.Map<K,V> destinationMap,
java.lang.Class<? extends V> valuesClass)
Copies values from the
sourceMap to destinationMap . |
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>> |
enumToMap(java.lang.Class<E> values)
Returns a table of all values from the specified enum keyed by the enum value name.
|
static java.lang.Object |
expandReferencedInstances(java.lang.Object instance)
Expand
ThreadLocal and Reference instances to their referees. |
static <E> java.lang.Class<E> |
genericCastToClass(java.lang.Object classObject,
java.lang.Class<E> checkClass)
Cast the provided object to Class with generic 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 <E> java.lang.reflect.Constructor<E>[] |
getConstructors(java.lang.Class<? extends E> objectClass,
boolean publicOnlyConstructors)
Returns type-safe 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 long |
hmsToMilliseconds(java.lang.String hmsStr)
Converts a string specifying hours/minutes/seconds time period.
|
static java.lang.String |
inetAddressToString(java.net.InetAddress host,
int port)
Returns a string representation of the given IP address and port.
|
static boolean |
isPrimitiveWritableClass(java.lang.Class<?> type,
boolean checkArrays)
Returns whether the given class is a primitive-writable.
|
static java.lang.String |
iterableToString(java.util.Iterator<?> iterator,
java.lang.String separator)
Converts the values of the given iterator to string by separating
all the values by the given separator.
|
static java.lang.String |
iterableToString(java.lang.Object iterable,
java.lang.String separator)
Converts the values of the given iterable instance to string by separating
all the values by the given separator.
|
static java.lang.Object[] |
parseTypesFromString(java.lang.String[] strings,
java.lang.Class<?>[] types,
boolean handleVarArgs)
Parses array of strings into array of objects according to the types provided in the second argument.
|
static java.lang.Object[] |
parseTypesFromString(java.lang.String[] strings,
java.lang.Class<?>[] types,
boolean handleVarArgs,
int argStartIndex)
Parses array of strings into array of objects according to the types provided in the second argument.
|
static java.lang.Object[] |
parseTypesFromString(java.lang.String[] strings,
java.lang.Class<?>[] types,
boolean handleVarArgs,
int argStartIndex,
int argEndIndex)
Parses array of strings into array of objects according to the types provided in the second argument.
|
static java.lang.Object[] |
parseTypesFromString(java.lang.String[] strings,
java.lang.Class<?>[] types,
boolean handleVarArgs,
int argStartIndex,
int argEndIndex,
java.util.Map<java.lang.String,? extends java.lang.Object> namedInstances)
Parses array of strings into array of objects according to the types provided in the second argument.
|
static java.lang.Object[] |
parseTypesFromString(java.lang.String[] strings,
java.lang.Class<?>[] types,
boolean handleVarArgs,
int argStartIndex,
java.util.Map<java.lang.String,? extends java.lang.Object> namedInstances)
Parses array of strings into array of objects according to the types provided in the second argument.
|
static java.lang.Object[] |
parseTypesFromString(java.lang.String[] strings,
java.lang.Class<?>[] types,
boolean handleVarArgs,
java.util.Map<java.lang.String,? extends java.lang.Object> namedInstances)
Parses array of strings into array of objects according 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.util.Collection<java.lang.Integer> |
rangeSelectorsToIndexes(java.lang.String rangeSelectors,
boolean removeDuplicates)
Returns a collection of integer indexes that correspond to the coma-separated
numbers or number ranges specified by the given string.
|
static void |
rangeSelectorsToIndexes(java.lang.String rangeSelectors,
java.util.Collection<? super java.lang.Integer> collection)
Fills the given collection with integer indexes that correspond to the
coma-separated numbers or number ranges specified by the given string.
|
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 byte[] |
readStreamData(java.io.InputStream inputStream,
int maxBytes)
Read data from input stream into a byte buffer.
|
static java.lang.StringBuilder |
readStringData(java.io.InputStream data,
java.lang.StringBuilder str)
Read data from the input stream into a string builder.
|
static java.lang.StringBuilder |
readStringData(java.io.Reader data,
java.lang.StringBuilder str)
Read data from the string reader into a string builder.
|
static <T> T[] |
removeFromArray(T[] original,
T item)
Removes an item from the specified static array (shrinking its size by one).
|
static void |
resourceToLocalFile(java.lang.String resourcePath,
java.io.File localFile,
boolean overwrite)
Copies a resource data to a given local file.
|
static java.io.File |
resourceToTemporaryFile(java.lang.String resourcePath)
Copies a resource data to a generated temporary file.
|
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 java.lang.String[] |
splitBySpaceWithQuotes(java.lang.String text)
Splits the given string by white space, but preserve the whitespace
enclosed in (single or double) quotes.
|
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 <T> T |
stringToArray(java.lang.CharSequence string,
java.util.regex.Pattern splitPattern,
int splitLimit,
java.lang.Class<? extends T> type,
java.util.Map<java.lang.String,? extends java.lang.Object> namedInstances)
Creates a static array from the given string.
|
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,? extends 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 flagsRegexpGroup,
java.util.Map<java.lang.String,?> variables)
Replace all occurrences of the variable pattern with the value from the hash table.
|
static long |
timeToMilliseconds(java.lang.String time)
Returns the today's specified time in milliseconds.
|
static <E> java.lang.Class<E> |
toGenericClass(java.lang.Object classObject,
java.lang.Class<E> checkClass)
Convert the provided object to Class with generic typing.
|
static java.lang.String |
trimAndUnquote(java.lang.String text)
Removes quotes from the given string (double or single).
|
static java.lang.String |
typeToString(java.lang.Object object)
Returns a string serialization of the given
object . |
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.
|
public static <E> E stringToType(java.lang.String string, java.lang.Class<E> type, java.util.Map<java.lang.String,? extends java.lang.Object> namedInstances) throws java.lang.InstantiationException
Currently supported types are:
String
Class
Iterator
- if the parameter represents the named object that implements Iterator
, then the next value from the iterator is triedstringToType(java.lang.String, java.lang.Class<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
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 mapjava.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 valuejava.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 instancejava.lang.IllegalArgumentException
- if the instance cannot be created (see the message to get the reason)public static java.lang.Object expandReferencedInstances(java.lang.Object instance)
ThreadLocal
and Reference
instances to their referees.
If the given instance is null or a non-expandable data type, it is returned as-is.instance
- the instance to expandpublic static java.lang.String arrayToString(java.lang.Object array, boolean deep) throws java.lang.IllegalArgumentException
array
.array
- the array object to convertdeep
- if true, the typeToString(java.lang.Object)
method is applied to array items, otherwise a simple Object.toString()
is usedjava.lang.IllegalArgumentException
- if the argument is not an arraypublic static <T> T stringToArray(java.lang.CharSequence string, java.util.regex.Pattern splitPattern, int splitLimit, java.lang.Class<? extends T> type, java.util.Map<java.lang.String,? extends java.lang.Object> namedInstances) throws java.lang.InstantiationException
stringToType
method.T
- the class of the array to createstring
- the string to convert to arraysplitPattern
- the pattern used to split the string into array elementssplitLimit
- the number of times the splitPattern is applied and
therefore affects the length of the resulting array (see split
method)type
- the class of the array to create (note that this must be the class of the static array)namedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the mapjava.lang.InstantiationException
- if there was a problem converting the array elementspublic static java.lang.String inetAddressToString(java.net.InetAddress host, int port)
host
- the IP address to convertport
- the port to convert (zero or negative port is ignored)public static java.lang.String typeToString(java.lang.Object object)
object
.
Note that the stringToType(java.lang.String, java.lang.Class<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
methods are able to convert the object back.object
- the object to convertpublic static <T> java.lang.Class<T> wrapPrimitiveType(java.lang.Class<T> type)
T
- a primitive type classtype
- a primitive type classpublic 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 mapjava.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 mapjava.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 convertedpublic 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 boolean isPrimitiveWritableClass(java.lang.Class<?> type, boolean checkArrays)
String
, or a static array with primitive-writable components.
Note that the static arrays are only checked if checkArrays
is true.type
- the class to checkcheckArrays
- flag whether the static arrays should be considered writablepublic static int getPrimitiveTypeSize(java.lang.Class<?> type) throws java.lang.IllegalArgumentException
type
- the primitive type for which to get the sizejava.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 writtenjava.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 readjava.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 generic checkClass
object associated with the class or
interface with the given string namejava.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 generic typed class that is returnedClass
objectjava.lang.ClassCastException
- if passed classObject
is not subclass of checkClass
public static <E> java.lang.Class<E> toGenericClass(java.lang.Object classObject, java.lang.Class<E> checkClass) throws java.lang.ClassCastException
classObject
should be either instance of String
or Class
.
If the generic type check fails, the 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 generic typed class that is returnedClass
objectjava.lang.ClassCastException
- if passed classObject
is not subclass of checkClass
public static java.lang.Object[] parseTypesFromString(java.lang.String[] strings, java.lang.Class<?>[] types, boolean handleVarArgs, int argStartIndex, int argEndIndex, java.util.Map<java.lang.String,? extends 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 tohandleVarArgs
- flag whether to handle the variable number of arguments (true) or not (false)argStartIndex
- index in the strings array which denotes the first changeable argumentargEndIndex
- index in the strings array which denotes the last changeable argumentnamedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the mapjava.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, boolean handleVarArgs, 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 functionality,
use the full parseTypesFromString
method instead.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted tohandleVarArgs
- flag whether to handle the variable number of arguments (true) or not (false)argStartIndex
- index in the strings array which denotes the first changeable argumentargEndIndex
- index in the strings array which denotes the last changeable argumentjava.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, boolean handleVarArgs, 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 functionality,
use the full parseTypesFromString
method instead.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted tohandleVarArgs
- flag whether to handle the variable number of arguments (true) or not (false)argStartIndex
- index in the strings array which denotes the first changeable argumentjava.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, boolean handleVarArgs, int argStartIndex, java.util.Map<java.lang.String,? extends 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 tohandleVarArgs
- flag whether to handle the variable number of arguments (true) or not (false)argStartIndex
- index in the strings array which denotes the first changeable argumentnamedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the mapjava.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, boolean handleVarArgs) throws java.lang.InstantiationException
strings
array will be used.
Note that LocalAbstractObject
parameters will not be converted. For this functionality,
use the full parseTypesFromString
method instead.
strings
- array of strings that hold the valuestypes
- array of classes that the strings should be converted tohandleVarArgs
- flag whether to handle the variable number of arguments (true) or not (false)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, boolean handleVarArgs, java.util.Map<java.lang.String,? extends 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 tohandleVarArgs
- flag whether to handle the variable number of arguments (true) or not (false)namedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the mapjava.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 forpublic static <E> java.lang.reflect.Constructor<E>[] getConstructors(java.lang.Class<? extends E> objectClass, boolean publicOnlyConstructors)
E
- the class for which to get the constructorsobjectClass
- the class for which to get the constructorspublicOnlyConstructors
- flag whether to return all declared constructors (false) or only the public ones (true)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 constructorspublic 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 arraypublic 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 arraypublic static <T,U extends T> T[] copyGenericArray(U[] original, int offset, int length, java.lang.Class<T> componentType) throws java.lang.NegativeArraySizeException
offset
up the the
length of the original or the new array (whichever is smaller). Thus
the zeroth index of the new array will have the value of offset
index
of the original array, and so on. All elements of the new array that does
not have the corresponding value in the original array (i.e. the elements
of the new array with indices i >= original.length - offset
) will
be filled with null.
The resulting array is of the class componentType
.T
- the type of objects in the new arrayU
- the type of objects in the old array (must be assignable to T)original
- the array to be copiedoffset
- the index of the element from the original array from which to start copying the datalength
- the length of the new copied array to be returnedcomponentType
- the class of array components in the new arrayjava.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 addpublic 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 elementpublic 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 removepublic static java.lang.String trimAndUnquote(java.lang.String text)
text
- the text to unquotepublic static java.lang.String[] splitBySpaceWithQuotes(java.lang.String text) throws java.lang.IllegalArgumentException
text
- the string to splitjava.lang.IllegalArgumentException
- if there are unterminated quotes in the stringpublic static java.lang.String iterableToString(java.util.Iterator<?> iterator, java.lang.String separator)
iterator
- the iterator with the valuesseparator
- the separator string inserted in between the valuesiterator
separated by the separator
public static java.lang.String iterableToString(java.lang.Object iterable, java.lang.String separator)
Iterable
.iterable
- the iterable with the valuesseparator
- the separator string inserted in between the valuesiterable
separated by the separator
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 should be cast to other typekeysClass
- the class to cast the map keys tovaluesClass
- the class to cast the map values tojava.lang.ClassCastException
- if there was an incompatible key or value or the object is not Map
public static <K,V> java.util.Map<K,V> copyMapValues(java.util.Map<?,?> sourceMap, java.lang.Iterable<? extends K> keysToRetrieve, java.util.Map<K,V> destinationMap, java.lang.Class<? extends V> valuesClass)
sourceMap
to destinationMap
.
Note that only non-null values that are instances of
valuesClass
are copied.K
- the type of keys in the returned mapV
- the type of values in the returned mapsourceMap
- the source map that provides the valueskeysToRetrieve
- the keys for which the values are copied from the source mapdestinationMap
- the target map into which the values are put
(if null, a new instance of HashMap
is used)valuesClass
- the class to cast the map values topublic static <K,V> java.util.Map<K,V> copyAllMapValues(java.util.Map<? extends K,?> sourceMap, java.util.Map<K,V> destinationMap, java.lang.Class<? extends V> valuesClass)
sourceMap
to destinationMap
.
Note that only non-null values that are instances of
valuesClass
are copied.K
- the type of keys in the returned mapV
- the type of values in the returned mapsourceMap
- the source map that provides the valuesdestinationMap
- the target map into which the values are put
(if null, a new instance of HashMap
is used)valuesClass
- the class to cast the map values topublic 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<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
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 default value to return if there is no parameterjava.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 flagsRegexpGroup, java.util.Map<java.lang.String,?> variables) throws java.lang.IllegalArgumentException
string
- the string to be modifiedvariableRegex
- regular expression that matches variablesvariableRegexGroup
- parenthesis group within regular expression
that holds the variable nameflagsRegexpGroup
- parenthesis group within regular expression
that holds flags:
":defaultValue" the default value for a variable that is not present in the variables
map,
"!" the value is requiredvariables
- the variable names with their valuesjava.lang.IllegalArgumentException
- if there was a required variable that has no valuepublic static long timeToMilliseconds(java.lang.String time) throws java.lang.NumberFormatException
time
- the time in hh:mm:ss.iii
format (seconds and milliseconds are optional)java.lang.NumberFormatException
- if the specified time has invalid formatpublic static long hmsToMilliseconds(java.lang.String hmsStr) throws java.lang.NumberFormatException
Example: 1h 10 m 23s 999
will return 4223999 milliseconds
hmsStr
- the string containing the hours/minutes/seconds time periodjava.lang.NumberFormatException
- if the format of the string is not recognized or the numbers cannot be convertedpublic static void rangeSelectorsToIndexes(java.lang.String rangeSelectors, java.util.Collection<? super java.lang.Integer> collection) throws java.lang.NumberFormatException
rangeSelectors
- the coma-separated numbers or number rangescollection
- the collection to which to add the indexesjava.lang.NumberFormatException
- if the format of the string is not recognized or the numbers cannot be convertedpublic static java.util.Collection<java.lang.Integer> rangeSelectorsToIndexes(java.lang.String rangeSelectors, boolean removeDuplicates) throws java.lang.NumberFormatException
rangeSelectors
- the coma-separated numbers or number rangesremoveDuplicates
- flag if each index is selected only once (true)
or if the returned collection can contain the same indexes multiple timesjava.lang.NumberFormatException
- if the format of the string is not recognized or the numbers cannot be convertedpublic 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 mappublic static byte[] readStreamData(java.io.InputStream inputStream, int maxBytes) throws java.io.IOException
maxBytes
parameter is greater than zero, then no more than
maxBytes
will be read from the input stream. Otherwise, the buffer
will contain all the data from the input stream until the end-of-stream.
Note that the stream is not closed.
inputStream
- the stream from which to read the datamaxBytes
- maximal number of bytes to read from the stream (unlimited if less or equal to zero)java.io.IOException
- if there was a problem reading from the input streampublic static java.lang.StringBuilder readStringData(java.io.Reader data, java.lang.StringBuilder str) throws java.io.IOException
data
is closed
after
the data are read.data
- the reader to retrieve the data fromstr
- the buffer to store the data tostr
or,
if str
was null, a new instance of StringBuilder
)java.io.IOException
- if there was a problem reading the datapublic static java.lang.StringBuilder readStringData(java.io.InputStream data, java.lang.StringBuilder str) throws java.io.IOException
data
is closed
after
the data are read.data
- the stream to retrieve the data fromstr
- the buffer to store the data tostr
or,
if str
was null, a new instance of StringBuilder
)java.io.IOException
- if there was a problem reading the datapublic static void resourceToLocalFile(java.lang.String resourcePath, java.io.File localFile, boolean overwrite) throws java.io.FileNotFoundException, java.io.IOException
resourcePath
- the absolute path of the resourcelocalFile
- the local file where the resource will be copiedoverwrite
- flag whether to silently overwrite the local file if it already existsjava.io.FileNotFoundException
- if the resource was not found, the destination local file cannot be created,
or (when overwrite flag is true) the local file already existsjava.io.IOException
- if there was a problem writing the local fileClass.getResource(java.lang.String)
public static java.io.File resourceToTemporaryFile(java.lang.String resourcePath) throws java.io.IOException
resourcePath
- the absolute path of the resourcejava.io.FileNotFoundException
- if the resource was not found, the destination local file cannot be created,
or (when overwrite flag is true) the local file already existsjava.io.IOException
- if there was a problem writing the local fileClass.getResource(java.lang.String)