DATACOUCH-61 - Add Converter support for Jodatime
This changeset adds converters for Jodatime to unix timestamps and back if jodatime is found on the classpath.
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -87,6 +87,13 @@
|
||||
<version>${jackson}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${jodatime}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
|
||||
@@ -16,9 +16,14 @@
|
||||
|
||||
package org.springframework.data.couchbase.core.convert;
|
||||
|
||||
import org.joda.time.DateMidnight;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.convert.ReadingConverter;
|
||||
import org.springframework.data.convert.WritingConverter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
@@ -35,13 +40,32 @@ public final class DateConverters {
|
||||
|
||||
private DateConverters() {}
|
||||
|
||||
private static final boolean JODA_TIME_IS_PRESENT = ClassUtils.isPresent("org.joda.time.LocalDate", null);
|
||||
|
||||
/**
|
||||
* Returns all converters by this class that can be registered.
|
||||
*
|
||||
* @return the list of converters to register.
|
||||
*/
|
||||
public static Collection<Converter<?, ?>> getConvertersToRegister() {
|
||||
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
|
||||
|
||||
converters.add(DateToLongConverter.INSTANCE);
|
||||
converters.add(CalendarToLongConverter.INSTANCE);
|
||||
|
||||
converters.add(LongToDateConverter.INSTANCE);
|
||||
converters.add(LongToCalendarConverter.INSTANCE);
|
||||
|
||||
if (JODA_TIME_IS_PRESENT) {
|
||||
converters.add(LocalDateToLongConverter.INSTANCE);
|
||||
converters.add(LocalDateTimeToLongConverter.INSTANCE);
|
||||
converters.add(DateTimeToLongConverter.INSTANCE);
|
||||
converters.add(DateMidnightToLongConverter.INSTANCE);
|
||||
converters.add(LongToLocalDateConverter.INSTANCE);
|
||||
converters.add(LongToLocalDateTimeConverter.INSTANCE);
|
||||
converters.add(LongToDateTimeConverter.INSTANCE);
|
||||
converters.add(LongToDateMidnightConverter.INSTANCE);
|
||||
}
|
||||
|
||||
return converters;
|
||||
}
|
||||
|
||||
@@ -51,7 +75,7 @@ public final class DateConverters {
|
||||
|
||||
@Override
|
||||
public Long convert(Date source) {
|
||||
return source.getTime();
|
||||
return source == null ? null : source.getTime();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +85,7 @@ public final class DateConverters {
|
||||
|
||||
@Override
|
||||
public Long convert(Calendar source) {
|
||||
return source.getTimeInMillis() / 1000;
|
||||
return source == null ? null : source.getTimeInMillis() / 1000;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +95,10 @@ public final class DateConverters {
|
||||
|
||||
@Override
|
||||
public Date convert(Long source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Date date = new Date();
|
||||
date.setTime(source);
|
||||
return date;
|
||||
@@ -83,10 +111,94 @@ public final class DateConverters {
|
||||
|
||||
@Override
|
||||
public Calendar convert(Long source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(source * 1000);
|
||||
return calendar;
|
||||
}
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
public enum LocalDateToLongConverter implements Converter<LocalDate, Long> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Long convert(LocalDate source) {
|
||||
return source == null ? null : source.toDate().getTime();
|
||||
}
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
public enum LocalDateTimeToLongConverter implements Converter<LocalDateTime, Long> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Long convert(LocalDateTime source) {
|
||||
return source == null ? null : source.toDate().getTime();
|
||||
}
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
public enum DateTimeToLongConverter implements Converter<DateTime, Long> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Long convert(DateTime source) {
|
||||
return source == null ? null : source.toDate().getTime();
|
||||
}
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
public enum DateMidnightToLongConverter implements Converter<DateMidnight, Long> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Long convert(DateMidnight source) {
|
||||
return source == null ? null : source.toDate().getTime();
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum LongToLocalDateConverter implements Converter<Long, LocalDate> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public LocalDate convert(Long source) {
|
||||
return source == null ? null : new LocalDate(source);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum LongToLocalDateTimeConverter implements Converter<Long, LocalDateTime> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public LocalDateTime convert(Long source) {
|
||||
return source == null ? null : new LocalDateTime(source);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum LongToDateTimeConverter implements Converter<Long, DateTime> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public DateTime convert(Long source) {
|
||||
return source == null ? null : new DateTime(source);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum LongToDateMidnightConverter implements Converter<Long, DateMidnight> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public DateMidnight convert(Long source) {
|
||||
return source == null ? null : new DateMidnight(source);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.couchbase.core.mapping;
|
||||
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -387,16 +388,19 @@ public class MappingCouchbaseConverterTests {
|
||||
public void writesAndReadsDates() {
|
||||
Date created = new Date();
|
||||
Calendar modified = Calendar.getInstance();
|
||||
DateEntity entity = new DateEntity(created, modified);
|
||||
LocalDateTime deleted = LocalDateTime.now();
|
||||
DateEntity entity = new DateEntity(created, modified, deleted);
|
||||
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
converter.write(entity, converted);
|
||||
assertEquals(created.getTime(), converted.getPayload().get("created"));
|
||||
assertEquals(modified.getTimeInMillis() / 1000, converted.getPayload().get("modified"));
|
||||
assertEquals(deleted.toDate().getTime(), converted.getPayload().get("deleted"));
|
||||
|
||||
DateEntity read = converter.read(DateEntity.class, converted);
|
||||
assertEquals(created.getTime(), read.created.getTime());
|
||||
assertEquals(modified.getTimeInMillis() / 1000, read.modified.getTimeInMillis() / 1000);
|
||||
assertEquals(deleted.toDate().getTime(), read.deleted.toDate().getTime());
|
||||
}
|
||||
|
||||
static class EntityWithoutID {
|
||||
@@ -507,9 +511,12 @@ public class MappingCouchbaseConverterTests {
|
||||
static class DateEntity extends BaseEntity {
|
||||
private Date created;
|
||||
private Calendar modified;
|
||||
public DateEntity(Date created, Calendar modified) {
|
||||
private LocalDateTime deleted;
|
||||
|
||||
public DateEntity(Date created, Calendar modified, LocalDateTime deleted) {
|
||||
this.created = created;
|
||||
this.modified = modified;
|
||||
this.deleted = deleted;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user