15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.nio.fs;
27
28 import java.nio.file.*;
29 import java.util.*;
30
31 /**
32 * Base implementation class for watch keys.
33 */
34
35 abstract class AbstractWatchKey extends WatchKey {
36
37 /**
38 * Maximum size of event list (in the future this may be tunable)
39 */
40 static final int MAX_EVENT_LIST_SIZE = 512;
41
42 /**
43 * Special event to signal overflow
44 */
45 static final Event<Void> OVERFLOW_EVENT =
46 new Event<Void>(StandardWatchEventKind.OVERFLOW, null);
47
48 /**
49 * Possible key states
50 */
51 private static enum State { READY, SIGNALLED };
52
53 // reference to watcher
54 private final AbstractWatchService watcher;
55
56 // key state
57 private State state;
58
59 // pending events
60 private List<WatchEvent<?>> events;
61
62 // maps a context to the last event for the context (iff the last queued
63 // event for the context is an ENTRY_MODIFY event).
64 private Map<Object,WatchEvent<?>> lastModifyEvents;
65
66 protected AbstractWatchKey(AbstractWatchService watcher) {
67 this.watcher = watcher;
68 this.state = State.READY;
69 this.events = new ArrayList<WatchEvent<?>>();
70 this.lastModifyEvents = new HashMap<Object,WatchEvent<?>>();
71 }
72
73 final AbstractWatchService watcher() {
74 return watcher;
75 }
76
77 /**
78 * Enqueues this key to the watch service
79 */
80 final void signal() {
81 synchronized (this) {
82 if (state == State.READY) {
83 state = State.SIGNALLED;
84 watcher.enqueueKey(this);
85 }
86 }
87 }
88
89 /**
90 * Adds the event to this key and signals it.
91 */
92 @SuppressWarnings("unchecked")
93 final void signalEvent(WatchEvent.Kind<?> kind, Object context) {
94 boolean isModify = (kind == StandardWatchEventKind.ENTRY_MODIFY);
95 synchronized (this) {
96 int size = events.size();
97 if (size > 0) {
158 }
159
160 @Override
161 public final boolean reset() {
162 synchronized (this) {
163 if (state == State.SIGNALLED && isValid()) {
164 if (events.isEmpty()) {
165 state = State.READY;
166 } else {
167 // pending events so re-queue key
168 watcher.enqueueKey(this);
169 }
170 }
171 return isValid();
172 }
173 }
174
175 /**
176 * WatchEvent implementation
177 */
178 private static class Event<T> extends WatchEvent<T> {
179 private final WatchEvent.Kind<T> kind;
180 private final T context;
181
182 // synchronize on watch key to access/increment count
183 private int count;
184
185 Event(WatchEvent.Kind<T> type, T context) {
186 this.kind = type;
187 this.context = context;
188 this.count = 1;
189 }
190
191 @Override
192 public WatchEvent.Kind<T> kind() {
193 return kind;
194 }
195
196 @Override
197 public T context() {
198 return context;
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.nio.fs;
27
28 import java.nio.file.*;
29 import java.util.*;
30
31 /**
32 * Base implementation class for watch keys.
33 */
34
35 abstract class AbstractWatchKey implements WatchKey {
36
37 /**
38 * Maximum size of event list (in the future this may be tunable)
39 */
40 static final int MAX_EVENT_LIST_SIZE = 512;
41
42 /**
43 * Special event to signal overflow
44 */
45 static final Event<Void> OVERFLOW_EVENT =
46 new Event<Void>(StandardWatchEventKind.OVERFLOW, null);
47
48 /**
49 * Possible key states
50 */
51 private static enum State { READY, SIGNALLED };
52
53 // reference to watcher
54 private final AbstractWatchService watcher;
55
56 // reference to the original directory
57 private final Path dir;
58
59 // key state
60 private State state;
61
62 // pending events
63 private List<WatchEvent<?>> events;
64
65 // maps a context to the last event for the context (iff the last queued
66 // event for the context is an ENTRY_MODIFY event).
67 private Map<Object,WatchEvent<?>> lastModifyEvents;
68
69 protected AbstractWatchKey(Path dir, AbstractWatchService watcher) {
70 this.watcher = watcher;
71 this.dir = dir;
72 this.state = State.READY;
73 this.events = new ArrayList<WatchEvent<?>>();
74 this.lastModifyEvents = new HashMap<Object,WatchEvent<?>>();
75 }
76
77 final AbstractWatchService watcher() {
78 return watcher;
79 }
80
81 /**
82 * Return the original watchable (Path)
83 */
84 Path watchable() {
85 return dir;
86 }
87
88 /**
89 * Enqueues this key to the watch service
90 */
91 final void signal() {
92 synchronized (this) {
93 if (state == State.READY) {
94 state = State.SIGNALLED;
95 watcher.enqueueKey(this);
96 }
97 }
98 }
99
100 /**
101 * Adds the event to this key and signals it.
102 */
103 @SuppressWarnings("unchecked")
104 final void signalEvent(WatchEvent.Kind<?> kind, Object context) {
105 boolean isModify = (kind == StandardWatchEventKind.ENTRY_MODIFY);
106 synchronized (this) {
107 int size = events.size();
108 if (size > 0) {
169 }
170
171 @Override
172 public final boolean reset() {
173 synchronized (this) {
174 if (state == State.SIGNALLED && isValid()) {
175 if (events.isEmpty()) {
176 state = State.READY;
177 } else {
178 // pending events so re-queue key
179 watcher.enqueueKey(this);
180 }
181 }
182 return isValid();
183 }
184 }
185
186 /**
187 * WatchEvent implementation
188 */
189 private static class Event<T> implements WatchEvent<T> {
190 private final WatchEvent.Kind<T> kind;
191 private final T context;
192
193 // synchronize on watch key to access/increment count
194 private int count;
195
196 Event(WatchEvent.Kind<T> type, T context) {
197 this.kind = type;
198 this.context = context;
199 this.count = 1;
200 }
201
202 @Override
203 public WatchEvent.Kind<T> kind() {
204 return kind;
205 }
206
207 @Override
208 public T context() {
209 return context;
|