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

public static String[] breakString(String str)

breakString

public static String[] breakString(String str, String[] removes, String[] search, String[] replacement, String separator)

breakStringForCollection

public static String[] breakStringForCollection(String str)

buildStringFromList

public static String buildStringFromList(List<String> items)

format

public static String format(Object obj)

Creates a java.lang.String representation of the given value. If given object is a java.util.List, each new element is placed in a new line.

Parameters:
Returns:

java.lang.String representation of an object

format

public static String format(Object obj, char collJoinChar)

Creates a java.lang.String representation of the given value. If given object is a java.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

getClassNameForMdsType

public static String getClassNameForMdsType(Class<?> clazz)

getPrimitive

public static Class<?> getPrimitive(Class<?> clazz)

Retrieves equivalent primitive class for the given wrapper class.

Parameters:
  • clazz – wrapper class
Returns:

equivalent primitive class

getWrapperForPrimitive

public static Class<?> getWrapperForPrimitive(Class<?> clazz)

Retrieves equivalent wrapper class for the given primitive type.

Parameters:
  • clazz – primitive type
Returns:

equivalent wrapper class

hasPrimitive

public static boolean hasPrimitive(Class<?> clazz)

Verifies whether given class has got a primitive equivalent.

Parameters:
  • clazz – class to verify
Returns:

true if given wrapper class has got a primitive equivalent; false otherwise

isBaseEntity

public static boolean isBaseEntity(String entitySuperClass)

isPrimitive

public static boolean isPrimitive(Class<?> clazz)

Verifies whether given class is primitive.

Parameters:
  • clazz – class to verify
Returns:

true if given class is primitive; false otherwise

isPrimitive

public static boolean isPrimitive(String className)

Verifies whether given class is primitive.

Parameters:
  • className – fully qualified class name to verify
Returns:

true if given class is primitive; false otherwise

isSubclassOfMdsEntity

public static boolean isSubclassOfMdsEntity(String entitySuperClass)

isSubclassOfMdsVersionedEntity

public static boolean isSubclassOfMdsVersionedEntity(String entitySuperClass)

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 and org.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 into boolean.

Parameters:
  • val – value to parse
Returns:

true if value is not null and greater than 0; false otherwise

parseMapValue

public static Object parseMapValue(Object valueToParse, String type, boolean isKey)

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 to java.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 to java.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 to java.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

parseStringToUUID

public static Object parseStringToUUID(String val)

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 throws java.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:

org.motechproject.commons.api.Range value

toSet

public static Set toSet(Object object, String typeClass, ClassLoader classLoader)

Parses given java.util.Collection class into java.util.Set. If given value is not a subtype of java.util.Collection it throws java.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