Repository: wicket Updated Branches: refs/heads/WICKET-6200-converters-for-java-time 8eb01465a -> 55fdd6641
WICKET-6200 Add default converters for Java 8 classes Add converter for LocalTime. Remove the ones for OffsetDateTime and ZonedDateTime. We can re-introduce them later if needed (and when we/I know how they are supposed to work). Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/55fdd664 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/55fdd664 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/55fdd664 Branch: refs/heads/WICKET-6200-converters-for-java-time Commit: 55fdd6641b0b34873220fe17b785d06234180734 Parents: 8eb0146 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Sun Jul 17 09:12:37 2016 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Sun Jul 17 09:12:37 2016 +0200 ---------------------------------------------------------------------- .../convert/converter/LocalTimeConverter.java | 46 ++++++++++++++++++ .../converter/OffsetDateTimeConverter.java | 49 -------------------- .../converter/ZonedDateTimeConverter.java | 49 -------------------- .../converter/LocalTimeConverterTest.java | 46 ++++++++++++++++++ .../converter/OffsetDateTimeConverterTest.java | 49 -------------------- .../converter/ZonedDateTimeConverterTest.java | 49 -------------------- 6 files changed, 92 insertions(+), 196 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/55fdd664/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalTimeConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalTimeConverter.java new file mode 100644 index 0000000..7b3af0e --- /dev/null +++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalTimeConverter.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.wicket.util.convert.converter; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; + +/** + * Converts from Object to {@link LocalTime}. + */ +public class LocalTimeConverter extends AbstractJavaTimeConverter<LocalTime> +{ + private static final long serialVersionUID = 1L; + + @Override + protected Class<LocalTime> getTargetType() + { + return LocalTime.class; + } + + @Override + protected LocalTime createTemporal(TemporalAccessor temporalAccessor) + { + return LocalTime.from(temporalAccessor); + } + + @Override + protected DateTimeFormatter getDateTimeFormatter() { + return DateTimeFormatter.ISO_LOCAL_TIME; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/55fdd664/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java deleted file mode 100644 index 88d8216..0000000 --- a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.wicket.util.convert.converter; - -import java.time.OffsetDateTime; -import java.time.ZoneOffset; -import java.time.format.DateTimeFormatter; -import java.time.temporal.TemporalAccessor; - -/** - * Converts from Object to {@link OffsetDateTime}. - */ -public class OffsetDateTimeConverter extends AbstractJavaTimeConverter<OffsetDateTime> -{ - private static final long serialVersionUID = 1L; - - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME; - - @Override - protected Class<OffsetDateTime> getTargetType() - { - return OffsetDateTime.class; - } - - @Override - protected OffsetDateTime createTemporal(TemporalAccessor temporalAccessor) - { - return OffsetDateTime.from(temporalAccessor); - } - - @Override - protected DateTimeFormatter getDateTimeFormatter() { - return DATE_TIME_FORMATTER.withZone(ZoneOffset.UTC); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/55fdd664/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java deleted file mode 100644 index b9d372f..0000000 --- a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.wicket.util.convert.converter; - -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.time.temporal.TemporalAccessor; - -/** - * Converts from Object to {@link ZonedDateTime}. - */ -public class ZonedDateTimeConverter extends AbstractJavaTimeConverter<ZonedDateTime> -{ - private static final long serialVersionUID = 1L; - - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME; - - @Override - protected Class<ZonedDateTime> getTargetType() - { - return ZonedDateTime.class; - } - - @Override - protected ZonedDateTime createTemporal(TemporalAccessor temporalAccessor) - { - return ZonedDateTime.from(temporalAccessor); - } - - @Override - protected DateTimeFormatter getDateTimeFormatter() { - return DATE_TIME_FORMATTER.withZone(ZoneOffset.UTC); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/55fdd664/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java new file mode 100644 index 0000000..fcfdaf4 --- /dev/null +++ b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.wicket.util.convert.converter; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import java.time.LocalTime; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link LocalTimeConverter} + */ +public class LocalTimeConverterTest extends Assert +{ + @Test + public void convertToString() { + LocalTimeConverter converter = new LocalTimeConverter(); + String time = converter.convertToString(LocalTime.of(1, 2, 3), Locale.ENGLISH); + assertThat(time, is(equalTo("01:02:03"))); + } + + @Test + public void convertToObject() { + LocalTimeConverter converter = new LocalTimeConverter(); + LocalTime time = converter.convertToObject("01:02:03", Locale.ENGLISH); + assertThat(time, is(equalTo(LocalTime.of(1, 2, 3)))); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/55fdd664/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java deleted file mode 100644 index a332579..0000000 --- a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.wicket.util.convert.converter; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; - -import java.time.OffsetDateTime; -import java.time.ZoneOffset; -import java.util.Locale; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Tests for {@link OffsetDateTimeConverter} - */ -public class OffsetDateTimeConverterTest extends Assert -{ - @Test - public void convertToString() { - OffsetDateTimeConverter converter = new OffsetDateTimeConverter(); - OffsetDateTime offsetDateTime = OffsetDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); - String dateTime = converter.convertToString(offsetDateTime, Locale.ENGLISH); - assertThat(dateTime, is(equalTo("2016-07-10T23:02:03Z"))); - } - - @Test - public void convertToObject() { - OffsetDateTimeConverter converter = new OffsetDateTimeConverter(); - OffsetDateTime dateTime = converter.convertToObject("2016-07-10T23:02:03+02:00", Locale.ENGLISH); - OffsetDateTime offsetDateTime = OffsetDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); - assertThat(dateTime, is(equalTo(offsetDateTime))); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/55fdd664/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java deleted file mode 100644 index 72ac395..0000000 --- a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.wicket.util.convert.converter; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; - -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.util.Locale; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Tests for {@link ZonedDateTimeConverter} - */ -public class ZonedDateTimeConverterTest extends Assert -{ - @Test - public void convertToString() { - ZonedDateTimeConverter converter = new ZonedDateTimeConverter(); - ZonedDateTime zonedDateTime = ZonedDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); - String dateTime = converter.convertToString(zonedDateTime, Locale.ENGLISH); - assertThat(dateTime, is(equalTo("2016-07-10T23:02:03Z"))); - } - - @Test - public void convertToObject() { - ZonedDateTimeConverter converter = new ZonedDateTimeConverter(); - ZonedDateTime dateTime = converter.convertToObject("2016-07-10T23:02:03Z", Locale.ENGLISH); - ZonedDateTime offsetDateTime = ZonedDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); - assertThat(dateTime, is(equalTo(offsetDateTime))); - } -}
