-
Notifications
You must be signed in to change notification settings - Fork 86
Add retrocompatibility to java 6 and 7 #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7fdbd9a
8711c91
e09e396
c9522cb
1080afa
0deaedf
8af46e0
2d82423
c07a8f8
b825175
68a8798
deec695
a664dc7
f855335
92e2d79
d7f6008
bb2d3e3
b4549a3
98722b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,74 @@ | ||
| package br.com.six2six.fixturefactory.transformer; | ||
|
|
||
| import static br.com.six2six.fixturefactory.util.DateTimeUtils.toZonedDateTime; | ||
| import static org.apache.commons.lang.ArrayUtils.EMPTY_OBJECT_ARRAY; | ||
| import static org.apache.commons.lang.reflect.MethodUtils.invokeMethod; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.util.Calendar; | ||
|
|
||
| import org.apache.commons.lang.ClassUtils; | ||
|
|
||
| import br.com.six2six.fixturefactory.JavaVersion; | ||
| import br.com.six2six.fixturefactory.util.DateTimeUtils; | ||
|
|
||
| public class DateTimeTransformer implements Transformer { | ||
|
|
||
| @Override | ||
| public <T> T transform(Object value, Class<T> type) { | ||
| Object returnValue = null; | ||
|
|
||
| if (value == null) { | ||
| if (value == null || !JavaVersion.current().gte(JavaVersion.JAVA_8)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (ClassUtils.isAssignable(type, java.time.LocalDateTime.class)) { | ||
| returnValue = toZonedDateTime((Calendar) value).toLocalDateTime(); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, java.time.LocalDate.class)) { | ||
| returnValue = toZonedDateTime((Calendar) value).toLocalDate(); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, java.time.LocalTime.class)) { | ||
| returnValue = toZonedDateTime((Calendar) value).toLocalTime(); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, java.time.OffsetDateTime.class)) { | ||
| returnValue = toZonedDateTime((Calendar) value).toOffsetDateTime(); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, java.time.ZonedDateTime.class)) { | ||
| returnValue = toZonedDateTime((Calendar) value); | ||
| try { | ||
| Object zonedDateTime = DateTimeUtils.toZonedDateTime((Calendar) value); | ||
| if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.LocalDateTime"))) { | ||
| returnValue = invokeMethod(zonedDateTime, "toLocalDateTime", EMPTY_OBJECT_ARRAY); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.LocalDate"))) { | ||
| returnValue = invokeMethod(zonedDateTime, "toLocalDate", EMPTY_OBJECT_ARRAY); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.LocalTime"))) { | ||
| returnValue = invokeMethod(zonedDateTime, "toLocalTime", EMPTY_OBJECT_ARRAY); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.OffsetDateTime"))) { | ||
| returnValue = invokeMethod(zonedDateTime, "toOffsetDateTime", EMPTY_OBJECT_ARRAY); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.ZonedDateTime"))) { | ||
| returnValue = zonedDateTime; | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.OffsetTime"))) { | ||
| returnValue = invokeMethod(zonedDateTime, "toOffsetDateTime", EMPTY_OBJECT_ARRAY); | ||
| returnValue = invokeMethod(returnValue, "toOffsetTime", EMPTY_OBJECT_ARRAY); | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, ClassUtils.getClass("java.time.Instant"))) { | ||
| returnValue = invokeMethod(zonedDateTime, "toInstant", EMPTY_OBJECT_ARRAY); | ||
|
|
||
| } else { | ||
| throw new IllegalArgumentException("Incorrect type for transformer: " + type.getCanonicalName()); | ||
| } | ||
|
|
||
| } else if (ClassUtils.isAssignable(type, java.time.OffsetTime.class)) { | ||
| returnValue = toZonedDateTime((Calendar) value).toOffsetDateTime().toOffsetTime(); | ||
| } else if (ClassUtils.isAssignable(type, java.time.Instant.class)) { | ||
| returnValue = ((Calendar) value).toInstant(); | ||
|
|
||
| } else { | ||
| throw new IllegalArgumentException("Incorrect type for transformer: " + type.getCanonicalName()); | ||
| } catch (NoSuchMethodException e) { | ||
| throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); | ||
| } catch (IllegalAccessException e) { | ||
| throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); | ||
| } catch (InvocationTargetException e) { | ||
| throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); | ||
| } catch (ClassNotFoundException e) { | ||
| throw new IllegalArgumentException("Problem to convert to type: " + type.getCanonicalName(), e); | ||
| } | ||
|
|
||
| return type.cast(returnValue); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public boolean accepts(Object value, Class<?> type) { | ||
| return value instanceof Calendar && java.time.temporal.Temporal.class.isAssignableFrom(type); | ||
| try { | ||
| return value instanceof Calendar && ClassUtils.getClass("java.time.temporal.Temporal").isAssignableFrom(type); | ||
| } catch (ClassNotFoundException e) { | ||
| return false; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mundodojava please fix the indentation here. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,4 +59,4 @@ public void setSendDate(LocalDate sendDate) { | |
| this.sendDate = sendDate; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you stop to use simple imports? Thank you!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once all exceptions will trigger the same behavior, please, try to use a multiple catch approach.
You can check how to do it here here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will work with the java compiler 6?
Because I knew how to do but did not because of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's true! It's a java 7 feature:+1:
Let's make sure it will compile using any previous version.