Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public <T> T transform(Object value, Class<T> type) {
return type.cast(returnValue);
}

public boolean accepts(Object value, Class<?> type) {
boolean instanceOfCalendar = value instanceof Calendar;
return JavaVersion.current().gte(JavaVersion.JAVA_8) ? instanceOfCalendar && !java.time.temporal.Temporal.class.isAssignableFrom(type) : instanceOfCalendar;
}
@SuppressWarnings("unchecked")
public boolean accepts(Object value, Class<?> type) {
boolean instanceOfCalendar = value instanceof Calendar;

try {
return JavaVersion.current().gte(JavaVersion.JAVA_8) ? instanceOfCalendar && !ClassUtils.getClass("java.time.temporal.Temporal").isAssignableFrom(type) : instanceOfCalendar;
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e.getMessage(),e);
}
}
}
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);
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

}

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;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
@@ -1,9 +1,13 @@
package br.com.six2six.fixturefactory.util;

import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.reflect.MethodUtils;

public class DateTimeUtils {

public static Calendar toCalendar(String source, DateFormat format) {
Expand All @@ -16,8 +20,12 @@ public static Calendar toCalendar(String source, DateFormat format) {
return date;
}

public static java.time.ZonedDateTime toZonedDateTime(Calendar value) {
return value.toInstant().atZone(value.getTimeZone().toZoneId());
public static Object toZonedDateTime(Calendar value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Object timeZone = MethodUtils.invokeMethod(value, "getTimeZone", ArrayUtils.EMPTY_OBJECT_ARRAY);
Object zoneId = MethodUtils.invokeMethod(timeZone, "toZoneId", ArrayUtils.EMPTY_OBJECT_ARRAY);
Object instant = MethodUtils.invokeMethod(value, "toInstant", ArrayUtils.EMPTY_OBJECT_ARRAY);
Object zone = MethodUtils.invokeMethod(instant, "atZone", new Object[]{zoneId});
return zone;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ public void setSendDate(LocalDate sendDate) {
this.sendDate = sendDate;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you stop to use simple imports?

Thank you!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


}
}