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 2017/04/24 19:39:59 UTC
svn commit: r1792538 - in /httpcomponents/httpcore/trunk/httpcore5/src:
main/java/org/apache/hc/core5/util/Timeout.java
test/java/org/apache/hc/core5/util/TestTimeout.java
Author: ggregory
Date: Mon Apr 24 19:39:59 2017
New Revision: 1792538
URL: http://svn.apache.org/viewvc?rev=1792538&view=rev
Log:
[HTTPCORE-454] Add a Timeout class that extends TimeValue. Add missing methods for MICROSECONDS and NANOSECONDS TimeUnits.
Modified:
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/TestTimeout.java
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=1792538&r1=1792537&r2=1792538&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 Apr 24 19:39:59 2017
@@ -48,12 +48,12 @@ public class Timeout extends TimeValue {
/**
* An undefined timeout represented as -1 {@code MILLISECONDS}.
*/
- public static final Timeout UNDEFINED_MILLISECONDS = new Timeout(UNDEFINED, TimeUnit.MILLISECONDS);
+ public static final Timeout UNDEFINED_MILLISECONDS = new Timeout(INT_UNDEFINED, TimeUnit.MILLISECONDS);
/**
* An undefined timeout represented as -1 {@code SECONDS}.
*/
- public static final Timeout UNDEFINED_SECONDS = new Timeout(UNDEFINED, TimeUnit.SECONDS);
+ public static final Timeout UNDEFINED_SECONDS = new Timeout(INT_UNDEFINED, TimeUnit.SECONDS);
/**
* Creates a Timeout.
@@ -93,6 +93,17 @@ public class Timeout extends TimeValue {
/**
* Creates a Timeout.
*
+ * @param microseconds
+ * the duration in seconds and the given {@code timeUnit}.
+ * @return a Timeout
+ */
+ public static Timeout ofMicroseconds(final long microseconds) {
+ return of(microseconds, TimeUnit.MICROSECONDS);
+ }
+
+ /**
+ * Creates a Timeout.
+ *
* @param milliseconds
* the duration in milliseconds and the given {@code timeUnit}.
* @return a Timeout
@@ -115,6 +126,17 @@ public class Timeout extends TimeValue {
/**
* Creates a Timeout.
*
+ * @param nanoseconds
+ * the duration in seconds and the given {@code timeUnit}.
+ * @return a Timeout
+ */
+ public static Timeout ofNanoseconds(final long nanoseconds) {
+ return of(nanoseconds, TimeUnit.NANOSECONDS);
+ }
+
+ /**
+ * Creates a Timeout.
+ *
* @param seconds
* the duration in seconds and the given {@code timeUnit}.
* @return a Timeout
@@ -123,9 +145,10 @@ public class Timeout extends TimeValue {
return of(seconds, TimeUnit.SECONDS);
}
+
private static long validateDuration(final long duration) {
- if (duration < UNDEFINED) {
- throw new IllegalArgumentException("Duration may not be less than " + UNDEFINED);
+ if (duration < INT_UNDEFINED) {
+ throw new IllegalArgumentException("Duration may not be less than " + INT_UNDEFINED);
}
return duration;
}
@@ -158,7 +181,7 @@ public class Timeout extends TimeValue {
* @return Whether this timeout is undefined.
*/
public boolean isUndefinedMilliseconds() {
- return getDuration() == UNDEFINED && getTimeUnit() == TimeUnit.MILLISECONDS;
+ return getDuration() == INT_UNDEFINED && getTimeUnit() == TimeUnit.MILLISECONDS;
}
/**
@@ -167,7 +190,7 @@ public class Timeout extends TimeValue {
* @return Whether this timeout is undefined.
*/
public boolean isUndefinedSeconds() {
- return getDuration() == UNDEFINED && getTimeUnit() == TimeUnit.SECONDS;
+ return getDuration() == INT_UNDEFINED && getTimeUnit() == TimeUnit.SECONDS;
}
}
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=1792538&r1=1792537&r2=1792538&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 Apr 24 19:39:59 2017
@@ -92,6 +92,45 @@ public class TestTimeout {
Assert.assertFalse(Timeout.DISABLED.isUndefinedSeconds());
}
+ private void testFactory(final TimeUnit timeUnit) {
+ Assert.assertEquals(timeUnit, Timeout.of(1, timeUnit).getTimeUnit());
+ }
+
+ @Test
+ 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
public void testMaxInt() {
test(Integer.MAX_VALUE);