You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2018/10/23 21:01:35 UTC

httpcomponents-core git commit: Add handy methods in TimeValue and fix TimeValue.convert(TimeUnit).

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master b1eb8f6e3 -> 9c3d5d307


Add handy methods in TimeValue and fix TimeValue.convert(TimeUnit).

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

Branch: refs/heads/master
Commit: 9c3d5d307dee25afb82a0a50e2865cc7cb77dcc5
Parents: b1eb8f6
Author: Gary Gregory <ga...@gmail.com>
Authored: Tue Oct 23 15:01:31 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Tue Oct 23 15:01:31 2018 -0600

----------------------------------------------------------------------
 RELEASE_NOTES.txt                               |   6 ++
 .../org/apache/hc/core5/util/TimeValue.java     |  81 +++++++++++++-
 .../org/apache/hc/core5/util/TestTimeValue.java | 105 +++++++++++++++++++
 3 files changed, 189 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/9c3d5d30/RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 14a681c..c4ea795 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -7,6 +7,12 @@ Release 5.0-BETA6
 * Deprecate and rename org.apache.hc.core5.util.TimeValue.ofMillis(long) to ofMilliseconds(long).
   Contributed by Gary Gregory <ggregory at apache.org>
 
+* Fix org.apache.hc.core5.util.TimeValue.convert(TimeUnit).
+  Contributed by Gary Gregory <ggregory at apache.org>
+
+* Add org.apache.hc.core5.util.TimeValue.divide(*).
+  Contributed by Gary Gregory <ggregory at apache.org>
+
 Release 5.0-BETA5
 -------------------
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/9c3d5d30/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 ac8dfcd..f80ad74 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
@@ -36,7 +36,7 @@ import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 
 /**
- * Represents a time value as a {@code long} time and {@link TimeUnit}.
+ * Represents a time value as a {@code long} time and a {@link TimeUnit}.
  *
  * @since 5.0
  */
@@ -207,8 +207,8 @@ public class TimeValue {
         this.timeUnit = Args.notNull(timeUnit, "timeUnit");
     }
 
-    public long convert(final TimeUnit sourceUnit) {
-        return timeUnit.convert(duration, sourceUnit);
+    public long convert(final TimeUnit targetTimeUnit) {
+        return targetTimeUnit.convert(duration, timeUnit);
     }
 
     @Override
@@ -223,6 +223,35 @@ public class TimeValue {
         return false;
     }
 
+    /**
+     * Returns a TimeValue whose value is {@code (this / divisor)}.
+     *
+     * @param divisor
+     *            value by which this TimeValue is to be divided.
+     * @return {@code this / divisor}
+     * @throws ArithmeticException
+     *             if {@code divisor} is zero.
+     */
+    public TimeValue divide(final long divisor) {
+        final long newDuration = duration / divisor;
+        return of(newDuration, timeUnit);
+    }
+
+    /**
+     * Returns a TimeValue whose value is {@code (this / divisor)}.
+     *
+     * @param divisor
+     *            value by which this TimeValue is to be divided.
+     * @param targetTimeUnit
+     *            the target TimeUnit
+     * @return {@code this / divisor}
+     * @throws ArithmeticException
+     *             if {@code divisor} is zero.
+     */
+    public TimeValue divide(final long divisor, final TimeUnit targetTimeUnit) {
+        return of(convert(targetTimeUnit) / divisor, targetTimeUnit);
+    }
+
     public long getDuration() {
         return duration;
     }
@@ -239,6 +268,52 @@ public class TimeValue {
         return hash;
     }
 
+    public TimeValue min(final TimeValue other) {
+        return isGreaterThan(other) ? other : this;
+    }
+
+    private boolean isGreaterThan(final TimeValue other) {
+        final TimeUnit targetTimeUnit = min(other.getTimeUnit());
+        return convert(targetTimeUnit) > other.convert(targetTimeUnit);
+    }
+
+    private TimeUnit min(final TimeUnit other) {
+        return scale() > scale(other) ? other : getTimeUnit();
+    }
+
+    private int scale() {
+        return scale(timeUnit);
+    }
+
+    /**
+     * Returns a made up scale for TimeUnits.
+     *
+     * @param tUnit
+     *            a TimeUnit
+     * @return a number from 1 to 7, where 1 is NANOSECONDS and 7 DAYS.
+     */
+    private int scale(final TimeUnit tUnit) {
+        switch (tUnit) {
+        case NANOSECONDS:
+            return 1;
+        case MICROSECONDS:
+            return 2;
+        case MILLISECONDS:
+            return 3;
+        case SECONDS:
+            return 4;
+        case MINUTES:
+            return 5;
+        case HOURS:
+            return 6;
+        case DAYS:
+            return 7;
+        default:
+            // Should never happens unless Java adds to the enum.
+            throw new IllegalStateException();
+        }
+    }
+
     public void sleep() throws InterruptedException {
         timeUnit.sleep(duration);
     }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/9c3d5d30/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 b1b73b5..6c33980 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
@@ -85,6 +85,36 @@ public class TestTimeValue {
         test(1);
     }
 
+    @Test
+    public void testConvert() {
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).convert(TimeUnit.DAYS));
+        Assert.assertEquals(1000, TimeValue.ofSeconds(1).convert(TimeUnit.MILLISECONDS));
+    }
+
+    @Test
+    public void testDivide() {
+        // nominator is 0, result should be 0.
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toDays());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toHours());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMicros());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMillis());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMinutes());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toNanos());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSeconds());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMillisIntBound());
+        Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSecondsIntBound());
+        //
+        Assert.assertEquals(50, TimeValue.ofMilliseconds(100).divide(2).toMillis());
+        Assert.assertEquals(0, TimeValue.ofMinutes(1).divide(2).toSeconds());
+        Assert.assertEquals(30, TimeValue.ofMinutes(1).divide(2, TimeUnit.SECONDS).toSeconds());
+        Assert.assertEquals(30000, TimeValue.ofMinutes(1).divide(2, TimeUnit.MILLISECONDS).toMillis());
+    }
+
+    @Test(expected = ArithmeticException.class)
+    public void testDivideBy0() {
+        TimeValue.ofMilliseconds(0).divide(0);
+    }
+
     private void testFactory(final TimeUnit timeUnit) {
         Assert.assertEquals(timeUnit, TimeValue.of(1, timeUnit).getTimeUnit());
     }
