View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.base;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import java.util.concurrent.TimeoutException;
36  import javax.annotation.Nonnegative;
37  
38  /**
39   * @author Zoltan Farkas
40   */
41  @SuppressFBWarnings("FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY")
42  public final class TimeoutDeadline {
43  
44    private final long timeoutNanos;
45  
46    private final long deadlineNanos;
47  
48    public TimeoutDeadline(@Nonnegative final long timeoutNanos, final long deadlineNanos) {
49      this.timeoutNanos = timeoutNanos;
50      this.deadlineNanos = deadlineNanos;
51    }
52  
53    public static TimeoutDeadline of(final long timeoutNanos, final long deadlineNanos)
54      throws TimeoutException {
55      if (timeoutNanos < 0) {
56        throw new TimeoutException("Timeout exceeded by " + (-timeoutNanos) + " ns, deadline: "
57                + Timing.getCurrentTiming().fromNanoTimeToInstant(deadlineNanos));
58      }
59      return new TimeoutDeadline(timeoutNanos, deadlineNanos);
60    }
61  
62  
63    public long getTimeoutNanos() {
64      return timeoutNanos;
65    }
66  
67    public long getDeadlineNanos() {
68      return deadlineNanos;
69    }
70  
71    @Override
72    public int hashCode() {
73      int hash = 3;
74      hash = 23 * hash + (int) (this.timeoutNanos ^ (this.timeoutNanos >>> 32));
75      return 23 * hash + (int) (this.deadlineNanos ^ (this.deadlineNanos >>> 32));
76    }
77  
78    @Override
79    public boolean equals(final Object obj) {
80      if (this == obj) {
81        return true;
82      }
83      if (obj == null) {
84        return false;
85      }
86      if (getClass() != obj.getClass()) {
87        return false;
88      }
89      final TimeoutDeadline other = (TimeoutDeadline) obj;
90      if (this.timeoutNanos != other.timeoutNanos) {
91        return false;
92      }
93      return this.deadlineNanos == other.deadlineNanos;
94    }
95  
96    @Override
97    public String toString() {
98      return "TimeoutDeadline{" + "timeoutNanos=" + timeoutNanos + ", deadlineNanos=" + deadlineNanos + '}';
99    }
100 
101 
102 }