jdk/src/share/classes/sun/nio/fs/AbstractWatchKey.java
author alanb
Mon, 09 May 2011 18:45:33 +0100
changeset 9679 d98ae8bc45fc
parent 9035 1255eb81cc2f
permissions -rw-r--r--
7042979: Rename StandardSocketOption and StandardWatchEventKind Reviewed-by: forax, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8808
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4979
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4979
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4979
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4979
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4979
diff changeset
    23
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    25
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    26
package sun.nio.fs;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    28
import java.nio.file.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    29
import java.util.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    30
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    31
/**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    32
 * Base implementation class for watch keys.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    34
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    35
abstract class AbstractWatchKey implements WatchKey {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    37
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    38
     * Maximum size of event list (in the future this may be tunable)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    39
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    40
    static final int MAX_EVENT_LIST_SIZE    = 512;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    41
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    42
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    43
     * Special event to signal overflow
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    44
     */
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
    45
    static final Event<Object> OVERFLOW_EVENT =
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
    46
        new Event<Object>(StandardWatchEventKinds.OVERFLOW, null);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    47
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    48
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    49
     * Possible key states
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    50
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    51
    private static enum State { READY, SIGNALLED };
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    52
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    53
    // reference to watcher
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    54
    private final AbstractWatchService watcher;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    55
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    56
    // reference to the original directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    57
    private final Path dir;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    58
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    59
    // key state
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    60
    private State state;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    61
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    62
    // pending events
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    63
    private List<WatchEvent<?>> events;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    64
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
    65
    // maps a context to the last event for the context (iff the last queued
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
    66
    // event for the context is an ENTRY_MODIFY event).
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
    67
    private Map<Object,WatchEvent<?>> lastModifyEvents;
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
    68
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    69
    protected AbstractWatchKey(Path dir, AbstractWatchService watcher) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    70
        this.watcher = watcher;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    71
        this.dir = dir;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    72
        this.state = State.READY;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    73
        this.events = new ArrayList<WatchEvent<?>>();
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
    74
        this.lastModifyEvents = new HashMap<Object,WatchEvent<?>>();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    75
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    76
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    77
    final AbstractWatchService watcher() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    78
        return watcher;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    79
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    80
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    81
    /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    82
     * Return the original watchable (Path)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    83
     */
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
    84
    @Override
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
    85
    public Path watchable() {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    86
        return dir;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    87
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    88
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    89
    /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    90
     * Enqueues this key to the watch service
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    91
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    92
    final void signal() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    93
        synchronized (this) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    94
            if (state == State.READY) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    95
                state = State.SIGNALLED;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    96
                watcher.enqueueKey(this);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    97
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    98
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    99
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   100
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   101
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   102
     * Adds the event to this key and signals it.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   103
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   104
    @SuppressWarnings("unchecked")
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   105
    final void signalEvent(WatchEvent.Kind<?> kind, Object context) {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   106
        boolean isModify = (kind == StandardWatchEventKinds.ENTRY_MODIFY);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   107
        synchronized (this) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   108
            int size = events.size();
4667
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   109
            if (size > 0) {
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   110
                // if the previous event is an OVERFLOW event or this is a
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   111
                // repeated event then we simply increment the counter
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   112
                WatchEvent<?> prev = events.get(size-1);
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   113
                if ((prev.kind() == StandardWatchEventKinds.OVERFLOW) ||
4667
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   114
                    ((kind == prev.kind() &&
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   115
                     Objects.equals(context, prev.context()))))
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   116
                {
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   117
                    ((Event<?>)prev).increment();
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   118
                    return;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   119
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   120
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   121
                // if this is a modify event and the last entry for the context
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   122
                // is a modify event then we simply increment the count
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   123
                if (!lastModifyEvents.isEmpty()) {
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   124
                    if (isModify) {
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   125
                        WatchEvent<?> ev = lastModifyEvents.get(context);
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   126
                        if (ev != null) {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   127
                            assert ev.kind() == StandardWatchEventKinds.ENTRY_MODIFY;
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   128
                            ((Event<?>)ev).increment();
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   129
                            return;
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   130
                        }
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   131
                    } else {
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   132
                        // not a modify event so remove from the map as the
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   133
                        // last event will no longer be a modify event.
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   134
                        lastModifyEvents.remove(context);
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   135
                    }
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   136
                }
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   137
4667
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   138
                // if the list has reached the limit then drop pending events
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   139
                // and queue an OVERFLOW event
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   140
                if (size >= MAX_EVENT_LIST_SIZE) {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   141
                    kind = StandardWatchEventKinds.OVERFLOW;
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   142
                    isModify = false;
4667
dd65a1df2231 6907760: (file) OVERFLOW event should cause pending events to be purged
alanb
parents: 2057
diff changeset
   143
                    context = null;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   144
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   145
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   146
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   147
            // non-repeated event
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   148
            Event<Object> ev =
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   149
                new Event<Object>((WatchEvent.Kind<Object>)kind, context);
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   150
            if (isModify) {
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   151
                lastModifyEvents.put(context, ev);
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 9035
diff changeset
   152
            } else if (kind == StandardWatchEventKinds.OVERFLOW) {
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   153
                // drop all pending events
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   154
                events.clear();
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   155
                lastModifyEvents.clear();
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   156
            }
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   157
            events.add(ev);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   158
            signal();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   159
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   160
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   161
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   162
    @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   163
    public final List<WatchEvent<?>> pollEvents() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   164
        synchronized (this) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   165
            List<WatchEvent<?>> result = events;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   166
            events = new ArrayList<WatchEvent<?>>();
4979
9043b4b40b30 6929532: (file) WatchService should avoid queuing new modify events when lots of files are changing
alanb
parents: 4667
diff changeset
   167
            lastModifyEvents.clear();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   168
            return result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   169
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   170
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   171
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   172
    @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   173
    public final boolean reset() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   174
        synchronized (this) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   175
            if (state == State.SIGNALLED && isValid()) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   176
                if (events.isEmpty()) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   177
                    state = State.READY;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   178
                } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   179
                    // pending events so re-queue key
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   180
                    watcher.enqueueKey(this);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   181
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   182
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   183
            return isValid();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   184
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   185
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   186
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   187
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   188
     * WatchEvent implementation
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   189
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   190
    private static class Event<T> implements WatchEvent<T> {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   191
        private final WatchEvent.Kind<T> kind;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   192
        private final T context;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   193
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   194
        // synchronize on watch key to access/increment count
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   195
        private int count;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   196
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   197
        Event(WatchEvent.Kind<T> type, T context) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   198
            this.kind = type;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   199
            this.context = context;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   200
            this.count = 1;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   201
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   202
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   203
        @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   204
        public WatchEvent.Kind<T> kind() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   205
            return kind;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   206
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   207
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   208
        @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   209
        public T context() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   210
            return context;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   211
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   212
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   213
        @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   214
        public int count() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   215
            return count;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   216
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   217
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   218
        // for repeated events
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   219
        void increment() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   220
            count++;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   221
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   222
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   223
}