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");
        final int minorVersion = Integer.parseInt(osVersion.split("\\.")[1]);
        if (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();
        }
    }

}

@@ -2524,16 +2524,18 @@
 l String
+%5B%5D
  osVersi
@@ -2571,16 +2571,29 @@
 ersion%22)
+.split(%22%5C%5C.%22)
 ;%0A      
@@ -2605,18 +2605,18 @@
 al int m
-in
+aj
 orVersio
@@ -2649,37 +2649,112 @@
 sion
-.split(%22%5C%5C.%22)%5B1%5D);%0A        if
+%5B0%5D);%0A        final int minorVersion = Integer.parseInt(osVersion%5B1%5D);%0A        if ((majorVersion %3C 11) &
  (mi
@@ -2768,16 +2768,17 @@
 ion %3C 5)
+)
  %7B%0A     

Fix watch service on macOS 11