View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.util;
29  
30  import java.text.ParseException;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.ThreadingBehavior;
35  
36  /**
37   * Represents a timeout value as a non-negative {@code long} time and {@link TimeUnit}.
38   *
39   * @since 5.0
40   */
41  @Contract(threading = ThreadingBehavior.IMMUTABLE)
42  public class Timeout extends TimeValue {
43  
44      /**
45       * A zero milliseconds {@link Timeout}.
46       */
47      public static final Timeout ZERO_MILLISECONDS = Timeout.of(0, TimeUnit.MILLISECONDS);
48  
49      /**
50       * A disabled timeout represented as 0 {@code MILLISECONDS}.
51       */
52      public static final Timeout DISABLED = ZERO_MILLISECONDS;
53  
54      /**
55       * Returns the given {@code timeout} if it is not {@code null}, if {@code null} then returns
56       * {@link #DISABLED}.
57       *
58       * @param timeout
59       *            may be {@code null}
60       * @return {@code timeValue} or {@link #DISABLED}
61       */
62      public static Timeout defaultsToDisabled(final Timeout timeout) {
63          return defaultsTo(timeout, DISABLED);
64      }
65  
66      /**
67       * Creates a Timeout.
68       *
69       * @param duration
70       *            the time duration in the given {@code timeUnit}.
71       * @param timeUnit
72       *            the time unit for the given durarion.
73       * @return a Timeout
74       */
75      public static Timeout of(final long duration, final TimeUnit timeUnit) {
76          return new Timeout(duration, timeUnit);
77      }
78  
79      /**
80       * Creates a Timeout.
81       *
82       * @param days
83       *            the duration in days and the given {@code timeUnit}.
84       * @return a Timeout
85       */
86      public static Timeout ofDays(final long days) {
87          return of(days, TimeUnit.DAYS);
88      }
89  
90      /**
91       * Creates a Timeout.
92       *
93       * @param hours
94       *            the duration in hours and the given {@code timeUnit}.
95       * @return a Timeout
96       */
97      public static Timeout ofHours(final long hours) {
98          return of(hours, TimeUnit.HOURS);
99      }
100 
101     /**
102      * Creates a Timeout.
103      *
104      * @param microseconds
105      *            the duration in seconds and the given {@code timeUnit}.
106      * @return a Timeout
107      */
108     public static Timeout ofMicroseconds(final long microseconds) {
109         return of(microseconds, TimeUnit.MICROSECONDS);
110     }
111 
112     /**
113      * Creates a Timeout.
114      *
115      * @param milliseconds
116      *            the duration in milliseconds and the given {@code timeUnit}.
117      * @return a Timeout
118      */
119     public static Timeout ofMillis(final long milliseconds) {
120         return of(milliseconds, TimeUnit.MILLISECONDS);
121     }
122 
123     /**
124      * Creates a Timeout.
125      *
126      * @param minutes
127      *            the duration in minutes and the given {@code timeUnit}.
128      * @return a Timeout
129      */
130     public static Timeout ofMinutes(final long minutes) {
131         return of(minutes, TimeUnit.MINUTES);
132     }
133 
134     /**
135      * Creates a Timeout.
136      *
137      * @param nanoseconds
138      *            the duration in seconds and the given {@code timeUnit}.
139      * @return a Timeout
140      */
141     public static Timeout ofNanoseconds(final long nanoseconds) {
142         return of(nanoseconds, TimeUnit.NANOSECONDS);
143     }
144 
145     /**
146      * Creates a Timeout.
147      *
148      * @param seconds
149      *            the duration in seconds and the given {@code timeUnit}.
150      * @return a Timeout
151      */
152     public static Timeout ofSeconds(final long seconds) {
153         return of(seconds, TimeUnit.SECONDS);
154     }
155 
156     /**
157      * Parses a Timeout in the format {@code <Integer><SPACE><TimeUnit>}, for example {@code "1,200 MILLISECONDS"}
158      *
159      * @param value
160      *            the TimeValue to parse
161      * @return a new TimeValue
162      * @throws ParseException
163      *             if the number cannot be parsed
164      */
165     public static Timeout parse(final String value) throws ParseException {
166         return TimeValue.parse(value).toTimeout();
167     }
168 
169     Timeout(final long duration, final TimeUnit timeUnit) {
170         super(Args.notNegative(duration, "Duration"), Args.notNull(timeUnit, "timeUnit"));
171     }
172 
173     /**
174      * Whether this timeout is disabled.
175      *
176      * @return Whether this timeout is disabled.
177      */
178     public boolean isDisabled() {
179         return getDuration() == 0;
180     }
181 
182     /**
183      * Whether this timeout is enabled.
184      *
185      * @return Whether this timeout is disabled.
186      */
187     public boolean isEnabled() {
188         return !isDisabled();
189     }
190 
191 }