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 {@link #DISABLED}.
56       *
57       * @param timeout may be {@code null}
58       * @return {@code timeValue} or {@link #DISABLED}
59       */
60      public static TimeoutTimeout">Timeout defaultsToDisabled(final Timeout timeout) {
61          return defaultsTo(timeout, DISABLED);
62      }
63  
64      /**
65       * Creates a Timeout.
66       *
67       * @param duration the time duration in the given {@code timeUnit}.
68       * @param timeUnit the time unit for the given duration.
69       * @return a Timeout
70       */
71      public static Timeout of(final long duration, final TimeUnit timeUnit) {
72          return new Timeout(duration, timeUnit);
73      }
74  
75      /**
76       * Creates a Timeout.
77       *
78       * @param days the duration in days and the given {@code timeUnit}.
79       * @return a Timeout
80       */
81      public static Timeout ofDays(final long days) {
82          return of(days, TimeUnit.DAYS);
83      }
84  
85      /**
86       * Creates a Timeout.
87       *
88       * @param hours the duration in hours and the given {@code timeUnit}.
89       * @return a Timeout
90       */
91      public static Timeout ofHours(final long hours) {
92          return of(hours, TimeUnit.HOURS);
93      }
94  
95      /**
96       * Creates a Timeout.
97       *
98       * @param microseconds the duration in seconds and the given {@code timeUnit}.
99       * @return a Timeout
100      */
101     public static Timeout ofMicroseconds(final long microseconds) {
102         return of(microseconds, TimeUnit.MICROSECONDS);
103     }
104 
105     /**
106      * Creates a Timeout.
107      *
108      * @param milliseconds the duration in milliseconds and the given {@code timeUnit}.
109      * @return a Timeout
110      */
111     public static Timeout ofMilliseconds(final long milliseconds) {
112         return of(milliseconds, TimeUnit.MILLISECONDS);
113     }
114 
115     /**
116      * Creates a Timeout.
117      *
118      * @param minutes the duration in minutes and the given {@code timeUnit}.
119      * @return a Timeout
120      */
121     public static Timeout ofMinutes(final long minutes) {
122         return of(minutes, TimeUnit.MINUTES);
123     }
124 
125     /**
126      * Creates a Timeout.
127      *
128      * @param nanoseconds the duration in seconds and the given {@code timeUnit}.
129      * @return a Timeout
130      */
131     public static Timeout ofNanoseconds(final long nanoseconds) {
132         return of(nanoseconds, TimeUnit.NANOSECONDS);
133     }
134 
135     /**
136      * Creates a Timeout.
137      *
138      * @param seconds the duration in seconds and the given {@code timeUnit}.
139      * @return a Timeout
140      */
141     public static Timeout ofSeconds(final long seconds) {
142         return of(seconds, TimeUnit.SECONDS);
143     }
144 
145     /**
146      * Parses a Timeout in the format {@code <Integer><SPACE><TimeUnit>}, for example {@code "1,200 MILLISECONDS"}
147      *
148      * @param value the TimeValue to parse
149      * @return a new TimeValue
150      * @throws ParseException if the number cannot be parsed
151      */
152     public static Timeout parse(final String value) throws ParseException {
153         return TimeValue.parse(value).toTimeout();
154     }
155 
156     Timeout(final long duration, final TimeUnit timeUnit) {
157         super(Args.notNegative(duration, "duration"), Args.notNull(timeUnit, "timeUnit"));
158     }
159 
160     /**
161      * Whether this timeout is disabled.
162      *
163      * @return Whether this timeout is disabled.
164      */
165     public boolean isDisabled() {
166         return getDuration() == 0;
167     }
168 
169     /**
170      * Whether this timeout is enabled.
171      *
172      * @return Whether this timeout is disabled.
173      */
174     public boolean isEnabled() {
175         return !isDisabled();
176     }
177 
178 }