@@ -125,6 +155,81 @@ public class TestTimeValue {
     }
 
     @Test
+    public void testMin() {
+        final TimeValue nanos1 = TimeValue.ofNanoseconds(1);
+        final TimeValue micros1 = TimeValue.ofMicroseconds(1);
+        final TimeValue millis1 = TimeValue.ofMilliseconds(1);
+        final TimeValue seconds1 = TimeValue.ofSeconds(1);
+        final TimeValue minutes1 = TimeValue.ofMinutes(1);
+        final TimeValue hours1 = TimeValue.ofHours(1);
+        final TimeValue days1 = TimeValue.ofDays(1);
+        //
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(nanos1));
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(micros1));
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(millis1));
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(seconds1));
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(minutes1));
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(hours1));
+        Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(days1));
+        //
+        Assert.assertEquals(nanos1, nanos1.min(nanos1));
+        Assert.assertEquals(nanos1, nanos1.min(micros1));
+        Assert.assertEquals(nanos1, nanos1.min(millis1));
+        Assert.assertEquals(nanos1, nanos1.min(seconds1));
+        Assert.assertEquals(nanos1, nanos1.min(minutes1));
+        Assert.assertEquals(nanos1, nanos1.min(hours1));
+        Assert.assertEquals(nanos1, nanos1.min(days1));
+        //
+        Assert.assertEquals(nanos1, micros1.min(nanos1));
+        Assert.assertEquals(micros1, micros1.min(micros1));
+        Assert.assertEquals(micros1, micros1.min(millis1));
+        Assert.assertEquals(micros1, micros1.min(seconds1));
+        Assert.assertEquals(micros1, micros1.min(minutes1));
+        Assert.assertEquals(micros1, micros1.min(hours1));
+        Assert.assertEquals(micros1, micros1.min(days1));
+        //
+        Assert.assertEquals(nanos1, millis1.min(nanos1));
+        Assert.assertEquals(micros1, millis1.min(micros1));
+        Assert.assertEquals(millis1, millis1.min(millis1));
+        Assert.assertEquals(millis1, millis1.min(seconds1));
+        Assert.assertEquals(millis1, millis1.min(minutes1));
+        Assert.assertEquals(millis1, millis1.min(hours1));
+        Assert.assertEquals(millis1, millis1.min(days1));
+        //
+        Assert.assertEquals(nanos1, seconds1.min(nanos1));
+        Assert.assertEquals(micros1, seconds1.min(micros1));
+        Assert.assertEquals(millis1, seconds1.min(millis1));
+        Assert.assertEquals(seconds1, seconds1.min(seconds1));
+        Assert.assertEquals(seconds1, seconds1.min(minutes1));
+        Assert.assertEquals(seconds1, seconds1.min(hours1));
+        Assert.assertEquals(seconds1, seconds1.min(days1));
+        //
+        Assert.assertEquals(nanos1, minutes1.min(nanos1));
+        Assert.assertEquals(micros1, minutes1.min(micros1));
+        Assert.assertEquals(millis1, minutes1.min(millis1));
+        Assert.assertEquals(seconds1, minutes1.min(seconds1));
+        Assert.assertEquals(minutes1, minutes1.min(minutes1));
+        Assert.assertEquals(minutes1, minutes1.min(hours1));
+        Assert.assertEquals(minutes1, minutes1.min(days1));
+        //
+        Assert.assertEquals(nanos1, hours1.min(nanos1));
+        Assert.assertEquals(micros1, hours1.min(micros1));
+        Assert.assertEquals(millis1, hours1.min(millis1));
+        Assert.assertEquals(seconds1, hours1.min(seconds1));
+        Assert.assertEquals(minutes1, hours1.min(minutes1));
+        Assert.assertEquals(hours1, hours1.min(hours1));
+        Assert.assertEquals(hours1, hours1.min(days1));
+        //
+        Assert.assertEquals(nanos1, days1.min(nanos1));
+        Assert.assertEquals(micros1, days1.min(micros1));
+        Assert.assertEquals(millis1, days1.min(millis1));
+        Assert.assertEquals(seconds1, days1.min(seconds1));
+        Assert.assertEquals(minutes1, days1.min(minutes1));
+        Assert.assertEquals(hours1, days1.min(hours1));
+        Assert.assertEquals(days1, days1.min(days1));
+    }
+
+    @Test
     public void testMaxInt() {
         test(Integer.MAX_VALUE);
     }