You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/05/08 05:50:19 UTC

camel git commit: CAMEL-9744: camel-bindy - Add support for Java 8 date and time API. Thanks to Arno Noordover for the patch.

Repository: camel
Updated Branches:
  refs/heads/master 2820ca76b -> 1935ddfe1


CAMEL-9744: camel-bindy - Add support for Java 8 date and time API. Thanks to Arno Noordover for the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1935ddfe
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1935ddfe
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1935ddfe

Branch: refs/heads/master
Commit: 1935ddfe1c4339e0269062212eef90c595b03d61
Parents: 2820ca7
Author: Claus Ibsen <da...@apache.org>
Authored: Sun May 8 07:50:09 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 8 07:50:09 2016 +0200

----------------------------------------------------------------------
 .../camel/dataformat/bindy/FormatFactory.java   | 12 +++
 .../bindy/format/LocalDatePatternFormat.java    | 85 ++++++++++++++++++
 .../format/LocalDateTimePatternFormat.java      | 94 +++++++++++++++++++
 .../bindy/format/LocalTimePatternFormat.java    | 95 ++++++++++++++++++++
 .../date/BindyDatePatternCsvUnmarshallTest.java | 38 +++++++-
 5 files changed, 323 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1935ddfe/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
index 7a63f77..1971012 100755
--- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/FormatFactory.java
@@ -19,6 +19,9 @@ package org.apache.camel.dataformat.bindy;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.util.Date;
 import java.util.Locale;
 
@@ -40,6 +43,9 @@ import org.apache.camel.dataformat.bindy.format.FloatFormat;
 import org.apache.camel.dataformat.bindy.format.FloatPatternFormat;
 import org.apache.camel.dataformat.bindy.format.IntegerFormat;
 import org.apache.camel.dataformat.bindy.format.IntegerPatternFormat;
+import org.apache.camel.dataformat.bindy.format.LocalDatePatternFormat;
+import org.apache.camel.dataformat.bindy.format.LocalDateTimePatternFormat;
+import org.apache.camel.dataformat.bindy.format.LocalTimePatternFormat;
 import org.apache.camel.dataformat.bindy.format.LongFormat;
 import org.apache.camel.dataformat.bindy.format.LongPatternFormat;
 import org.apache.camel.dataformat.bindy.format.ShortFormat;
