hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1793400 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/util/ test/java/org/apache/hc/core5/util/
Date Mon, 01 May 2017 19:13:18 GMT
Author: ggregory
Date: Mon May  1 19:13:18 2017
New Revision: 1793400

URL: http://svn.apache.org/viewvc?rev=1793400&view=rev
Log:
Add parse methods in TimeValue and Timeout to consume what toString() produces. Also allow
a TimeValue to be easily converted to a Timeout with the new toTimeout() method.

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java?rev=1793400&r1=1793399&r2=1793400&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
Mon May  1 19:13:18 2017
@@ -27,6 +27,9 @@
 
 package org.apache.hc.core5.util;
 
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.hc.core5.annotation.Contract;
@@ -105,7 +108,8 @@ public class TimeValue {
     }
 
     /**
-     * Returns the given {@code timeValue} if it is not {@code null}, if {@code null} then
returns {@link #ZERO_MILLISECONDS}.
+     * Returns the given {@code timeValue} if it is not {@code null}, if {@code null} then
returns
+     * {@link #ZERO_MILLISECONDS}.
      *
      * @param timeValue
      *            may be {@code null}
@@ -164,6 +168,24 @@ public class TimeValue {
         return of(seconds, TimeUnit.SECONDS);
     }
 
+    /**
+     * Parses a TimeValue in the format {@code <Integer><SPACE><TimeUnit>},
for example {@code "1,200 MILLISECONDS"}
+     * 
+     * @param value
+     *            the TimeValue to parse
+     * @return a new TimeValue
+     * @throws ParseException
+     *             if the number cannot be parsed
+     */
+    public static TimeValue parse(String value) throws ParseException {
+        String split[] = value.split("\\s+");
+        if (split.length < 2) {
+            throw new IllegalArgumentException(String.format("Expected format for <Integer><SPACE><TimeUnit>:
", value));
+        }
+        return TimeValue.of(NumberFormat.getInstance().parse(split[0]).longValue(),
+                TimeUnit.valueOf(split[1].trim().toUpperCase(Locale.ROOT)));
+    }
+
     private final long duration;
 
     private final TimeUnit timeUnit;
@@ -185,8 +207,7 @@ public class TimeValue {
         }
         if (obj instanceof TimeValue) {
             final TimeValue that = (TimeValue) obj;
-            return this.duration == that.duration &&
-                    LangUtils.equals(this.timeUnit, that.timeUnit);
+            return this.duration == that.duration && LangUtils.equals(this.timeUnit,
that.timeUnit);
         }
         return false;
     }
@@ -259,4 +280,9 @@ public class TimeValue {
     public String toString() {
         return String.format("%,d %s", Long.valueOf(duration), timeUnit);
     }
+
+    public Timeout toTimeout() {
+        return Timeout.of(duration, timeUnit);
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java?rev=1793400&r1=1793399&r2=1793400&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
Mon May  1 19:13:18 2017
@@ -27,6 +27,9 @@
 
 package org.apache.hc.core5.util;
 
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.hc.core5.annotation.Contract;
@@ -40,6 +43,8 @@ import org.apache.hc.core5.annotation.Th
 @Contract(threading = ThreadingBehavior.IMMUTABLE)
 public class Timeout extends TimeValue {
 
+    public static final Timeout ZERO_MILLISECONDS = Timeout.of(0, TimeUnit.MILLISECONDS);
+
     /**
      * A disabled timeout represented as 0 {@code MILLISECONDS}.
      */
@@ -145,7 +150,19 @@ public class Timeout extends TimeValue {
         return of(seconds, TimeUnit.SECONDS);
     }
 
-
+    /**
+     * Parses a Timeout in the format {@code <Integer><SPACE><TimeUnit>},
for example {@code "1,200 MILLISECONDS"}
+     * 
+     * @param value
+     *            the TimeValue to parse
+     * @return a new TimeValue
+     * @throws ParseException
+     *             if the number cannot be parsed
+     */
+    public static Timeout parse(String value) throws ParseException {
+        return TimeValue.parse(value).toTimeout();
+    }
+    
     private static long validateDuration(final long duration) {
         if (duration < INT_UNDEFINED) {
             throw new IllegalArgumentException("Duration may not be less than " + INT_UNDEFINED);

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java?rev=1793400&r1=1793399&r2=1793400&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
Mon May  1 19:13:18 2017
@@ -27,6 +27,7 @@
 
 package org.apache.hc.core5.util;
 
+import java.text.ParseException;
 import java.util.concurrent.TimeUnit;
 
 import org.junit.Assert;
@@ -144,4 +145,13 @@ public class TestTimeValue {
         Assert.assertEquals("0 MILLISECONDS", TimeValue.ZERO_MILLISECONDS.toString());
     }
 
+    @Test
+    public void testFromString() throws ParseException {
+        Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807
SECONDS"));
+        Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807
Seconds"));
+        Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807
 Seconds"));
+        Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807\tSeconds"));
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.parse("0 MILLISECONDS"));
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java?rev=1793400&r1=1793399&r2=1793400&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java
Mon May  1 19:13:18 2017
@@ -27,6 +27,7 @@
 
 package org.apache.hc.core5.util;
 
+import java.text.ParseException;
 import java.util.concurrent.TimeUnit;
 
 import org.junit.Assert;
@@ -165,4 +166,19 @@ public class TestTimeout {
         Assert.assertFalse(Timeout.UNDEFINED_SECONDS.isDisabled());
     }
 
+    @Test
+    public void testToString() {
+        Assert.assertEquals("9,223,372,036,854,775,807 SECONDS", Timeout.ofSeconds(Long.MAX_VALUE).toString());
+        Assert.assertEquals("0 MILLISECONDS", Timeout.ZERO_MILLISECONDS.toString());
+    }
+
+    @Test
+    public void testFromString() throws ParseException {
+        Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807
SECONDS"));
+        Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807
Seconds"));
+        Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807
 Seconds"));
+        Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807\tSeconds"));
+        Assert.assertEquals(Timeout.ZERO_MILLISECONDS, Timeout.parse("0 MILLISECONDS"));
+    }
+
 }



Mime
View raw message