| 1 | package org.jtoolkit.essence.concurrency; |
| 2 | |
| 3 | /* |
| 4 | Copyright 2006 Peter Lawrey |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import org.jetbrains.annotations.NotNull; |
| 20 | import static org.jtoolkit.essence.utils.StringUtils.isBlank; |
| 21 | |
| 22 | /** |
| 23 | * A standardised timeout with a fixed end time. Using the same timeout ensures all operation stop at a given future time. |
| 24 | * |
| 25 | * @author Peter Lawrey |
| 26 | */ |
| 27 | @Immutable |
| 28 | public class Timeout { |
| 29 | public static final Timeout NEVER = new Timeout(Long.MAX_VALUE); |
| 30 | public static final Timeout ALWAYS = new Timeout(Long.MIN_VALUE); |
| 31 | |
| 32 | private final long endTime; |
| 33 | private final boolean never; |
| 34 | private final boolean always; |
| 35 | public static final String NEVER_STR = "never"; |
| 36 | private static final String ALWAYS_STR = "always"; |
| 37 | private static final String TIMEOUT_IN = "Timeout in "; |
| 38 | |
| 39 | public Timeout(long timeoutMS) { |
| 40 | if (timeoutMS == Long.MIN_VALUE) { |
| 41 | never = false; |
| 42 | always = true; |
| 43 | endTime = timeoutMS; |
| 44 | } else if (timeoutMS == Long.MAX_VALUE) { |
| 45 | never = true; |
| 46 | always = false; |
| 47 | endTime = Long.MAX_VALUE; |
| 48 | } else { |
| 49 | never = false; |
| 50 | always = false; |
| 51 | endTime = System.nanoTime() + timeoutMS * 1000L * 1000L; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Derive a timeout fronm a string. Either the time is "never" or "" which means never or a delay in milliseconds. |
| 57 | */ |
| 58 | public static Timeout parse(String serverTimeOutStr) throws IllegalArgumentException { |
| 59 | if (isBlank(serverTimeOutStr) || NEVER_STR.equalsIgnoreCase(serverTimeOutStr)) |
| 60 | return NEVER; |
| 61 | if (ALWAYS_STR.equalsIgnoreCase(serverTimeOutStr)) |
| 62 | return ALWAYS; |
| 63 | long time = Long.parseLong(serverTimeOutStr); |
| 64 | if (time < 0) throw new IllegalArgumentException("Cannot get a negative timeout."); |
| 65 | return new Timeout(time); |
| 66 | } |
| 67 | |
| 68 | public boolean isNever() { |
| 69 | return never; |
| 70 | } |
| 71 | |
| 72 | public boolean hasNotTimedOut() { |
| 73 | // do a minus in case of wrapped values. |
| 74 | return !always && System.nanoTime() - endTime < 0; |
| 75 | } |
| 76 | |
| 77 | public long getMillisLeft() { |
| 78 | if (always) return 0; |
| 79 | if (never) return Long.MAX_VALUE; |
| 80 | return (endTime - System.nanoTime() + 999 * 1000) / (1000 * 1000); |
| 81 | } |
| 82 | |
| 83 | public void join(@NotNull Thread thread) throws InterruptedException { |
| 84 | if (!thread.isAlive()) return; |
| 85 | long millisLeft = getMillisLeft(); |
| 86 | if (millisLeft < 0) return; |
| 87 | if (millisLeft == 0) { |
| 88 | if (Thread.currentThread().isInterrupted()) |
| 89 | throw new InterruptedException(); |
| 90 | if (thread.isAlive()) |
| 91 | // noinspection CallToThreadYield |
| 92 | Thread.yield(); |
| 93 | return; |
| 94 | } |
| 95 | thread.join(millisLeft); |
| 96 | } |
| 97 | |
| 98 | public String toString() { |
| 99 | if (never) return "Never timeout"; |
| 100 | long left = getMillisLeft(); |
| 101 | if (left == 0) |
| 102 | return "Timeout now"; |
| 103 | if (left > -10000 && left < 10000) |
| 104 | return TIMEOUT_IN + left + " ms"; |
| 105 | return TIMEOUT_IN + left / 1000 + " secs"; |
| 106 | } |
| 107 | |
| 108 | public boolean equals(Object o) { |
| 109 | if (this == o) return true; |
| 110 | //noinspection SimplifiableIfStatement |
| 111 | if (o == null || getClass() != o.getClass()) return false; |
| 112 | return endTime == ((Timeout) o).endTime; |
| 113 | } |
| 114 | |
| 115 | public int hashCode() { |
| 116 | return (int) (endTime ^ endTime >>> 32); |
| 117 | } |
| 118 | |
| 119 | public Timeout or(Timeout timeout) { |
| 120 | return endTime < timeout.endTime ? this : timeout; |
| 121 | } |
| 122 | |
| 123 | public static class TimeoutException extends RuntimeException { |
| 124 | public TimeoutException(String message) { |
| 125 | super(message); |
| 126 | } |
| 127 | } |
| 128 | } |