TypeHelper { public static Range toRange(Object object, String typeClass) { if (object instanceof Range) { return (Range) object; } else if (object instanceof Map) { Map map = (Map) object; Object min = parse(map.get("min"), typeClass); Object max = parse(map.get("max"), typeClass); return new Range(min, max); } else if (object instanceof String) { String objectAsStr = (String) object; String minStr = null; String maxStr = null; if (objectAsStr.endsWith("..")) { minStr = objectAsStr.substring(0, objectAsStr.length() - 2); } else if (objectAsStr.startsWith("..")) { maxStr = objectAsStr.substring(2, objectAsStr.length()); } else { String[] split = objectAsStr.split("\\.\\."); if (split.length != 2) { throw unableToParseException(object, Range.class); } minStr = split[0]; maxStr = split[1]; } Object min = parse(minStr, typeClass); Object max = parse(maxStr, typeClass); return new Range(min, max); } else { throw unableToParseException(object, Range.class); } } private  TypeHelper(); static Object parse(Object val, Class<?> toClass); static Object parse(Object val, String toClass); static Object parse(Object val, String toClass, ClassLoader classLoader); static Object parse(Object val, String toClass, String genericType); static Object parse(Object val, String toClass, String genericType, ClassLoader classLoader); static Object parseString(String str, Class<?> toClass); static Object parseString(String str, String toClass); static Object parseString(String str, Class<?> toClass, Class<?> generic); static String buildStringFromList(List<String> items); static String[] breakString(String str); static String[] breakStringForCollection(String str); static String[] breakString(String str, String[] removes, String[] search,
                                       String[] replacement, String separator); static Map parseStringToMap(String str); static Map<K, V> parseStringToMap(Class<K> keyClass, Class<V> valueClass, String str); static Map parseStringToMap(String keyClass, String valueClass, String str); static Object parseStringToUUID(String val); static Object parseMapValue(Object valueToParse, String type, boolean isKey); static boolean isTypeSupportedInMap(String type, boolean isKey); static Object parseDateToDate(Object val, String toClass); static Collection parseCollection(Collection val, Class<?> toClassDefinition, Class<?> generic); static boolean parseIntToBool(Integer val); static String format(Object obj); static String format(Object obj, char collJoinChar); static Number parseNumber(Object val, String toClass); static boolean hasPrimitive(Class<?> clazz); static boolean isPrimitive(Class<?> clazz); static boolean isPrimitive(String className); static Class<?> getPrimitive(Class<?> clazz); static Class<?> getWrapperForPrimitive(Class<?> clazz); static String getClassNameForMdsType(Class<?> clazz); static Range toRange(Object object, String typeClass); static Set toSet(Object object, String typeClass, ClassLoader classLoader); static Class suggestCollectionImplementation(String collectionClass); static Class<? extends Collection> suggestCollectionImplementation(Class<? extends Collection> collectionClass); static Collection suggestAndCreateCollectionImplementation(Class<? extends Collection> collectionClass); static Collection<T> asCollection(Object value); static boolean isBaseEntity(String entitySuperClass); static boolean isSubclassOfMdsEntity(String entitySuperClass); static boolean isSubclassOfMdsVersionedEntity(String entitySuperClass);  }
@Test public void shouldBuildRanges() { final DateTime now = DateUtil.now(); final Range dtRange = new Range<>(now, now.plusHours(1)); final Range intRange = new Range<>(1, 5); assertEquals(intRange, TypeHelper.toRange(new Range<>(1, 5), Integer.class.getName())); assertEquals(dtRange, TypeHelper.toRange(new Range<>(now, now.plusHours(1)), DateTime.class.getName())); Map<String, Object> map = new LinkedHashMap<>(); map.put("min", now); map.put("max", now.plusHours(1)); assertEquals(dtRange, TypeHelper.toRange(map, DateTime.class.getName())); map.put("min", now.toString()); map.put("max", now.plusHours(1).toString()); assertEquals(dtRange, TypeHelper.toRange(map, DateTime.class.getName())); assertEquals(dtRange, TypeHelper.toRange(dtRange.getMin() + ".." + dtRange.getMax(), DateTime.class.getName())); assertEquals(intRange, TypeHelper.toRange("1..5", Integer.class.getName())); }