hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1792537 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/util/TimeValue.java test/java/org/apache/hc/core5/util/TestTimeValue.java
Date Mon, 24 Apr 2017 19:39:31 GMT
Author: ggregory
Date: Mon Apr 24 19:39:31 2017
New Revision: 1792537

URL: http://svn.apache.org/viewvc?rev=1792537&view=rev
Log:
HTTPCORE-451: Add a TimeValue class to wrap a long and a TimeUnit. Add missing methods for
MICROSECONDS and NANOSECONDS TimeUnits.

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.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=1792537&r1=1792536&r2=1792537&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 Apr 24 19:39:31 2017
@@ -40,9 +40,9 @@ import org.apache.hc.core5.annotation.Th
 @Contract(threading = ThreadingBehavior.IMMUTABLE)
 public class TimeValue {
 
-    static final int UNDEFINED = -1;
-    public static final TimeValue NEG_ONE_MILLISECONDS = TimeValue.of(UNDEFINED, TimeUnit.MILLISECONDS);
-    public static final TimeValue NEG_ONE_SECONDS = TimeValue.of(UNDEFINED, TimeUnit.SECONDS);
+    static final int INT_UNDEFINED = -1;
+    public static final TimeValue NEG_ONE_MILLISECONDS = TimeValue.of(INT_UNDEFINED, TimeUnit.MILLISECONDS);
+    public static final TimeValue NEG_ONE_SECONDS = TimeValue.of(INT_UNDEFINED, TimeUnit.SECONDS);
     public static final TimeValue ZERO_MILLISECONDS = TimeValue.of(0, TimeUnit.MILLISECONDS);
 
     /**
@@ -81,17 +81,6 @@ public class TimeValue {
     }
 
     /**
-     * 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}
-     * @return {@code timeValue} or {@link #ZERO_MILLISECONDS}
-     */
-    public static TimeValue defaultsToZeroMillis(final TimeValue timeValue) {
-        return defaultsTo(timeValue, ZERO_MILLISECONDS);
-    }
-
-    /**
      * Returns the given {@code timeValue} if it is not {@code null}, if {@code null} then
returns
      * {@link #NEG_ONE_SECONDS}.
      *
@@ -116,6 +105,25 @@ public class TimeValue {
     }
 
     /**
+     * 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}
+     * @return {@code timeValue} or {@link #ZERO_MILLISECONDS}
+     */
+    public static TimeValue defaultsToZeroMillis(final TimeValue timeValue) {
+        return defaultsTo(timeValue, ZERO_MILLISECONDS);
+    }
+
+    public static boolean isNonNegative(final TimeValue timeValue) {
+        return timeValue != null && timeValue.getDuration() >= 0;
+    }
+
+    public static boolean isPositive(final TimeValue timeValue) {
+        return timeValue != null && timeValue.getDuration() > 0;
+    }
+
+    /**
      * Creates a TimeValue.
      *
      * @param duration
@@ -136,6 +144,10 @@ public class TimeValue {
         return of(hours, TimeUnit.HOURS);
     }
 
+    public static TimeValue ofMicroseconds(final long microseconds) {
+        return of(microseconds, TimeUnit.MICROSECONDS);
+    }
+
     public static TimeValue ofMillis(final long millis) {
         return of(millis, TimeUnit.MILLISECONDS);
     }
@@ -144,16 +156,12 @@ public class TimeValue {
         return of(minutes, TimeUnit.MINUTES);
     }
 
-    public static TimeValue ofSeconds(final long seconds) {
-        return of(seconds, TimeUnit.SECONDS);
-    }
-
-    public static boolean isPositive(final TimeValue timeValue) {
-        return timeValue != null && timeValue.getDuration() > 0;
+    public static TimeValue ofNanoseconds(final long nanoseconds) {
+        return of(nanoseconds, TimeUnit.NANOSECONDS);
     }
 
-    public static boolean isNonNegative(final TimeValue timeValue) {
-        return timeValue != null && timeValue.getDuration() >= 0;
+    public static TimeValue ofSeconds(final long seconds) {
+        return of(seconds, TimeUnit.SECONDS);
     }
 
     private final long duration;
@@ -170,6 +178,19 @@ public class TimeValue {
         return timeUnit.convert(duration, sourceUnit);
     }
 
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof TimeValue) {
+            final TimeValue that = (TimeValue) obj;
+            return this.duration == that.duration &&
+                    LangUtils.equals(this.timeUnit, that.timeUnit);
+        }
+        return false;
+    }
+
     public long getDuration() {
         return duration;
     }
@@ -178,6 +199,14 @@ public class TimeValue {
         return timeUnit;
     }
 
+    @Override
+    public int hashCode() {
+        int hash = LangUtils.HASH_SEED;
+        hash = LangUtils.hashCode(hash, Long.valueOf(duration));
+        hash = LangUtils.hashCode(hash, timeUnit);
+        return hash;
+    }
+
     public void sleep() throws InterruptedException {
         timeUnit.sleep(duration);
     }
@@ -227,27 +256,6 @@ public class TimeValue {
     }
 
     @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj instanceof TimeValue) {
-            final TimeValue that = (TimeValue) obj;
-            return this.duration == that.duration &&
-                    LangUtils.equals(this.timeUnit, that.timeUnit);
-        }
-        return false;
-    }
-
-    @Override
-    public int hashCode() {
-        int hash = LangUtils.HASH_SEED;
-        hash = LangUtils.hashCode(hash, Long.valueOf(duration));
-        hash = LangUtils.hashCode(hash, timeUnit);
-        return hash;
-    }
-
-    @Override
     public String toString() {
         return String.format("%,d %s", Long.valueOf(duration), timeUnit);
     }

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=1792537&r1=1792536&r2=1792537&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 Apr 24 19:39:31 2017
@@ -34,10 +34,6 @@ import org.junit.Test;
 
 public class TestTimeValue {
 
-    private void checkToSeconds(final long value, final TimeUnit timeUnit) {
-        Assert.assertEquals(timeUnit.toSeconds(value), TimeValue.of(value, timeUnit).toSeconds());
-    }
-
     private void checkToDays(final long value, final TimeUnit timeUnit) {
         Assert.assertEquals(timeUnit.toDays(value), TimeValue.of(value, timeUnit).toDays());
     }
@@ -46,22 +42,38 @@ public class TestTimeValue {
         Assert.assertEquals(timeUnit.toHours(value), TimeValue.of(value, timeUnit).toHours());
     }
 
-    private void checkToMinutes(final long value, final TimeUnit timeUnit) {
-        Assert.assertEquals(timeUnit.toMinutes(value), TimeValue.of(value, timeUnit).toMinutes());
+    private void checkToMicros(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toMicros(value), TimeValue.of(value, timeUnit).toMicros());
     }
 
     private void checkToMillis(final long value, final TimeUnit timeUnit) {
         Assert.assertEquals(timeUnit.toMillis(value), TimeValue.of(value, timeUnit).toMillis());
     }
 
-    private void checkToMicros(final long value, final TimeUnit timeUnit) {
-        Assert.assertEquals(timeUnit.toMicros(value), TimeValue.of(value, timeUnit).toMicros());
+    private void checkToMinutes(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toMinutes(value), TimeValue.of(value, timeUnit).toMinutes());
     }
 
     private void checkToNanos(final long value, final TimeUnit timeUnit) {
         Assert.assertEquals(timeUnit.toNanos(value), TimeValue.of(value, timeUnit).toNanos());
     }
 
+    private void checkToSeconds(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toSeconds(value), TimeValue.of(value, timeUnit).toSeconds());
+    }
+
+    private void test(final long value) {
+        for (final TimeUnit timeUnit : TimeUnit.values()) {
+            checkToDays(value, timeUnit);
+            checkToHours(value, timeUnit);
+            checkToMinutes(value, timeUnit);
+            checkToSeconds(value, timeUnit);
+            checkToMillis(value, timeUnit);
+            checkToMicros(value, timeUnit);
+            checkToNanos(value, timeUnit);
+        }
+    }
+
     @Test
     public void test0() {
         test(0);
@@ -72,9 +84,43 @@ public class TestTimeValue {
         test(1);
     }
 
+    private void testFactory(final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit, TimeValue.of(1, timeUnit).getTimeUnit());
+    }
+
     @Test
-    public void testNegative1() {
-        test(-1);
+    public void testFactoryForDays() {
+        testFactory(TimeUnit.DAYS);
+    }
+
+    @Test
+    public void testFactoryForHours() {
+        testFactory(TimeUnit.HOURS);
+    }
+
+    @Test
+    public void testFactoryForMicroseconds() {
+        testFactory(TimeUnit.MICROSECONDS);
+    }
+
+    @Test
+    public void testFactoryForMillisseconds() {
+        testFactory(TimeUnit.MILLISECONDS);
+    }
+
+    @Test
+    public void testFactoryForMinutes() {
+        testFactory(TimeUnit.MINUTES);
+    }
+
+    @Test
+    public void testFactoryForNanoseconds() {
+        testFactory(TimeUnit.NANOSECONDS);
+    }
+
+    @Test
+    public void testFactoryForSeconds() {
+        testFactory(TimeUnit.SECONDS);
     }
 
     @Test
@@ -87,16 +133,9 @@ public class TestTimeValue {
         test(Long.MAX_VALUE);
     }
 
-    private void test(final long value) {
-        for (final TimeUnit timeUnit : TimeUnit.values()) {
-            checkToDays(value, timeUnit);
-            checkToHours(value, timeUnit);
-            checkToMinutes(value, timeUnit);
-            checkToSeconds(value, timeUnit);
-            checkToMillis(value, timeUnit);
-            checkToMicros(value, timeUnit);
-            checkToNanos(value, timeUnit);
-        }
+    @Test
+    public void testNegative1() {
+        test(-1);
     }
 
 }



Mime
View raw message