Class AbstractWatchService

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.file.WatchService
    Direct Known Subclasses:
    PollingWatchService

    abstract class AbstractWatchService
    extends java.lang.Object
    implements java.nio.file.WatchService
    Abstract implementation of WatchService. Provides the means for registering and managing keys but does not handle actually watching. Subclasses should implement the means of watching watchables, posting events to registered keys and queueing keys with the service by signalling them.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.atomic.AtomicBoolean open  
      private java.nio.file.WatchKey poison  
      private java.util.concurrent.BlockingQueue<java.nio.file.WatchKey> queue  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancelled​(AbstractWatchService.Key key)
      Called when the given key is cancelled.
      private @Nullable java.nio.file.WatchKey check​(@Nullable java.nio.file.WatchKey key)
      Returns the given key, throwing an exception if it's the poison.
      protected void checkOpen()
      Checks that the watch service is open, throwing ClosedWatchServiceException if not.
      void close()  
      (package private) void enqueue​(AbstractWatchService.Key key)
      Enqueues the given key if the watch service is open; does nothing otherwise.
      boolean isOpen()
      Returns whether or not this watch service is open.
      @Nullable java.nio.file.WatchKey poll()  
      @Nullable java.nio.file.WatchKey poll​(long timeout, java.util.concurrent.TimeUnit unit)  
      (package private) com.google.common.collect.ImmutableList<java.nio.file.WatchKey> queuedKeys()  
      AbstractWatchService.Key register​(java.nio.file.Watchable watchable, java.lang.Iterable<? extends java.nio.file.WatchEvent.Kind<?>> eventTypes)
      Registers the given watchable with this service, returning a new watch key for it.
      java.nio.file.WatchKey take()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • queue

        private final java.util.concurrent.BlockingQueue<java.nio.file.WatchKey> queue
      • poison

        private final java.nio.file.WatchKey poison
      • open

        private final java.util.concurrent.atomic.AtomicBoolean open
    • Constructor Detail

      • AbstractWatchService

        AbstractWatchService()
    • Method Detail

      • register

        public AbstractWatchService.Key register​(java.nio.file.Watchable watchable,
                                                 java.lang.Iterable<? extends java.nio.file.WatchEvent.Kind<?>> eventTypes)
                                          throws java.io.IOException
        Registers the given watchable with this service, returning a new watch key for it. This implementation just checks that the service is open and creates a key; subclasses may override it to do other things as well.
        Throws:
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Returns whether or not this watch service is open.
      • enqueue

        final void enqueue​(AbstractWatchService.Key key)
        Enqueues the given key if the watch service is open; does nothing otherwise.
      • cancelled

        public void cancelled​(AbstractWatchService.Key key)
        Called when the given key is cancelled. Does nothing by default.
      • queuedKeys

        com.google.common.collect.ImmutableList<java.nio.file.WatchKey> queuedKeys()
      • poll

        public @Nullable java.nio.file.WatchKey poll()
        Specified by:
        poll in interface java.nio.file.WatchService
      • poll

        public @Nullable java.nio.file.WatchKey poll​(long timeout,
                                                     java.util.concurrent.TimeUnit unit)
                                              throws java.lang.InterruptedException
        Specified by:
        poll in interface java.nio.file.WatchService
        Throws:
        java.lang.InterruptedException
      • take

        public java.nio.file.WatchKey take()
                                    throws java.lang.InterruptedException
        Specified by:
        take in interface java.nio.file.WatchService
        Throws:
        java.lang.InterruptedException
      • check

        private @Nullable java.nio.file.WatchKey check​(@Nullable java.nio.file.WatchKey key)
        Returns the given key, throwing an exception if it's the poison.
      • checkOpen

        protected final void checkOpen()
        Checks that the watch service is open, throwing ClosedWatchServiceException if not.
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface java.nio.file.WatchService