EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.concurrency]

COVERAGE SUMMARY FOR SOURCE FILE [Timeout.java]

nameclass, %method, %block, %line, %
Timeout.java100% (2/2)92%  (11/12)76%  (192/251)72%  (37.7/52)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Timeout100% (1/1)91%  (10/11)76%  (188/247)71%  (35.7/50)
join (Thread): void 0%   (0/1)0%   (0/32)0%   (0/11)
toString (): String 100% (1/1)72%  (33/46)86%  (6/7)
equals (Object): boolean 100% (1/1)76%  (19/25)73%  (2.2/3)
parse (String): Timeout 100% (1/1)78%  (25/32)80%  (5.6/7)
hasNotTimedOut (): boolean 100% (1/1)93%  (13/14)92%  (0.9/1)
<static initializer> 100% (1/1)100% (11/11)100% (2/2)
Timeout (long): void 100% (1/1)100% (46/46)100% (13/13)
getMillisLeft (): long 100% (1/1)100% (19/19)100% (3/3)
hashCode (): int 100% (1/1)100% (9/9)100% (1/1)
isNever (): boolean 100% (1/1)100% (3/3)100% (1/1)
or (Timeout): Timeout 100% (1/1)100% (10/10)100% (1/1)
     
class Timeout$TimeoutException100% (1/1)100% (1/1)100% (4/4)100% (2/2)
Timeout$TimeoutException (String): void 100% (1/1)100% (4/4)100% (2/2)

1package 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 &quot;AS IS&quot; 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 
19import org.jetbrains.annotations.NotNull;
20import 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
28public 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}

[all classes][org.jtoolkit.essence.concurrency]
EMMA 2.0.5312 (C) Vladimir Roubtsov