changeset 486:b1e63257aefa

PeriodFormatter
author Devel 1
date Wed, 09 Aug 2017 13:29:25 +0200
parents 2fd1eb419a15
children c58400b7591e
files stress-tester/src/main/java/com/passus/st/utils/Formatter.java stress-tester/src/main/java/com/passus/st/utils/PeriodFormatter.java stress-tester/src/test/java/com/passus/st/utils/PeriodFormatterTest.java
diffstat 3 files changed, 39 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/utils/Formatter.java	Wed Aug 09 10:55:59 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/Formatter.java	Wed Aug 09 13:29:25 2017 +0200
@@ -10,12 +10,14 @@
  */
 public interface Formatter<T> extends Transformer<String, T> {
 
+    @Override
     public default String reverseTransform(T value) throws ConversionException {
         return reverseTransform(value, Locale.getDefault());
     }
 
     public String reverseTransform(T value, Locale locale) throws ConversionException;
 
+    @Override
     public default T transform(String value) throws ConversionException {
         return transform(value, Locale.getDefault());
     }
--- a/stress-tester/src/main/java/com/passus/st/utils/PeriodFormatter.java	Wed Aug 09 10:55:59 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/PeriodFormatter.java	Wed Aug 09 13:29:25 2017 +0200
@@ -71,49 +71,56 @@
         0L, 1000L, 60L * 1000L, 60L * 60L * 1000L, 24L * 60L * 60L * 1000L, 7L * 24L * 60L * 60L * 1000L
     };
 
+    private final boolean printMillis;
+
+    public PeriodFormatter() {
+        this(true);
+    }
+
+    public PeriodFormatter(boolean printMillis) {
+        this.printMillis = printMillis;
+    }
+
     /**
-     * Formatts specified amount of miliseconds into String with weeks, days,
-     * hours, minutes and seconds.
+     * Formatts specified amount of miliseconds into String with weeks, days, hours,
+     * minutes, seconds and optionally milliseconds.
      *
-     * Miliseconds of each second are truncated. Leading time units with value 0
-     * are ignored. No week nor month units are calculated.
+     * Leading time units with value 0 are ignored. No week nor month units are
+     * calculated.
      *
-     * @param time duration in miliseconds
+     * @param timeLong duration in miliseconds
      * @param locale not used in this implementation
      * @return text with formatted duration
      */
     @Override
-    public String reverseTransform(Long time, Locale locale) {
-        if (time == null) {
+    public String reverseTransform(Long timeLong, Locale locale) {
+        if (timeLong == null) {
             return "";
         }
 
+        long time = timeLong;
         long[] times = {0, 0, 0, 0, 0, 0};
-        time /= DURATION[0];
-
-        for (int i = 1; i < DURATION.length; i++) {
+        for (int i = 0; i < DURATION.length; i++) {
             times[i] = time % DURATION[i];
             time /= DURATION[i];
         }
-        StringBuilder sb = new StringBuilder();
-        boolean insertSpace = false;
-        boolean empty = true;
 
-        for (int i = DURATION.length - 1; i > 0; i--) {
+        StringBuilder sb = new StringBuilder();
+        int bound = printMillis ? 0 : 1;
+        for (int i = DURATION.length - 1; i >= bound; i--) {
             if (times[i] != 0) {
-                if (insertSpace) {
-                    sb.append(" ");
+                if (sb.length() != 0) {
+                    sb.append(' ');
                 }
                 sb.append(times[i]);
                 sb.append(UNITS[i]);
-                insertSpace = true;
-                empty = false;
             }
         }
-        if (empty) {
-            sb.append("0s");
+        if (sb.length() != 0) {
+            return sb.toString();
+        } else {
+            return printMillis ? "0ms" : "0s";
         }
-        return sb.toString();
     }
 
     /**
@@ -124,7 +131,7 @@
      * @param time text containing duration description
      * @param locale not used in this implementation
      * @return duration in miliseconds
-     * @throws ParseException in case of not well formatted input
+     * @throws ConversionException in case of not well formatted input
      */
     @Override
     public Long transform(String time, Locale locale) throws ConversionException {
--- a/stress-tester/src/test/java/com/passus/st/utils/PeriodFormatterTest.java	Wed Aug 09 10:55:59 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/utils/PeriodFormatterTest.java	Wed Aug 09 13:29:25 2017 +0200
@@ -10,7 +10,8 @@
  */
 public class PeriodFormatterTest {
 
-    private final PeriodFormatter instance = new PeriodFormatter();
+    private final PeriodFormatter instance = new PeriodFormatter(false);
+    private final PeriodFormatter instanceWithMillis = new PeriodFormatter(true);
 
     private void assertInvalid(String value) {
         try {
@@ -80,6 +81,12 @@
 
     @Test
     public void testPrint() throws Exception {
+        assertEquals(instanceWithMillis.reverseTransform(0L, null), "0ms");
+        assertEquals(instanceWithMillis.reverseTransform(1L, null), "1ms");
+        assertEquals(instanceWithMillis.reverseTransform(999L, null), "999ms");
+        assertEquals(instanceWithMillis.reverseTransform(1000L, null), "1s");
+        assertEquals(instanceWithMillis.reverseTransform(1001L, null), "1s 1ms");
+
         assertEquals(instance.reverseTransform(0L, null), "0s");
         assertEquals(instance.reverseTransform(1L, null), "0s");
         assertEquals(instance.reverseTransform(999L, null), "0s");