Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id AB16C200C6A for ; Wed, 19 Apr 2017 23:48:41 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A9995160B9C; Wed, 19 Apr 2017 21:48:41 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A47B2160B94 for ; Wed, 19 Apr 2017 23:48:40 +0200 (CEST) Received: (qmail 39615 invoked by uid 500); 19 Apr 2017 21:48:39 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 39606 invoked by uid 99); 19 Apr 2017 21:48:39 -0000 Received: from Unknown (HELO svn01-us-west.apache.org) (209.188.14.144) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 19 Apr 2017 21:48:39 +0000 Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 2B7C83A002A for ; Wed, 19 Apr 2017 21:48:39 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1791989 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/util/TimeValue.java main/java/org/apache/hc/core5/util/Timeout.java test/java/org/apache/hc/core5/util/TestTimeout.java Date: Wed, 19 Apr 2017 21:48:38 -0000 To: commits@hc.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20170419214839.2B7C83A002A@svn01-us-west.apache.org> archived-at: Wed, 19 Apr 2017 21:48:41 -0000 Author: ggregory Date: Wed Apr 19 21:48:38 2017 New Revision: 1791989 URL: http://svn.apache.org/viewvc?rev=1791989&view=rev Log: [HTTPCORE-454] Add a Timeout class that extends TimeValue. First cut. Not used yet. Unit test is there though. Might need some refinements. Added: 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/TimeValue.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=1791989&r1=1791988&r2=1791989&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 Wed Apr 19 21:48:38 2017 @@ -40,7 +40,7 @@ import org.apache.hc.core5.annotation.Th @Contract(threading = ThreadingBehavior.IMMUTABLE) public class TimeValue { - private static final int UNDEFINED = -1; + 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); public static final TimeValue ZERO_MILLISECONDS = TimeValue.of(0, TimeUnit.MILLISECONDS); @@ -115,6 +115,15 @@ public class TimeValue { return defaultsTo(timeValue, NEG_ONE_SECONDS); } + /** + * Creates a TimeValue. + * + * @param duration + * the time duration in the given {@code timeUnit}. + * @param timeUnit + * the time unit for the given durarion. + * @return a Timeout + */ public static TimeValue of(final long duration, final TimeUnit timeUnit) { return new TimeValue(duration, timeUnit); } @@ -233,7 +242,7 @@ public class TimeValue { @Override public int hashCode() { int hash = LangUtils.HASH_SEED; - hash = LangUtils.hashCode(hash, duration); + hash = LangUtils.hashCode(hash, Long.valueOf(duration)); hash = LangUtils.hashCode(hash, timeUnit); return hash; } Added: 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=1791989&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java (added) +++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java Wed Apr 19 21:48:38 2017 @@ -0,0 +1,173 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.hc.core5.util; + +import java.util.concurrent.TimeUnit; + +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.ThreadingBehavior; + +/** + * Represents a timeout value as a {@code long} time and {@link TimeUnit}. + * + * @since 5.0 + */ +@Contract(threading = ThreadingBehavior.IMMUTABLE) +public class Timeout extends TimeValue { + + /** + * A disabled timeout represented as 0 {@code MILLISECONDS}. + */ + public static final Timeout DISABLED = new Timeout(0, TimeUnit.MILLISECONDS); + + /** + * An undefined timeout represented as -1 {@code MILLISECONDS}. + */ + public static final Timeout UNDEFINED_MILLISECONDS = new Timeout(UNDEFINED, TimeUnit.MILLISECONDS); + + /** + * An undefined timeout represented as -1 {@code SECONDS}. + */ + public static final Timeout UNDEFINED_SECONDS = new Timeout(UNDEFINED, TimeUnit.SECONDS); + + /** + * Creates a Timeout. + * + * @param duration + * the time duration in the given {@code timeUnit}. + * @param timeUnit + * the time unit for the given durarion. + * @return a Timeout + */ + public static Timeout of(final long duration, final TimeUnit timeUnit) { + return new Timeout(duration, timeUnit); + } + + /** + * Creates a Timeout. + * + * @param days + * the duration in days and the given {@code timeUnit}. + * @return a Timeout + */ + public static Timeout ofDays(final long days) { + return of(days, TimeUnit.DAYS); + } + + /** + * Creates a Timeout. + * + * @param hours + * the duration in hours and the given {@code timeUnit}. + * @return a Timeout + */ + public static Timeout ofHours(final long hours) { + return of(hours, TimeUnit.HOURS); + } + + /** + * Creates a Timeout. + * + * @param milliseconds + * the duration in milliseconds and the given {@code timeUnit}. + * @return a Timeout + */ + public static Timeout ofMillis(final long milliseconds) { + return of(milliseconds, TimeUnit.MILLISECONDS); + } + + /** + * Creates a Timeout. + * + * @param minutes + * the duration in minutes and the given {@code timeUnit}. + * @return a Timeout + */ + public static Timeout ofMinutes(final long minutes) { + return of(minutes, TimeUnit.MINUTES); + } + + /** + * Creates a Timeout. + * + * @param seconds + * the duration in seconds and the given {@code timeUnit}. + * @return a Timeout + */ + public static Timeout ofSeconds(final long seconds) { + 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); + } + return duration; + } + + Timeout(final long duration, final TimeUnit timeUnit) { + super(validateDuration(duration), Args.notNull(timeUnit, "timeUnit")); + } + + /** + * Whether this timeout is disabled. + * + * @return Whether this timeout is disabled. + */ + public boolean isDisabled() { + return getDuration() == 0; + } + + /** + * Whether this timeout is enabled. + * + * @return Whether this timeout is disabled. + */ + public boolean isEnabled() { + return !isDisabled() && !(isUndefinedMilliseconds() || isUndefinedSeconds()); + } + + /** + * Whether this timeout is undefined. + * + * @return Whether this timeout is undefined. + */ + public boolean isUndefinedMilliseconds() { + return getDuration() == UNDEFINED && getTimeUnit() == TimeUnit.MILLISECONDS; + } + + /** + * Whether this timeout is undefined. + * + * @return Whether this timeout is undefined. + */ + public boolean isUndefinedSeconds() { + return getDuration() == UNDEFINED && getTimeUnit() == TimeUnit.SECONDS; + } + +} Added: 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=1791989&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java (added) +++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java Wed Apr 19 21:48:38 2017 @@ -0,0 +1,129 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.hc.core5.util; + +import java.util.concurrent.TimeUnit; + +import org.junit.Assert; +import org.junit.Test; + +public class TestTimeout { + + private void checkToDays(final long value, final TimeUnit timeUnit) { + Assert.assertEquals(timeUnit.toDays(value), Timeout.of(value, timeUnit).toDays()); + } + + private void checkToHours(final long value, final TimeUnit timeUnit) { + Assert.assertEquals(timeUnit.toHours(value), Timeout.of(value, timeUnit).toHours()); + } + + private void checkToMicros(final long value, final TimeUnit timeUnit) { + Assert.assertEquals(timeUnit.toMicros(value), Timeout.of(value, timeUnit).toMicros()); + } + + private void checkToMillis(final long value, final TimeUnit timeUnit) { + Assert.assertEquals(timeUnit.toMillis(value), Timeout.of(value, timeUnit).toMillis()); + } + + private void checkToMinutes(final long value, final TimeUnit timeUnit) { + Assert.assertEquals(timeUnit.toMinutes(value), Timeout.of(value, timeUnit).toMinutes()); + } + + private void checkToNanos(final long value, final TimeUnit timeUnit) { + Assert.assertEquals(timeUnit.toNanos(value), Timeout.of(value, timeUnit).toNanos()); + } + + private void checkToSeconds(final long value, final TimeUnit timeUnit) { + Assert.assertEquals(timeUnit.toSeconds(value), Timeout.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); + } + + @Test + public void test1() { + test(1); + } + + @Test + public void testDisabled() { + Assert.assertTrue(Timeout.DISABLED.isDisabled()); + Assert.assertFalse(Timeout.DISABLED.isEnabled()); + Assert.assertFalse(Timeout.DISABLED.isUndefinedMilliseconds()); + Assert.assertFalse(Timeout.DISABLED.isUndefinedSeconds()); + } + + @Test + public void testMaxInt() { + test(Integer.MAX_VALUE); + } + + @Test + public void testMaxLong() { + test(Long.MAX_VALUE); + } + + @Test + public void testNegative1() { + test(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testNegative2() { + test(-2); + } + + @Test + public void testUndefinedMilliseconds() { + Assert.assertTrue(Timeout.UNDEFINED_MILLISECONDS.isUndefinedMilliseconds()); + Assert.assertFalse(Timeout.UNDEFINED_MILLISECONDS.isEnabled()); + Assert.assertFalse(Timeout.UNDEFINED_MILLISECONDS.isDisabled()); + } + + @Test + public void testUndefinedSeconds() { + Assert.assertTrue(Timeout.UNDEFINED_SECONDS.isUndefinedSeconds()); + Assert.assertFalse(Timeout.UNDEFINED_SECONDS.isEnabled()); + Assert.assertFalse(Timeout.UNDEFINED_SECONDS.isDisabled()); + } + +}