You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2017/02/11 22:40:16 UTC
incubator-tamaya-sandbox git commit: Added converters.
Repository: incubator-tamaya-sandbox
Updated Branches:
refs/heads/master d32ff6ba0 -> 1546f334d
Added converters.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/1546f334
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/1546f334
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/1546f334
Branch: refs/heads/master
Commit: 1546f334d729c5aaf9d2d7a17ab89c8a0c2e57b5
Parents: d32ff6b
Author: anatole <an...@apache.org>
Authored: Sat Feb 11 23:40:08 2017 +0100
Committer: anatole <an...@apache.org>
Committed: Sat Feb 11 23:40:08 2017 +0100
----------------------------------------------------------------------
.../tamaya/jodatime/InstantConverter.java | 102 +++++++++++++++++++
.../tamaya/jodatime/LocalDateConverter.java | 83 +++++++++++++++
.../tamaya/jodatime/LocalTimeConverter.java | 64 ++++++++++++
.../org.apache.tamaya.spi.PropertyConverter | 3 +
4 files changed, 252 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1546f334/jodatime/src/main/java/org/apache/tamaya/jodatime/InstantConverter.java
----------------------------------------------------------------------
diff --git a/jodatime/src/main/java/org/apache/tamaya/jodatime/InstantConverter.java b/jodatime/src/main/java/org/apache/tamaya/jodatime/InstantConverter.java
new file mode 100644
index 0000000..20d146b
--- /dev/null
+++ b/jodatime/src/main/java/org/apache/tamaya/jodatime/InstantConverter.java
@@ -0,0 +1,102 @@
+/*
+ * 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.tamaya.jodatime;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+import org.joda.time.DateTime;
+import org.joda.time.Instant;
+import org.joda.time.format.*;
+
+import java.util.Objects;
+
+/**
+ * Converter, converting from {@code String} to Joda-Time's
+ * {@code Instant}.
+ *
+ * The converter supports the following formats for the provided
+ * time information:
+ *
+ * <ul>
+ * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSZ}</li>
+ * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSz}</li>
+ * <li>{@code yyyy-MM-dd'T'HH:mm:ssZ}</li>
+ * <li>{@code yyyy-MM-dd'T'HH:mm:ssz}</li>
+ * <li>{@code yyyy-MM-dd'T'HH:mmZ}</li>
+ * <li>{@code yyyy-MM-dd'T'HH:mmz}</li>
+ * <li>{@code yyyy-MM-dd'T'HHZ}</li>
+ * <li>{@code yyyy-MM-dd'T'HHz}</li>
+ * </ul>
+ */
+public class InstantConverter implements PropertyConverter<Instant> {
+ static final String PARSER_FORMATS[] = {
+ "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
+ "yyyy-MM-dd'T'HH:mm:ss.SSSz",
+ "yyyy-MM-dd'T'HH:mm:ss.SSS z",
+ "yyyy-MM-dd'T'HH:mm:ssZ",
+ "yyyy-MM-dd'T'HH:mm:ssz",
+ "yyyy-MM-dd'T'HH:mm:ss z",
+ "yyyy-MM-dd'T'HH:mmZ",
+ "yyyy-MM-dd'T'HH:mmz",
+ "yyyy-MM-dd'T'HH:mm z",
+ "yyyy-MM-dd'T'HHZ",
+ "yyyy-MM-dd'T'HHz",
+ "yyyy-MM-dd'T'HH z",
+ };
+
+
+ // The DateTimeFormatter returned by ISODateTimeFormat are thread safe
+ // according to the JavaDoc of JodaTime
+ final static DateTimeParser FORMATS[] = {
+ DateTimeFormat.forPattern(PARSER_FORMATS[0]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[1]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[2]).getParser(),
+
+ DateTimeFormat.forPattern(PARSER_FORMATS[3]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[4]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[5]).getParser(),
+
+ DateTimeFormat.forPattern(PARSER_FORMATS[6]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[7]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[8]).getParser(),
+
+ DateTimeFormat.forPattern(PARSER_FORMATS[9]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[10]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[11]).getParser(),
+ };
+
+ protected static final DateTimeFormatter formatter;
+
+ static {
+ formatter = new DateTimeFormatterBuilder().append(null, FORMATS).toFormatter();
+ }
+
+ @Override
+ public Instant convert(String value, ConversionContext context) {
+ context.addSupportedFormats(InstantConverter.class, PARSER_FORMATS);
+
+ String trimmed = Objects.requireNonNull(value).trim();
+ try {
+ return ISODateTimeFormat.dateTimeParser().parseDateTime(trimmed).toInstant();
+ } catch (RuntimeException e) {
+ // Ok, go on and try the next parser
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1546f334/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalDateConverter.java
----------------------------------------------------------------------
diff --git a/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalDateConverter.java b/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalDateConverter.java
new file mode 100644
index 0000000..0739e93
--- /dev/null
+++ b/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalDateConverter.java
@@ -0,0 +1,83 @@
+/*
+ * 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.tamaya.jodatime;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+import org.joda.time.LocalDate;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+import org.joda.time.format.DateTimeFormatterBuilder;
+import org.joda.time.format.DateTimeParser;
+
+import java.util.Objects;
+
+/**
+ * Converter, converting from {@code String} to Joda-Time's
+ * {@code LocalDate}.
+ *
+ * The converter supports the following formats for the provided
+ * time information:
+ *
+ * <ul>
+ * <li>{@code yyyy ['-' MM ['-' dd]]}</li>
+ * <li>{@code yyyy ['-' DDD]}</li>
+ * <li>{@code xxxx '-W' ww ['-' e]}</li>
+ * </ul>
+ */
+public class LocalDateConverter implements PropertyConverter<LocalDate> {
+ static final String PARSER_FORMATS[] = {
+ "yyyy ['-' MM ['-' dd]]",
+ "yyyy ['-' DDD]",
+ "xxxx '-W' ww ['-' e]",
+ "yyyy ['-' dd ['-' MM]]",
+ };
+
+
+ // The DateTimeFormatter returned by ISODateTimeFormat are thread safe
+ // according to the JavaDoc of JodaTime
+ final static DateTimeParser FORMATS[] = {
+ DateTimeFormat.forPattern(PARSER_FORMATS[0]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[1]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[2]).getParser(),
+ DateTimeFormat.forPattern(PARSER_FORMATS[3]).getParser(),
+ };
+
+ protected static final DateTimeFormatter formatter;
+
+ static {
+ formatter = new DateTimeFormatterBuilder().append(null, FORMATS).toFormatter();
+ }
+
+ @Override
+ public LocalDate convert(String value, ConversionContext context) {
+ context.addSupportedFormats(LocalDateConverter.class, PARSER_FORMATS);
+
+ String trimmed = Objects.requireNonNull(value).trim();
+ LocalDate result = null;
+
+ try {
+ result = formatter.parseLocalDate(trimmed);
+ } catch (RuntimeException e) {
+ // Ok, go on and try the next parser
+ }
+
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1546f334/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalTimeConverter.java
----------------------------------------------------------------------
diff --git a/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalTimeConverter.java b/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalTimeConverter.java
new file mode 100644
index 0000000..22593c0
--- /dev/null
+++ b/jodatime/src/main/java/org/apache/tamaya/jodatime/LocalTimeConverter.java
@@ -0,0 +1,64 @@
+/*
+ * 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.tamaya.jodatime;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+import org.joda.time.LocalTime;
+import org.joda.time.format.*;
+
+import java.util.Objects;
+
+/**
+ * Converter, converting from {@code String} to Joda-Time's
+ * {@code LocalTime}.
+ *
+ * The converter supports the following formats for the provided
+ * time information:
+ *
+ * <ul>
+ * <li>{@code ['T']` time-element}</li>
+ * <li>{@code time-element = HH [minute-element] | [fraction]}</li>
+ * <li>{@code minute-element = ':' mm [second-element] | [fraction]}</li>
+ * <li>{@code second-element = ':' ss [fraction] }</li>
+ * <li>{@code fraction = ('.' | ',') digit+ }</li>
+ * </ul>
+ */
+public class LocalTimeConverter implements PropertyConverter<LocalTime> {
+ static final String PARSER_FORMATS[] = {
+ "['T']` time-element\n" +
+ "time-element = HH [minute-element] | [fraction]\n" +
+ "minute-element = ':' mm [second-element] | [fraction]\n" +
+ "second-element = ':' ss [fraction]" +
+ "fraction = ('.' | ',') digit+`",
+ };
+
+ @Override
+ public LocalTime convert(String value, ConversionContext context) {
+ context.addSupportedFormats(LocalTimeConverter.class, PARSER_FORMATS);
+
+ String trimmed = Objects.requireNonNull(value).trim();
+ try {
+ return ISODateTimeFormat.localTimeParser().parseLocalTime(trimmed);
+ } catch (RuntimeException e) {
+ // Ok, go on and try the next parser
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1546f334/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
----------------------------------------------------------------------
diff --git a/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
index 13c48c5..a8bb765 100644
--- a/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
+++ b/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
@@ -20,3 +20,6 @@ org.apache.tamaya.jodatime.DateTimeConverter
org.apache.tamaya.jodatime.DateTimeZoneConverter
org.apache.tamaya.jodatime.PeriodConverter
org.apache.tamaya.jodatime.DurationConverter
+org.apache.tamaya.jodatime.InstantConverter
+org.apache.tamaya.jodatime.LocalDateConverter
+org.apache.tamaya.jodatime.LocalTimeConverter