tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ple...@apache.org
Subject [3/4] incubator-tamaya git commit: TAMAYA-91 Introducing the PeriodConverter. Currently it is able to handle periods given in the standard ISO 8501 format for periods.
Date Tue, 25 Aug 2015 19:49:48 GMT
TAMAYA-91 Introducing the PeriodConverter. Currently it is able to handle periods given in
the standard ISO 8501 format for periods.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/7010b594
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/7010b594
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/7010b594

Branch: refs/heads/master
Commit: 7010b594b6a08e486c183d5b983ae6514eff1179
Parents: 80995f4
Author: Oliver B. Fischer <plexus@apache.org>
Authored: Sun Aug 23 13:22:24 2015 +0200
Committer: Oliver B. Fischer <plexus@apache.org>
Committed: Tue Aug 25 21:23:41 2015 +0200

----------------------------------------------------------------------
 .../apache/tamaya/jodatime/PeriodConverter.java | 95 ++++++++++++++++++++
 .../tamaya/jodatime/PeriodConverterTest.java    | 45 ++++++++++
 2 files changed, 140 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7010b594/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java
----------------------------------------------------------------------
diff --git a/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java
b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java
new file mode 100644
index 0000000..6950606
--- /dev/null
+++ b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java
@@ -0,0 +1,95 @@
+/*
+ * 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.PropertyConverter;
+import org.joda.time.MutablePeriod;
+import org.joda.time.Period;
+import org.joda.time.ReadWritablePeriod;
+import org.joda.time.format.ISOPeriodFormat;
+import org.joda.time.format.PeriodParser;
+
+import java.util.Locale;
+import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * <p>A {@link PropertyConverter} for converting a string representation of a
+ * given period into a {@link org.joda.time.Period} instance.</p>
+ *
+ * <p>This converter supports the following string representations of a
+ * period:
+ *
+ *   <ol>
+ *     <li>Alternatice format ({@code Pyyyy-mm-ddThh:mm:ss})</li>
+ *     <li>ISO format ({@code PyYmMwWdDThHmMsS})</li>
+ *   </ol>
+ * </p>
+ *
+ *
+ */
+public class PeriodConverter implements PropertyConverter<org.joda.time.Period> {
+
+    private final static PeriodParser ISO_FORMAT = ISOPeriodFormat.standard()
+                                                                  .getParser();
+
+    private final static PeriodParser ALTERNATIVE_FORMAT = ISOPeriodFormat.alternateExtended()
+                                                                          .getParser();
+
+    private final static String ISO_REGEX = "^P\\d+Y(?:\\d+M)?(?:\\d+W)?(?:\\d+D)?(?:T(?:\\d+H)?(?:\\d+M)?(?:\\d+S)?)?";
+    private final static String ALTERNATIVE_REGEX = "^P\\d+(?:-\\d+(?:-\\d+(?:T(?:\\d+:(?:\\d+:(?:\\d+)?+)?+)?+)?+)?+)?+$";
+
+    private final static Pattern ISO_PATTERN = Pattern.compile(ISO_REGEX);
+
+    private final static Pattern ALTERNATIVE_PATTERN = Pattern.compile(ALTERNATIVE_REGEX);
+
+    @Override
+    public Period convert(String value) {
+        String trimmed = Objects.requireNonNull(value).trim();
+        MutablePeriod result = null;
+
+        PeriodParser format = null;
+
+        if (isISOFormat(trimmed)) {
+            format = ISO_FORMAT;
+        } else if (isAlternativeFormat(trimmed)) {
+            format = ALTERNATIVE_FORMAT;
+        }
+
+        if (format != null) {
+            result = new MutablePeriod();
+            int parseResult = format.parseInto(result, trimmed, 0, Locale.ENGLISH);
+
+            if (parseResult < 0) {
+                result = null;
+            }
+        }
+
+        return result != null ? result.toPeriod() : null;
+    }
+
+    private boolean isISOFormat(String value) {
+        return ISO_PATTERN.matcher(value).matches();
+    }
+
+    private boolean isAlternativeFormat(String value) {
+        return ALTERNATIVE_PATTERN.matcher(value).matches();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7010b594/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java
----------------------------------------------------------------------
diff --git a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java
b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java
index 2dd01ce..29315a5 100644
--- a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java
+++ b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java
@@ -18,5 +18,50 @@
  */
 package org.apache.tamaya.jodatime;
 
+import org.joda.time.Period;
+import org.joda.time.format.ISOPeriodFormat;
+import org.joda.time.format.PeriodFormatter;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
 public class PeriodConverterTest {
+
+    private static PeriodConverter converter = new PeriodConverter();
+
+    private static PeriodFormatter FORMATTER = ISOPeriodFormat.standard();
+
+    @Test
+    public void canConvertPropertiesInAllSupportedFormats() {
+        Object[][] inputResultPairs = {
+             // ISO format
+             {"P0007Y", FORMATTER.parsePeriod("P7Y0M0W0DT0H0M0S")},
+             {"P7Y", FORMATTER.parsePeriod("P7Y0M0W0DT0H0M0S")},
+             {"P7891Y", FORMATTER.parsePeriod("P7891Y0M0W0DT0H0M0S")},
+             {"P7891Y", FORMATTER.parsePeriod("P7891Y0M0W0DT0H0M0S")},
+
+             {"P1Y1M", FORMATTER.parsePeriod("P1Y1M0W0DT0H0M0S")},
+             {"P1Y9M", FORMATTER.parsePeriod("P1Y9M0W0DT0H0M0S")},
+
+             {"P1Y1D", FORMATTER.parsePeriod("P1Y0M0W1DT0H0M0S")},
+             {"P1YT1S", FORMATTER.parsePeriod("P1Y0M0W0DT0H0M1S")},
+
+             // Alternative format
+             {"P0002-03-00T00:00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")},
+             {"P0002-03T00:00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")},
+             {"P0002T00:00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")},
+             {"P0002T00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")}
+
+        };
+
+        for (Object[] pair : inputResultPairs) {
+            Period period = converter.convert((String) pair[0]);
+
+            assertThat("Converter failed to convert input value " + pair[0], period, notNullValue());
+            assertThat(period, equalTo((Period)pair[1]));
+        }
+    }
 }


Mime
View raw message