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 2019/09/09 13:12:05 UTC

[httpcomponents-core] branch master updated: TimeValue to implement Comparable

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 4b02bc0  TimeValue to implement Comparable
4b02bc0 is described below

commit 4b02bc0208189dfb97fa029a975801160bf2c9c4
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Sep 7 15:01:45 2019 +0200

    TimeValue to implement Comparable
---
 .../java/org/apache/hc/core5/util/TimeValue.java   | 16 ++++----
 .../org/apache/hc/core5/util/TestTimeValue.java    | 45 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 7 deletions(-)

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 0db3ec3..794e135 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
@@ -41,7 +41,7 @@ import org.apache.hc.core5.annotation.ThreadingBehavior;
  * @since 5.0
  */
 @Contract(threading = ThreadingBehavior.IMMUTABLE)
-public class TimeValue {
+public class TimeValue implements Comparable<TimeValue> {
 
     static final int INT_UNDEFINED = -1;
 
@@ -217,6 +217,7 @@ public class TimeValue {
     }
 
     public long convert(final TimeUnit targetTimeUnit) {
+        Args.notNull(targetTimeUnit, "timeUnit");
         return targetTimeUnit.convert(duration, timeUnit);
     }
 
@@ -278,12 +279,7 @@ public class TimeValue {
     }
 
     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);
+        return this.compareTo(other) > 0 ? other : this;
     }
 
     private TimeUnit min(final TimeUnit other) {
@@ -372,6 +368,12 @@ public class TimeValue {
     }
 
     @Override
+    public int compareTo(final TimeValue other) {
+        final TimeUnit targetTimeUnit = min(other.getTimeUnit());
+        return Long.compare(convert(targetTimeUnit), other.convert(targetTimeUnit));
+    }
+
+    @Override
     public String toString() {
         return String.format(Locale.ROOT, "%,d %s", duration, timeUnit);
     }
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 d7fa797..4dcaf14 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
@@ -30,6 +30,7 @@ package org.apache.hc.core5.util;
 import java.text.ParseException;
 import java.util.concurrent.TimeUnit;
 
+import org.hamcrest.CoreMatchers;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -262,4 +263,48 @@ public class TestTimeValue {
         Assert.assertEquals(TimeValue.ofMilliseconds(1), TimeValue.parse("1 MILLISECOND"));
     }
 
+    @Test
+    public void testEqualsAndHashCode() throws ParseException {
+        final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
+        final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
+        final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
+        final TimeValue tv4 = TimeValue.ofSeconds(1L);
+        final TimeValue tv5 = TimeValue.ofSeconds(1000L);
+
+        Assert.assertThat(tv1.equals(tv1), CoreMatchers.equalTo(true));
+        Assert.assertThat(tv1.equals(null), CoreMatchers.equalTo(false));
+        Assert.assertThat(tv1.equals(tv2), CoreMatchers.equalTo(false));
+        Assert.assertThat(tv1.equals(tv3), CoreMatchers.equalTo(true));
+        Assert.assertThat(tv1.equals(tv4), CoreMatchers.equalTo(false));
+        Assert.assertThat(tv1.equals(tv5), CoreMatchers.equalTo(false));
+
+        Assert.assertThat(tv1.hashCode() == tv2.hashCode(), CoreMatchers.equalTo(false));
+        Assert.assertThat(tv1.hashCode() == tv3.hashCode(), CoreMatchers.equalTo(true));
+        Assert.assertThat(tv1.hashCode() == tv4.hashCode(), CoreMatchers.equalTo(false));
+        Assert.assertThat(tv1.hashCode() == tv5.hashCode(), CoreMatchers.equalTo(false));
+    }
+
+    @Test
+    public void testCompareTo() throws ParseException {
+        final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
+        final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
+        final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
+        final TimeValue tv4 = TimeValue.ofSeconds(1L);
+        final TimeValue tv5 = TimeValue.ofSeconds(60L);
+        final TimeValue tv6 = TimeValue.ofMinutes(1L);
+
+        Assert.assertThat(tv1.compareTo(tv1) == 0, CoreMatchers.equalTo(true));
+        Assert.assertThat(tv1.compareTo(tv2) < 0, CoreMatchers.equalTo(true));
+        Assert.assertThat(tv1.compareTo(tv3) == 0, CoreMatchers.equalTo(true));
+        Assert.assertThat(tv1.compareTo(tv4) == 0, CoreMatchers.equalTo(true));
+        Assert.assertThat(tv1.compareTo(tv5) < 0, CoreMatchers.equalTo(true));
+        Assert.assertThat(tv6.compareTo(tv5) == 0, CoreMatchers.equalTo(true));
+        Assert.assertThat(tv6.compareTo(tv4) > 0, CoreMatchers.equalTo(true));
+        try {
+            tv1.compareTo(null);
+            Assert.fail("NullPointerException expected");
+        } catch (final NullPointerException expected) {
+        }
+    }
+
 }