Path: blob/master/src/java.base/share/classes/java/nio/file/WatchKey.java
41159 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.util.List;2829/**30* A token representing the registration of a {@link Watchable watchable} object31* with a {@link WatchService}.32*33* <p> A watch key is created when a watchable object is registered with a watch34* service. The key remains {@link #isValid valid} until:35* <ol>36* <li> It is cancelled, explicitly, by invoking its {@link #cancel cancel}37* method, or</li>38* <li> Cancelled implicitly, because the object is no longer accessible,39* or </li>40* <li> By {@link WatchService#close closing} the watch service. </li>41* </ol>42*43* <p> A watch key has a state. When initially created the key is said to be44* <em>ready</em>. When an event is detected then the key is <em>signalled</em>45* and queued so that it can be retrieved by invoking the watch service's {@link46* WatchService#poll() poll} or {@link WatchService#take() take} methods. Once47* signalled, a key remains in this state until its {@link #reset reset} method48* is invoked to return the key to the ready state. Events detected while the49* key is in the signalled state are queued but do not cause the key to be50* re-queued for retrieval from the watch service. Events are retrieved by51* invoking the key's {@link #pollEvents pollEvents} method. This method52* retrieves and removes all events accumulated for the object. When initially53* created, a watch key has no pending events. Typically events are retrieved54* when the key is in the signalled state leading to the following idiom:55*56* <pre>57* for (;;) {58* // retrieve key59* WatchKey key = watcher.take();60*61* // process events62* for (WatchEvent<?> event: key.pollEvents()) {63* :64* }65*66* // reset the key67* boolean valid = key.reset();68* if (!valid) {69* // object no longer registered70* }71* }72* </pre>73*74* <p> Watch keys are safe for use by multiple concurrent threads. Where there75* are several threads retrieving signalled keys from a watch service then care76* should be taken to ensure that the {@code reset} method is only invoked after77* the events for the object have been processed. This ensures that one thread78* is processing the events for an object at any time.79*80* @since 1.781*/8283public interface WatchKey {8485/**86* Tells whether or not this watch key is valid.87*88* <p> A watch key is valid upon creation and remains until it is cancelled,89* or its watch service is closed.90*91* @return {@code true} if, and only if, this watch key is valid92*/93boolean isValid();9495/**96* Retrieves and removes all pending events for this watch key, returning97* a {@code List} of the events that were retrieved.98*99* <p> Note that this method does not wait if there are no events pending.100*101* @return the list of the events retrieved; may be empty102*/103List<WatchEvent<?>> pollEvents();104105/**106* Resets this watch key.107*108* <p> If this watch key has been cancelled or this watch key is already in109* the ready state then invoking this method has no effect. Otherwise110* if there are pending events for the object then this watch key is111* immediately re-queued to the watch service. If there are no pending112* events then the watch key is put into the ready state and will remain in113* that state until an event is detected or the watch key is cancelled.114*115* @return {@code true} if the watch key is valid and has been reset, and116* {@code false} if the watch key could not be reset because it is117* no longer {@link #isValid valid}118*/119boolean reset();120121/**122* Cancels the registration with the watch service. Upon return the watch key123* will be invalid. If the watch key is enqueued, waiting to be retrieved124* from the watch service, then it will remain in the queue until it is125* removed. Pending events, if any, remain pending and may be retrieved by126* invoking the {@link #pollEvents pollEvents} method after the key is127* cancelled.128*129* <p> If this watch key has already been cancelled then invoking this130* method has no effect. Once cancelled, a watch key remains forever invalid.131*/132void cancel();133134/**135* Returns the object for which this watch key was created. This method will136* continue to return the object even after the key is cancelled.137*138* <p> As the {@code WatchService} is intended to map directly on to the139* native file event notification facility (where available) then many of140* details on how registered objects are watched is highly implementation141* specific. When watching a directory for changes for example, and the142* directory is moved or renamed in the file system, there is no guarantee143* that the watch key will be cancelled and so the object returned by this144* method may no longer be a valid path to the directory.145*146* @return the object for which this watch key was created147*/148Watchable watchable();149}150151152