TypeHelper { public static Object parse(Object val, Class<?> toClass) { return parse(val, toClass.getName()); } 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 shouldParseStrings() { final DateTime dt = DateUtil.now(); final DateTime dt2 = new DateTime(2009, 6, 7, 12, 11, 0, 0, DateTimeZone.forOffsetHours(1)); final LocalDate ld = DateUtil.now().toLocalDate(); final LocalDate ld2 = new LocalDate(2000, 8, 22); final LocalDateTime ldt = LocalDateTime.now(); final LocalDateTime ldt2 = LocalDateTime.of(2015, 9, 1, 10, 15); final java.time.LocalDate jld = java.time.LocalDate.now(); final java.time.LocalDate jld2 = java.time.LocalDate.of(1991, 5, 1); assertEquals(3, TypeHelper.parse(3, Integer.class)); assertEquals("test", TypeHelper.parse("test", String.class)); assertEquals(new Time(16, 4), TypeHelper.parse("16:04", Time.class)); assertEquals(dt, TypeHelper.parse(dt.toString(), DateTime.class)); assertEquals(dt.toDate(), TypeHelper.parse(dt.toString(), Date.class)); assertEquals(DateUtil.setTimeZoneUTC(dt2), DateUtil.setTimeZoneUTC((DateTime) TypeHelper.parse("2009-06-07 12:11 +01:00", DateTime.class))); assertEquals(dt2.toDate(), TypeHelper.parse("2009-06-07 12:11 +01:00", Date.class)); assertEquals(asList("one", "2", "three"), TypeHelper.parse("one\n2\nthree", List.class)); assertEquals(true, TypeHelper.parse("true", Boolean.class)); assertEquals(ld, TypeHelper.parse(ld, LocalDate.class)); assertEquals(ld, TypeHelper.parse(ld.toString(), LocalDate.class)); assertEquals(ld2, TypeHelper.parse("2000-08-22", LocalDate.class)); assertEquals(ld2, TypeHelper.parse("2000-08-22T23:00:00.000Z", LocalDate.class)); assertEquals(jld, TypeHelper.parse(jld, java.time.LocalDate.class)); assertEquals(jld, TypeHelper.parse(jld.toString(), java.time.LocalDate.class)); assertEquals(jld2, TypeHelper.parse("1991-05-01", java.time.LocalDate.class)); assertEquals(ldt, TypeHelper.parse(ldt, java.time.LocalDateTime.class)); assertEquals(ldt, TypeHelper.parse(ldt.toString(), java.time.LocalDateTime.class)); assertTrue(ldt2.equals(TypeHelper.parse("2015-09-01 10:15 ".concat(findServerOffset()), java.time.LocalDateTime.class))); }
@Test public void shouldReturnCorrectInstances() { final DateTime dt = DateUtil.now(); assertEquals(new Time(10, 10), TypeHelper.parse(new Time(10, 10), Time.class)); assertEquals(dt, TypeHelper.parse(dt, DateTime.class)); assertEquals(dt.toDate(), TypeHelper.parse(dt.toDate(), Date.class)); assertEquals(11, TypeHelper.parse(11, Integer.class)); assertEquals(asList(1, 2), TypeHelper.parse(asList(1, 2), List.class)); }
@Test public void shouldParseIntToBool() { assertEquals(true, TypeHelper.parse(1, Boolean.class)); assertEquals(true, TypeHelper.parse(200, Boolean.class)); assertEquals(false, TypeHelper.parse(0, Boolean.class)); assertEquals(false, TypeHelper.parse(-1, Boolean.class)); assertEquals(null, TypeHelper.parse(null, Boolean.class)); }
@Test public void shouldParseDateToDate() { Date date = new Date(); DateTime dateTime = new DateTime(date); LocalDate localDate = dateTime.toLocalDate(); Time time = new Time(dateTime.getHourOfDay(), dateTime.getMinuteOfHour()); assertEquals(dateTime, TypeHelper.parse(dateTime, DateTime.class)); assertEquals(date, TypeHelper.parse(dateTime, Date.class)); assertEquals(localDate, TypeHelper.parse(dateTime, LocalDate.class)); assertEquals(time, TypeHelper.parse(dateTime, Time.class)); assertEquals(dateTime, TypeHelper.parse(date, DateTime.class)); assertEquals(date, TypeHelper.parse(date, Date.class)); assertEquals(localDate, TypeHelper.parse(date, LocalDate.class)); assertEquals(time, TypeHelper.parse(date, Time.class)); }