You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/05/09 19:58:34 UTC

[42/50] [abbrv] httpcomponents-core git commit: 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.

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.

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk@1793400 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/aa9928dc
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/aa9928dc
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/aa9928dc

Branch: refs/heads/trunk
Commit: aa9928dcddcf32c947c0e0ed2e960a9f9bd9aec9
Parents: 3ee0f67
Author: Gary D. Gregory <gg...@apache.org>
Authored: Mon May 1 19:13:18 2017 +0000
Committer: Gary D. Gregory <gg...@apache.org>
Committed: Mon May 1 19:13:18 2017 +0000

----------------------------------------------------------------------
 .../org/apache/hc/core5/util/TimeValue.java     | 32 ++++++++++++++++++--
 .../java/org/apache/hc/core5/util/Timeout.java  | 19 +++++++++++-
 .../org/apache/hc/core5/util/TestTimeValue.java | 10 ++++++
 .../org/apache/hc/core5/util/TestTimeout.java   | 16 ++++++++++
 4 files changed, 73 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aa9928dc/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java b/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
index d0ef665..877a92b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
@@ -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);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aa9928dc/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java b/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
index 4cb3c38..5b1f3b8 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java
@@ -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.ThreadingBehavior;
 @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);

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aa9928dc/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
index 21200bf..7b08112 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
@@ -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"));
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aa9928dc/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java
index 0fc3740..9dec674 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java
@@ -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"));
+    }
+
 }