-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
WatchService.java
79 lines (69 loc) · 3.02 KB
/
WatchService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.barbarysoftware.watchservice;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public abstract class WatchService implements Closeable {
protected WatchService() {
}
/**
* Closes this watch service.
* <p/>
* <p> If a thread is currently blocked in the {@link #take take} or {@link
* #poll(long,TimeUnit) poll} methods waiting for a key to be queued then
* it immediately receives a {@link ClosedWatchServiceException}. Any
* valid keys associated with this watch service are {@link WatchKey#isValid
* invalidated}.
* <p/>
* <p> After a watch service is closed, any further attempt to invoke
* operations upon it will throw {@link ClosedWatchServiceException}.
* If this watch service is already closed then invoking this method
* has no effect.
*
* @throws IOException if an I/O error occurs
*/
@Override
public abstract void close() throws IOException;
/**
* Retrieves and removes the next watch key, or {@code null} if none are
* present.
*
* @return the next watch key, or {@code null}
* @throws ClosedWatchServiceException if this watch service is closed
*/
public abstract WatchKey poll();
/**
* Retrieves and removes the next watch key, waiting if necessary up to the
* specified wait time if none are yet present.
*
* @param timeout how to wait before giving up, in units of unit
* @param unit a {@code TimeUnit} determining how to interpret the timeout
* parameter
* @return the next watch key, or {@code null}
* @throws ClosedWatchServiceException if this watch service is closed, or it is closed while waiting
* for the next key
* @throws InterruptedException if interrupted while waiting
*/
public abstract WatchKey poll(long timeout, TimeUnit unit)
throws InterruptedException;
/**
* Retrieves and removes next watch key, waiting if none are yet present.
*
* @return the next watch key
* @throws ClosedWatchServiceException if this watch service is closed, or it is closed while waiting
* for the next key
* @throws InterruptedException if interrupted while waiting
*/
public abstract WatchKey take() throws InterruptedException;
public static WatchService newWatchService() {
final String[] osVersion = System.getProperty("os.version").split("\\.");
final int majorVersion = Integer.parseInt(osVersion[0]);
final int minorVersion = Integer.parseInt(osVersion[1]);
if ((majorVersion < 11) & (minorVersion < 5)) {
throw new UnsupportedOperationException("barbarywatchservice not "
+ "supported on Mac OS X prior to Leopard (10.5)");
} else {
// for Mac OS X Leopard (10.5) and upwards
return new MacOSXListeningWatchService();
}
}
}