Browse Source
We now include Spring Converters for the ThreeTen backport project [0]. [0] http://www.threeten.org/threetenbppull/110/head
4 changed files with 293 additions and 0 deletions
@ -0,0 +1,152 @@
@@ -0,0 +1,152 @@
|
||||
/* |
||||
* Copyright 2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.convert; |
||||
|
||||
import static org.threeten.bp.DateTimeUtils.*; |
||||
import static org.threeten.bp.Instant.*; |
||||
import static org.threeten.bp.LocalDateTime.*; |
||||
import static org.threeten.bp.ZoneId.*; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
import org.springframework.core.convert.converter.Converter; |
||||
import org.springframework.util.ClassUtils; |
||||
import org.threeten.bp.Instant; |
||||
import org.threeten.bp.LocalDate; |
||||
import org.threeten.bp.LocalDateTime; |
||||
import org.threeten.bp.LocalTime; |
||||
|
||||
/** |
||||
* Helper class to register {@link Converter} implementations for the ThreeTen Backport project in case it's present on |
||||
* the classpath. |
||||
* |
||||
* @author Oliver Gierke |
||||
* @see http://www.threeten.org/threetenbp
|
||||
*/ |
||||
public abstract class Jsr310BackPortConverters { |
||||
|
||||
private static final boolean THREE_TEN_BACK_PORT_IS_PRESENT = ClassUtils.isPresent("org.threeten.bp.LocalDateTime", |
||||
Jsr310BackPortConverters.class.getClassLoader()); |
||||
|
||||
/** |
||||
* Returns the converters to be registered. Will only return converters in case we're running on Java 8. |
||||
* |
||||
* @return |
||||
*/ |
||||
public static Collection<Converter<?, ?>> getConvertersToRegister() { |
||||
|
||||
if (!THREE_TEN_BACK_PORT_IS_PRESENT) { |
||||
return Collections.emptySet(); |
||||
} |
||||
|
||||
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>(); |
||||
converters.add(DateToLocalDateTimeConverter.INSTANCE); |
||||
converters.add(LocalDateTimeToDateConverter.INSTANCE); |
||||
converters.add(DateToLocalDateConverter.INSTANCE); |
||||
converters.add(LocalDateToDateConverter.INSTANCE); |
||||
converters.add(DateToLocalTimeConverter.INSTANCE); |
||||
converters.add(LocalTimeToDateConverter.INSTANCE); |
||||
converters.add(DateToInstantConverter.INSTANCE); |
||||
converters.add(InstantToDateConverter.INSTANCE); |
||||
|
||||
return converters; |
||||
} |
||||
|
||||
public static enum DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public LocalDateTime convert(Date source) { |
||||
|
||||
return source == null ? null : ofInstant(toInstant(source), systemDefault()); |
||||
} |
||||
} |
||||
|
||||
public static enum LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public Date convert(LocalDateTime source) { |
||||
return source == null ? null : toDate(source.atZone(systemDefault()).toInstant()); |
||||
} |
||||
} |
||||
|
||||
public static enum DateToLocalDateConverter implements Converter<Date, LocalDate> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public LocalDate convert(Date source) { |
||||
return source == null ? null : ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalDate(); |
||||
} |
||||
} |
||||
|
||||
public static enum LocalDateToDateConverter implements Converter<LocalDate, Date> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public Date convert(LocalDate source) { |
||||
return source == null ? null : toDate(source.atStartOfDay(systemDefault()).toInstant()); |
||||
} |
||||
} |
||||
|
||||
public static enum DateToLocalTimeConverter implements Converter<Date, LocalTime> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public LocalTime convert(Date source) { |
||||
return source == null ? null : ofInstant(ofEpochMilli(source.getTime()), systemDefault()).toLocalTime(); |
||||
} |
||||
} |
||||
|
||||
public static enum LocalTimeToDateConverter implements Converter<LocalTime, Date> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public Date convert(LocalTime source) { |
||||
return source == null ? null : toDate(source.atDate(LocalDate.now()).atZone(systemDefault()).toInstant()); |
||||
} |
||||
} |
||||
|
||||
public static enum DateToInstantConverter implements Converter<Date, Instant> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public Instant convert(Date source) { |
||||
return source == null ? null : toInstant(source); |
||||
} |
||||
} |
||||
|
||||
public static enum InstantToDateConverter implements Converter<Instant, Date> { |
||||
|
||||
INSTANCE; |
||||
|
||||
@Override |
||||
public Date convert(Instant source) { |
||||
return source == null ? null : toDate(source.atZone(systemDefault()).toInstant()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,133 @@
@@ -0,0 +1,133 @@
|
||||
/* |
||||
* Copyright 2015 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.convert; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
import static org.threeten.bp.DateTimeUtils.*; |
||||
|
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Date; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.ConversionService; |
||||
import org.springframework.core.convert.converter.Converter; |
||||
import org.springframework.core.convert.support.GenericConversionService; |
||||
import org.threeten.bp.Instant; |
||||
import org.threeten.bp.LocalDate; |
||||
import org.threeten.bp.LocalDateTime; |
||||
import org.threeten.bp.LocalTime; |
||||
|
||||
/** |
||||
* Unit tests for {@link Jsr310BackPortConverters}. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class Jsr310BackPortConvertersUnitTests { |
||||
|
||||
static final Date NOW = new Date(); |
||||
static final ConversionService CONVERSION_SERVICE; |
||||
|
||||
static { |
||||
|
||||
GenericConversionService conversionService = new GenericConversionService(); |
||||
|
||||
for (Converter<?, ?> converter : Jsr310BackPortConverters.getConvertersToRegister()) { |
||||
conversionService.addConverter(converter); |
||||
} |
||||
|
||||
CONVERSION_SERVICE = conversionService; |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-606 |
||||
*/ |
||||
@Test |
||||
public void convertsDateToLocalDateTime() { |
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class).toString(), |
||||
is(format(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS"))); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-606 |
||||
*/ |
||||
@Test |
||||
public void convertsLocalDateTimeToDate() { |
||||
|
||||
LocalDateTime now = LocalDateTime.now(); |
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd'T'HH:mm:ss.SSS"), is(now.toString())); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-606 |
||||
*/ |
||||
@Test |
||||
public void convertsDateToLocalDate() { |
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class).toString(), is(format(NOW, "yyyy-MM-dd"))); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-606 |
||||
*/ |
||||
@Test |
||||
public void convertsLocalDateToDate() { |
||||
|
||||
LocalDate now = LocalDate.now(); |
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd"), is(now.toString())); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-606 |
||||
*/ |
||||
@Test |
||||
public void convertsDateToLocalTime() { |
||||
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class).toString(), is(format(NOW, "HH:mm:ss.SSS"))); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-606 |
||||
*/ |
||||
@Test |
||||
public void convertsLocalTimeToDate() { |
||||
|
||||
LocalTime now = LocalTime.now(); |
||||
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "HH:mm:ss.SSS"), is(now.toString())); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-623 |
||||
*/ |
||||
@Test |
||||
public void convertsDateToInstant() { |
||||
|
||||
Date now = new Date(); |
||||
assertThat(CONVERSION_SERVICE.convert(now, Instant.class), is(toInstant(now))); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-623 |
||||
*/ |
||||
@Test |
||||
public void convertsInstantToDate() { |
||||
|
||||
Date now = new Date(); |
||||
assertThat(CONVERSION_SERVICE.convert(toInstant(now), Date.class), is(now)); |
||||
} |
||||
|
||||
private static String format(Date date, String format) { |
||||
return new SimpleDateFormat(format).format(date); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue