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.util.concurrent.TimeoutException;
31  
32  /**
33   * A specialization of {@link TimeoutException} that carries a {@link Timeout} deadline and the actual value.
34   *
35   * @since 5.0
36   */
37  public class TimeoutValueException extends TimeoutException {
38  
39      private static final long serialVersionUID = 1L;
40  
41      /**
42       * Creates a new exception for the given timeout deadline and actual timeout.
43       *
44       * @param timeoutDeadline How long was the expected timeout in milliseconds.
45       * @param timeoutActual How long we actually waited in milliseconds.
46       * @return a new TimeoutValueException.
47       */
48      public static TimeoutValueException fromMilliseconds(final long timeoutDeadline, final long timeoutActual) {
49          return new TimeoutValueException(Timeout.ofMilliseconds(min0(timeoutDeadline)),
50                  Timeout.ofMilliseconds(min0(timeoutActual)));
51      }
52  
53      /**
54       * Returns the given {@code value} if positive, otherwise returns 0.
55       *
56       * @param value any timeout
57       * @return the given {@code value} if positive, otherwise returns 0.
58       */
59      private static long min0(final long value) {
60          return value < 0 ? 0 : value;
61      }
62  
63      private final Timeout actual;
64  
65      private final Timeout deadline;
66  
67      /**
68       * Creates a new exception for the given timeout deadline and actual timeout.
69       *
70       * @param deadline How long was the expected timeout.
71       * @param actual How long we actually waited.
72       */
73      public TimeoutValueException(final Timeoutout.html#Timeout">Timeout deadline, final Timeout actual) {
74          super(String.format("Timeout deadline: %s, actual: %s", deadline, actual));
75          this.actual = actual;
76          this.deadline = deadline;
77      }
78  
79      /**
80       * Gets how long was the expected timeout in milliseconds.
81       *
82       * @return how long was the expected timeout in milliseconds.
83       */
84      public Timeout getActual() {
85          return actual;
86      }
87  
88      /**
89       * Gets how long we actually waited in milliseconds.
90       *
91       * @return how long we actually waited in milliseconds.
92       */
93      public Timeout getDeadline() {
94          return deadline;
95      }
96  
97  }