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.time.Duration;
32  import java.util.concurrent.TimeUnit;
33  
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  
37  /**
38   * Represents a timeout value as a non-negative {@code long} time and {@link TimeUnit}.
39   *
40   * @since 5.0
41   */
42  @Contract(threading = ThreadingBehavior.IMMUTABLE)
43  public class Timeout extends TimeValue {
44  
45      /**
46       * A zero milliseconds {@link Timeout}.
47       */
48      public static final Timeout ZERO_MILLISECONDS = Timeout.of(0, TimeUnit.MILLISECONDS);
49  
50      /**
51       * A one milliseconds {@link Timeout}.
52       */
53      public static final Timeout ONE_MILLISECOND = Timeout.of(1, TimeUnit.MILLISECONDS);
54  
55      /**
56       * A disabled timeout represented as 0 {@code MILLISECONDS}.
57       */
58      public static final Timeout DISABLED = ZERO_MILLISECONDS;
59  
60      /**
61       * Returns the given {@code timeout} if it is not {@code null}, if {@code null} then returns {@link #DISABLED}.
62       *
63       * @param timeout may be {@code null}
64       * @return {@code timeValue} or {@link #DISABLED}
65       */
66      public static Timeout defaultsToDisabled(final Timeout timeout) {
67          return defaultsTo(timeout, DISABLED);
68      }
69  
70      /**
71       * Creates a Timeout from a Duration.
72       *
73       * @param duration the time duration.
74       * @return a Timeout.
75       * @since 5.2
76       */
77      public static Timeout of(final Duration duration) {
78          final long seconds = duration.getSeconds();
79          final long nanoOfSecond = duration.getNano();
80          if (seconds == 0) {
81              // no conversion
82              return of(nanoOfSecond, TimeUnit.NANOSECONDS);
83          } else if (nanoOfSecond == 0) {
84              // no conversion
85              return of(seconds, TimeUnit.SECONDS);
86          }
87          // conversion attempts
88          try {
89              return of(duration.toNanos(), TimeUnit.NANOSECONDS);
90          } catch (final ArithmeticException e) {
91              try {
92                  return of(duration.toMillis(), TimeUnit.MILLISECONDS);
93              } catch (final ArithmeticException e1) {
94                  // backstop
95                  return of(seconds, TimeUnit.SECONDS);
96              }
97          }
98      }
99  
100     /**
101      * Creates a Timeout.
102      *
103      * @param duration the time duration.
104      * @param timeUnit the time unit for the given duration.
105      * @return a Timeout
106      */
107     public static Timeout of(final long duration, final TimeUnit timeUnit) {
108         return new Timeout(duration, timeUnit);
109     }
110 
111     /**
112      * Creates a Timeout.
113      *
114      * @param days the duration in days and the given {@code timeUnit}.
115      * @return a Timeout
116      */
117     public static Timeout ofDays(final long days) {
118         return of(days, TimeUnit.DAYS);
119     }
120 
121     /**
122      * Creates a Timeout.
123      *
124      * @param hours the duration in hours and the given {@code timeUnit}.
125      * @return a Timeout
126      */
127     public static Timeout ofHours(final long hours) {
128         return of(hours, TimeUnit.HOURS);
129     }
130 
131     /**
132      * Creates a Timeout.
133      *
134      * @param microseconds the duration in seconds and the given {@code timeUnit}.
135      * @return a Timeout
136      */
137     public static Timeout ofMicroseconds(final long microseconds) {
138         return of(microseconds, TimeUnit.MICROSECONDS);
139     }
140 
141     /**
142      * Creates a Timeout.
143      *
144      * @param milliseconds the duration in milliseconds and the given {@code timeUnit}.
145      * @return a Timeout
146      */
147     public static Timeout ofMilliseconds(final long milliseconds) {
148         return of(milliseconds, TimeUnit.MILLISECONDS);
149     }
150 
151     /**
152      * Creates a Timeout.
153      *
154      * @param minutes the duration in minutes and the given {@code timeUnit}.
155      * @return a Timeout
156      */
157     public static Timeout ofMinutes(final long minutes) {
158         return of(minutes, TimeUnit.MINUTES);
159     }
160 
161     /**
162      * Creates a Timeout.
163      *
164      * @param nanoseconds the duration in seconds and the given {@code timeUnit}.
165      * @return a Timeout
166      */
167     public static Timeout ofNanoseconds(final long nanoseconds) {
168         return of(nanoseconds, TimeUnit.NANOSECONDS);
169     }
170 
171     /**
172      * Creates a Timeout.
173      *
174      * @param seconds the duration in seconds and the given {@code timeUnit}.
175      * @return a Timeout
176      */
177     public static Timeout ofSeconds(final long seconds) {
178         return of(seconds, TimeUnit.SECONDS);
179     }
180 
181     /**
182      * Parses a Timeout in the format {@code <Integer><SPACE><TimeUnit>}, for example {@code "1,200 MILLISECONDS"}
183      *
184      * @param value the TimeValue to parse
185      * @return a new TimeValue
186      * @throws ParseException if the number cannot be parsed
187      */
188     public static Timeout parse(final String value) throws ParseException {
189         return TimeValue.parse(value).toTimeout();
190     }
191 
192     Timeout(final long duration, final TimeUnit timeUnit) {
193         super(Args.notNegative(duration, "duration"), Args.notNull(timeUnit, "timeUnit"));
194     }
195 
196     /**
197      * Whether this timeout is disabled.
198      *
199      * @return Whether this timeout is disabled.
200      */
201     public boolean isDisabled() {
202         return getDuration() == 0;
203     }
204 
205     /**
206      * Whether this timeout is enabled.
207      *
208      * @return Whether this timeout is disabled.
209      */
210     public boolean isEnabled() {
211         return !isDisabled();
212     }
213 
214 }