myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lof...@apache.org
Subject [myfaces-tobago] branch master updated: TOBAGO-2004: Duration converter accept simple values
Date Thu, 29 Aug 2019 06:28:51 GMT
This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new ae16197  TOBAGO-2004: Duration converter accept simple values
ae16197 is described below

commit ae16197372b105c586b2d26e539acdb9dd3c0f29
Author: Udo Schnurpfeil <lofwyr@apache.org>
AuthorDate: Thu Aug 29 08:03:50 2019 +0200

    TOBAGO-2004: Duration converter accept simple values
---
 .../myfaces/tobago/convert/DurationConverter.java  | 49 ++++++++++++++++------
 .../tobago/convert/DurationConverterUnitTest.java  |  8 +++-
 2 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/convert/DurationConverter.java
b/tobago-core/src/main/java/org/apache/myfaces/tobago/convert/DurationConverter.java
index d46322a..e502450 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/convert/DurationConverter.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/convert/DurationConverter.java
@@ -35,6 +35,23 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.StringTokenizer;
 
+/**
+ * Converts durations. The duration value in the model is of type long.
+ * The string format must have one of this patterns:
+ * <ul>
+ * <li>hh:MM:ss</li>
+ * <li>MM:ss</li>
+ * <li>ss</li>
+ * </ul>
+ * There may be an optional attribute "unit" in the component, which allows to set the unit
of the model.
+ * The default unit is "millis". Examples:
+ * <table>
+ *   <tr><th>input string</th><th>unit</th><th>resulting
long in model</th><th>Remark</th></tr>
+ *   <tr><td>1:15</td><td>milli</td><td>75000</td><td></td></tr>
+ *   <tr><td>1:15:00</td><td>hour</td><td>1</td><td>Loosing
15 Minutes!</td></tr>
+ *   <tr><td>1:15:00</td><td>null</td><td>4500000</td><td></td></tr>
+ * </table>
+ */
 @org.apache.myfaces.tobago.apt.annotation.Converter(id = DurationConverter.CONVERTER_ID)
 public class DurationConverter implements Converter {
 
@@ -77,9 +94,7 @@ public class DurationConverter implements Converter {
       string = (negative ? "-" : "") + minutes + ":"
           + format.format(seconds);
     }
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("string = '{}'", string);
-    }
+    LOG.debug("string = '{}'", string);
     return string;
   }
 
@@ -93,7 +108,7 @@ public class DurationConverter implements Converter {
     while (tokenizer.hasMoreTokens()) {
       elements.add(tokenizer.nextToken());
     }
-    int hours = 0;
+    final int hours;
     final int minutes;
     final int seconds;
     switch (elements.size()) {
@@ -103,9 +118,15 @@ public class DurationConverter implements Converter {
         seconds = Integer.parseInt(elements.get(2));
         break;
       case 2:
+        hours = 0;
         minutes = Integer.parseInt(elements.get(0));
         seconds = Integer.parseInt(elements.get(1));
         break;
+      case 1:
+        hours = 0;
+        minutes = 0;
+        seconds = Integer.parseInt(elements.get(0));
+        break;
       default:
         throw new ConverterException("Cannot parse string='" + string + "'");
     }
@@ -121,15 +142,19 @@ public class DurationConverter implements Converter {
   private static double getUnitFactor(final UIComponent component) {
     final String unitString = ComponentUtils.getStringAttribute(component, Attributes.unit);
     Unit unit;
-    try {
-      unit = Unit.valueOf(unitString);
-    } catch (final Exception e) {
-      LOG.warn("Unsupported unit: '{}'", unitString);
+    if (unitString != null) {
+      try {
+        unit = Unit.valueOf(unitString);
+      } catch (final Exception e) {
+        LOG.warn("Unsupported unit: '{}'", unitString);
+        unit = Unit.milli;
+      }
+    } else {
       unit = Unit.milli;
     }
     switch (unit) {
       case nano:
-        return 0.000000001;
+        return 0.000_000_001;
       default:
       case milli:
         return 0.001;
@@ -138,11 +163,11 @@ public class DurationConverter implements Converter {
       case minute:
         return 60.0;
       case hour:
-        return 3600.0;
+        return 3_600.0;
       case day:
-        return 86400.0;
+        return 86_400.0;
       case year:
-        return 31556736.0;
+        return 31_556_736.0;
     }
   }
 
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/convert/DurationConverterUnitTest.java
b/tobago-core/src/test/java/org/apache/myfaces/tobago/convert/DurationConverterUnitTest.java
index 36bdd88..2a9368a 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/convert/DurationConverterUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/convert/DurationConverterUnitTest.java
@@ -45,18 +45,24 @@ public class DurationConverterUnitTest {
     format("hour", 1L, "1:00:00");
     format("day", 1L, "24:00:00");
     format("year", 1L, "8765:45:36");
-
+    format("milli",75_000L,"1:15");
+    format("hour",1L,"1:00:00");
+    format(null,4_500_000L,"1:15:00");
   }
 
   @Test
   public void testParse() {
 
     parse(null, 1000L, "0:01");
+    parse(null, 1000L, "1");
     parse("second", 1001L, "16:41");
     parse("minute", -16L, "-16:41");
     parse("hour", 1L, "1:00:00");
     parse("day", 1L, "24:00:00");
     parse("year", 1L, "8765:45:36");
+    parse("milli",75_000L,"1:15");
+    parse("hour",1L,"1:15:00");
+    parse(null,4_500_000L,"1:15:00");
   }
 
   private void format(final String unit, final Long aLong, final String string) {


Mime
View raw message