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