@@ -112,6 +118,12 @@ public final class FormatFactory {
             return new CharacterFormat();
         } else if (clazz == boolean.class || clazz == Boolean.class) {
             return new BooleanFormat();
+        } else if (clazz == LocalDate.class) {
+            return new LocalDatePatternFormat(pattern, getLocale(locale));
+        } else if (clazz == LocalDateTime.class) {
+            return new LocalDateTimePatternFormat(pattern, timezone, getLocale(locale));
+        } else if (clazz == LocalTime.class) {
+            return new LocalTimePatternFormat(pattern, timezone, getLocale(locale));
         } else if (clazz.isEnum()) {
             @SuppressWarnings({"rawtypes", "unchecked"})
             Format<?> fmt = new EnumFormat(clazz);

http://git-wip-us.apache.org/repos/asf/camel/blob/1935ddfe/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDatePatternFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDatePatternFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDatePatternFormat.java
new file mode 100644
index 0000000..0b4daee
--- /dev/null
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDatePatternFormat.java
@@ -0,0 +1,85 @@
+/**
+ * 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.camel.dataformat.bindy.format;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.Locale;
+import org.apache.camel.dataformat.bindy.PatternFormat;
+import org.apache.camel.util.ObjectHelper;
+
+public class LocalDatePatternFormat implements PatternFormat<LocalDate> {
+
+    private String pattern;
+    private Locale locale;
+
+    public LocalDatePatternFormat() {
+    }
+
+    public LocalDatePatternFormat(String pattern, Locale locale) {
+        this.pattern = pattern;
+        this.locale = locale;
+    }
+
+    public String format(LocalDate object) throws Exception {
+        ObjectHelper.notNull(this.pattern, "pattern");
+        return this.getDateFormat().format(object);
+    }
+
+    public LocalDate parse(String string) throws Exception {
+
+        LocalDate date;
+        DateTimeFormatter df = this.getDateFormat();
+
+        ObjectHelper.notNull(this.pattern, "pattern");
+
+        if (doesStringFitLengthOfPattern(string)) {
+            date = LocalDate.parse(string, df);
+            return date;
+        } else {
+            throw new FormatException("Date provided does not fit the pattern defined");
+        }
+
+    }
+
+    private boolean doesStringFitLengthOfPattern(String string) {
+        return string.length() <= this.pattern.length();
+    }
+
+    protected DateTimeFormatter getDateFormat() {
+        DateTimeFormatter result;
+        if (locale != null) {
+            result = DateTimeFormatter.ofPattern(pattern, locale);
+        } else {
+            result = DateTimeFormatter.ofPattern(pattern);
+        }
+        return result;
+    }
+
+    public String getPattern() {
+        return pattern;
+    }
+
+    /**
+     * Sets the pattern
+     * 
+     * @param pattern the pattern
+     */
+    public void setPattern(String pattern) {
+        this.pattern = pattern;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1935ddfe/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDateTimePatternFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDateTimePatternFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDateTimePatternFormat.java
new file mode 100644
index 0000000..62a0399
--- /dev/null
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalDateTimePatternFormat.java
@@ -0,0 +1,94 @@
+/**
+ * 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.camel.dataformat.bindy.format;
+
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.Locale;
+import org.apache.camel.dataformat.bindy.PatternFormat;
+import org.apache.camel.util.ObjectHelper;
+
+public class LocalDateTimePatternFormat implements PatternFormat<LocalDateTime> {
+
+    private String pattern;
+    private Locale locale;
+    private ZoneId zone;
+
+    public LocalDateTimePatternFormat() {
+    }
+
+    public LocalDateTimePatternFormat(String pattern, String timezone, Locale locale) {
+        this.pattern = pattern;
+        this.locale = locale;
+        if (timezone.isEmpty()) {
+            this.zone = ZoneId.systemDefault();
+        } else {
+            this.zone = ZoneId.of(timezone);
+        }
+    }
+
+    public String format(LocalDateTime object) throws Exception {
+        ObjectHelper.notNull(this.pattern, "pattern");
+        return this.getDateFormat().format(object);
+    }
+
+    public LocalDateTime parse(String string) throws Exception {
+
+        LocalDateTime date;
+        DateTimeFormatter df = this.getDateFormat();
+
+        ObjectHelper.notNull(this.pattern, "pattern");
+
+        if (doesStringFitLengthOfPattern(string)) {
+            date = LocalDateTime.parse(string, df);
+            return date;
+        } else {
+            throw new FormatException("Date provided does not fit the pattern defined");
+        }
+
+    }
+
+    private boolean doesStringFitLengthOfPattern(String string) {
+        return string.length() <= this.pattern.length();
+    }
+
+    protected DateTimeFormatter getDateFormat() {
+        DateTimeFormatter result;
+        if (locale != null) {
+            result = DateTimeFormatter.ofPattern(pattern, locale)
+                    .withZone(zone);
+        } else {
+            result = DateTimeFormatter.ofPattern(pattern)
+                    .withZone(zone);
+        }
+        return result;
+    }
+
+    public String getPattern() {
+        return pattern;
+    }
+
+    /**
+     * Sets the pattern
+     *
+     * @param pattern the pattern
+     */
+    public void setPattern(String pattern) {
+        this.pattern = pattern;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1935ddfe/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalTimePatternFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalTimePatternFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalTimePatternFormat.java
new file mode 100644
index 0000000..dbab92f
--- /dev/null
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/LocalTimePatternFormat.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.camel.dataformat.bindy.format;
+
+import java.time.LocalTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.Locale;
+import org.apache.camel.dataformat.bindy.PatternFormat;
+import org.apache.camel.util.ObjectHelper;
+
+
+public class LocalTimePatternFormat implements PatternFormat<LocalTime> {
+
+    private String pattern;
+    private Locale locale;
+    private ZoneId zone;
+
+    public LocalTimePatternFormat() {
+    }
+
+    public LocalTimePatternFormat(String pattern, String timezone, Locale locale) {
+        this.pattern = pattern;
+        this.locale = locale;
+        if (timezone.isEmpty()) {
+            this.zone = ZoneId.systemDefault();
+        } else {
+            this.zone = ZoneId.of(timezone);
+        }
+    }
+
+    public String format(LocalTime object) throws Exception {
+        ObjectHelper.notNull(this.pattern, "pattern");
+        return this.getDateFormat().format(object);
+    }
+
+    public LocalTime parse(String string) throws Exception {
+
+        LocalTime date;
+        DateTimeFormatter df = this.getDateFormat();
+
+        ObjectHelper.notNull(this.pattern, "pattern");
+
+        if (doesStringFitLengthOfPattern(string)) {
+            date = LocalTime.parse(string, df);
+            return date;
+        } else {
+            throw new FormatException("Date provided does not fit the pattern defined");
+        }
+
+    }
+
+    private boolean doesStringFitLengthOfPattern(String string) {
+        return string.length() <= this.pattern.length();
+    }
+
+    protected DateTimeFormatter getDateFormat() {
+        DateTimeFormatter result;
+        if (locale != null) {
+            result = DateTimeFormatter.ofPattern(pattern, locale)
+                    .withZone(zone);
+        } else {
+            result = DateTimeFormatter.ofPattern(pattern)
+                    .withZone(zone);
+        }
+        return result;
+    }
+
+    public String getPattern() {
+        return pattern;
+    }
+
+    /**
+     * Sets the pattern
+     *
+     * @param pattern the pattern
+     */
+    public void setPattern(String pattern) {
+        this.pattern = pattern;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1935ddfe/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/date/BindyDatePatternCsvUnmarshallTest.java
----------------------------------------------------------------------
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/date/BindyDatePatternCsvUnmarshallTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/date/BindyDatePatternCsvUnmarshallTest.java
index e3bd410..04f1857 100644
--- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/date/BindyDatePatternCsvUnmarshallTest.java
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/date/BindyDatePatternCsvUnmarshallTest.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.dataformat.bindy.model.date;
 
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.util.Date;
 
 import org.apache.camel.EndpointInject;
@@ -48,7 +51,7 @@ public class BindyDatePatternCsvUnmarshallTest extends AbstractJUnit4SpringConte
     @Test
     @DirtiesContext
     public void testUnMarshallMessage() throws Exception {
-        expected = "10,Christian,Mueller,12-24-2013";
+        expected = "10,Christian,Mueller,12-24-2013,12-26-2015,01-06-2016 12:14:49,13:15:01";
 
         result.expectedBodiesReceived(expected + "\r\n");
 
@@ -85,6 +88,15 @@ public class BindyDatePatternCsvUnmarshallTest extends AbstractJUnit4SpringConte
         @DataField(pos = 4, pattern = "MM-dd-yyyy")
         private Date orderDate;
 
+        @DataField(pos = 5, pattern = "MM-dd-yyyy")
+        private LocalDate deliveryDate;
+
+        @DataField(pos = 6, pattern = "MM-dd-yyyy HH:mm:ss")
+        private LocalDateTime returnedDateTime;
+
+        @DataField(pos = 7, pattern = "HH:mm:ss")
+        private LocalTime receivedTime;
+
         public int getOrderNr() {
             return orderNr;
         }
@@ -121,5 +133,29 @@ public class BindyDatePatternCsvUnmarshallTest extends AbstractJUnit4SpringConte
         public String toString() {
             return "Model : " + Order.class.getName() + " : " + this.orderNr + ", " + this.firstName + ", " + this.lastName + ", "  + String.valueOf(this.orderDate);
         }
+
+        public LocalDate getDeliveryDate() {
+            return deliveryDate;
+        }
+
+        public void setDeliveryDate(LocalDate deliveryDate) {
+            this.deliveryDate = deliveryDate;
+        }
+
+        public LocalDateTime getReturnedDateTime() {
+            return returnedDateTime;
+        }
+
+        public void setReturnedDateTime(LocalDateTime returnedDateTime) {
+            this.returnedDateTime = returnedDateTime;
+        }
+
+        public LocalTime getReceivedTime() {
+            return receivedTime;
+        }
+
+        public void setReceivedTime(LocalTime receivedTime) {
+            this.receivedTime = receivedTime;
+        }
     }
 }