TypeHelper¶
-
public final class
TypeHelper
¶ A helper class for parsing and formatting MDS supported types.
Methods¶
asCollection¶
-
public static <T> Collection<T>
asCollection
(Object value)¶ Returns the provided value as a collection. If the value is a collection, it is simply returned. If the value is a non-collection and non null object, it is returned as an arrayList containing one object. IF the object is null, then an empty arraylist is returned.
Parameters: - value – the value to parse
- <T> – the type of the collection
Returns: the value as a collection
breakString¶
format¶
-
public static String
format
(Object obj)¶ Creates a
java.lang.String
representation of the given value. If given object is ajava.util.List
, each new element is placed in a new line.Parameters: - obj – value to retrieve
java.lang.String
representation for
Returns: java.lang.String
representation of an object- obj – value to retrieve
format¶
-
public static String
format
(Object obj, char collJoinChar)¶ Creates a
java.lang.String
representation of the given value. If given object is ajava.util.List
a character put between next values can be specified.Parameters: - obj – value to retrieve
java.lang.String
representation for - collJoinChar – character to put between next elements of a collection; applicable if given object is a collection
Returns: java.lang.String
representation of an object- obj – value to retrieve
getPrimitive¶
getWrapperForPrimitive¶
hasPrimitive¶
isPrimitive¶
isPrimitive¶
isSubclassOfMdsVersionedEntity¶
isTypeSupportedInMap¶
-
public static boolean
isTypeSupportedInMap
(String type, boolean isKey)¶ Returns true if the given type is supported in map, otherwise false.
Parameters: - type – the type to be checked
- isKey – true if it is key of the map, otherwise false
Returns: true if the given type is supported in map, otherwise false
parse¶
-
public static Object
parse
(Object val, Class<?> toClass)¶ Attempts to parse given value to an instance of a given class. Throws
java.lang.IllegalArgumentException
if this method is unable to parse the value.Parameters: - val – value to parse
- toClass – a class to turn value into
Returns: parsed value, and instance of the given class
parse¶
-
public static Object
parse
(Object val, String toClass)¶ Attempts to parse given value to an instance of a given class. Throws
java.lang.IllegalArgumentException
if this method is unable to parse the value.Parameters: - val – value to parse
- toClass – fully qualified class name
Returns: parsed value, and instance of the given class
parse¶
-
public static Object
parse
(Object val, String toClass, ClassLoader classLoader)¶ Attempts to parse given value to an instance of a given class. The class may be loaded using custom class loader, in case of a failure to load it via default class loader. Throws
java.lang.IllegalArgumentException
if this method is unable to parse the value.Parameters: - val – value to parse
- toClass – fully qualified class name
- classLoader – class loader to use, in case of a failure to find class of name
toClass
Returns: parsed value, and instance of the given class
parse¶
-
public static Object
parse
(Object val, String toClass, String genericType)¶ Attempts to parse given value to an instance of a given class. The class may have a generic type. Throws
java.lang.IllegalArgumentException
if this method is unable to parse the value.Parameters: - val – value to parse
- toClass – fully qualified class name
- genericType – fully qualified class name of a generic type
Returns: parsed value, and instance of the given class
parse¶
-
public static Object
parse
(Object val, String toClass, String genericType, ClassLoader classLoader)¶ Attempts to parse given value to an instance of a given class. The class may have a generic type and can be loaded using custom class loader, in case of a failure to load it via default class loader. Throws
java.lang.IllegalArgumentException
if this method is unable to parse the value.Parameters: - val – value to parse
- toClass – fully qualified class name
- genericType – fully qualified class name of a generic type
- classLoader – class loader to use, in case of a failure to find class of name
toClass
Returns: parsed value, and instance of the given class
parseCollection¶
-
public static Collection
parseCollection
(Collection val, Class<?> toClassDefinition, Class<?> generic)¶
parseDateToDate¶
-
public static Object
parseDateToDate
(Object val, String toClass)¶ Allows parsing of the various date types. Parsing from the
org.joda.time.LocalDate
andorg.motechproject.commons.date.model.Time
is not supported at the moment.Parameters: - val – value to parse
- toClass – destination class
Returns: date, parsed to the specified type
parseIntToBool¶
-
public static boolean
parseIntToBool
(Integer val)¶ Turns given
java.lang.Integer
intoboolean
.Parameters: - val – value to parse
Returns: true if value is not null and greater than 0; false otherwise
parseNumber¶
-
public static Number
parseNumber
(Object val, String toClass)¶ Parses given value into
java.lang.Number
or one of the standard Java types, extending Number.Parameters: - val – value to parse
- toClass – fully qualified class name
Returns: parsed value
parseString¶
-
public static Object
parseString
(String str, Class<?> toClass)¶ Attempts to parse given String to an instance of a given class. It will throw
java.lang.IllegalStateException
in case this method was not able to parse the value.Parameters: - str – String to parse
- toClass – a class to turn value into
Returns: parsed value, an instance of the given class
parseString¶
-
public static Object
parseString
(String str, String toClass)¶ Attempts to parse given String to an instance of a given class. It will throw
java.lang.IllegalStateException
in case this method was not able to parse the value.Parameters: - str – String to parse
- toClass – fully qualified class name
Returns: parsed value, an instance of the given class
parseString¶
-
public static Object
parseString
(String str, Class<?> toClass, Class<?> generic)¶ Attempts to parse given String to an instance of a given class. The class may also have a generic type. It will throw
java.lang.IllegalStateException
in case this method was not able to parse the value.Parameters: - str – String to parse
- toClass – a class to turn value into
- generic – generic class
Returns: parsed value, an instance of the given class
parseStringToMap¶
-
public static Map
parseStringToMap
(String str)¶ Parses given
java.lang.String
tojava.util.Map
. Each new entry should be preceeded by a comma mark (,). The key and value should be split with a colon mark (:). By default parsed values will be String type.Parameters: - str – String to parse
Returns: Map, parsed from the given String
parseStringToMap¶
-
public static <K, V> Map<K, V>
parseStringToMap
(Class<K> keyClass, Class<V> valueClass, String str)¶ Parses given
java.lang.String
tojava.util.Map
. Each new entry should be preceeded by a comma mark (,). The key and value should be split with a colon mark (:). Types of the parsed values depend on the given keyClass and valueClass.Parameters: - keyClass – the type of key
- valueClass – the type of value
- str – String String to parse
Returns: Map, parsed from the given String
parseStringToMap¶
-
public static Map
parseStringToMap
(String keyClass, String valueClass, String str)¶ Parses given
java.lang.String
tojava.util.Map
. Each new entry should be preceeded by a comma mark (,). The key and value should be split with a colon mark (:). Types of the parsed values depend on the given keyClass and valueClass.Parameters: - keyClass – the type of key
- valueClass – the type of value
- str – String String to parse
Returns: Map, parsed from the given String
suggestAndCreateCollectionImplementation¶
-
public static Collection
suggestAndCreateCollectionImplementation
(Class<? extends Collection> collectionClass)¶
suggestCollectionImplementation¶
-
public static Class
suggestCollectionImplementation
(String collectionClass)¶ Returns concrete class, for the given collection interface or abstract class. If given class is already concrete, it will return that class. Throws
java.lang.IllegalArgumentException
if class of given name cannot be loaded.Parameters: - collectionClass – fully qualified class name to find implementation for
Returns: concrete class
suggestCollectionImplementation¶
-
public static Class<? extends Collection>
suggestCollectionImplementation
(Class<? extends Collection> collectionClass)¶ Returns concrete class, for the given collection interface or abstract class. If given class is already concrete, it will return that class.
Parameters: - collectionClass – collection class to find implementation for
Returns: concrete class
toRange¶
-
public static Range
toRange
(Object object, String typeClass)¶ Parses given value to
org.motechproject.commons.api.Range
. If passed value is assignable neither to range nor to map, it throwsjava.lang.IllegalArgumentException
. If value is a map, it should contain keys “min” and “max”.Parameters: - object – value to parse
- typeClass – fully qualified class name of the range values
Returns:
toSet¶
-
public static Set
toSet
(Object object, String typeClass, ClassLoader classLoader)¶ Parses given
java.util.Collection
class intojava.util.Set
. If given value is not a subtype ofjava.util.Collection
it throwsjava.lang.IllegalArgumentException
.Parameters: - object – value to parse
- typeClass – type of the values that should be placed in
java.util.Set
- classLoader – optional class loader to use, loading type class
Returns: Set, parsed from the given value