// Path: src/main/java/org/sputnikdev/bluetooth/manager/BluetoothGovernor.java
// public interface BluetoothGovernor {
// 
//     /**
//      * Returns the URL of the corresponding Bluetooth object.
//      * @return the URL of the corresponding Bluetooth object
//      */
//     URL getURL();
// 
//     /**
//      * Checks whether the governor is in state when its corresponding bluetooth object is acquired
//      * and ready for manipulations.
//      * @return true if the corresponding bluetooth object is acquired and ready for manipulations
//      */
//     boolean isReady();
// 
//     /**
//      * Returns type of the corresponding Bluetooth object.
//      * @return type of the corresponding Bluetooth object
//      */
//     BluetoothObjectType getType();
// 
//     /**
//      * Returns the date/time of last known successful interaction with the corresponding native object.
//      * @return the date/time of last known successful interaction with the corresponding native object
//      */
//     Instant getLastInteracted();
// 
//     /**
//      * An accept method of the visitor pattern to process different bluetooth governors at once.
//      * @param visitor bluetooth governor visitor
//      * @throws Exception in case of any error
//      */
//     void accept(BluetoothObjectVisitor visitor) throws Exception;
// 
//     /**
//      * Register a new governor listener.
//      * @param listener a new governor listener
//      */
//     void addGovernorListener(GovernorListener listener);
// 
//     /**
//      * Unregister a governor listener.
//      * @param listener a governor listener
//      */
//     void removeGovernorListener(GovernorListener listener);
// 
// 
//     <G extends BluetoothGovernor, V> CompletableFuture<V> when(Predicate<G> condition, Function<G, V> function);
// 
//     default <G extends BluetoothGovernor> CompletableFuture<Void> doWhen(Predicate<G> predicate,
//                                                                               Consumer<G> consumer) {
//         return when(predicate, g -> {
//             consumer.accept((G) this);
//             return null;
//         });
//     }
// 
//     /**
//      * Returns a completable future that gets completed when governor becomes ready.
//      * @param function a function that is invoked when governor becomes ready, the completable future is
//      *                 completed with the result of this function
//      * @param <G> bluetooth governor
//      * @param <V> returned value
//      * @return a completable future
//      */
//     default <G extends BluetoothGovernor, V> CompletableFuture<V> whenReady(Function<G, V> function) {
//         return when(BluetoothGovernor::isReady, function);
//     }
// 
// }
// 
// Path: src/main/java/org/sputnikdev/bluetooth/manager/BluetoothInteractionException.java
// public class BluetoothInteractionException extends RuntimeException {
// 
//     public BluetoothInteractionException() { }
// 
//     public BluetoothInteractionException(String message) {
//         super(message);
//     }
// 
//     public BluetoothInteractionException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: src/main/java/org/sputnikdev/bluetooth/manager/NotReadyException.java
// public class NotReadyException extends RuntimeException {
// 
//     /**
//      * A constructor without message.
//      */
//     public NotReadyException() {
//         super();
//     }
// 
//     /**
//      * A constructor with a message.
//      * @param message a message
//      */
//     public NotReadyException(String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/org/sputnikdev/bluetooth/manager/auth/BluetoothAuthenticationException.java
// public class BluetoothAuthenticationException extends RuntimeException {
// 
//     public BluetoothAuthenticationException() { }
// 
//     public BluetoothAuthenticationException(String message) {
//         super(message);
//     }
// 
//     public BluetoothAuthenticationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sputnikdev.bluetooth.manager.BluetoothGovernor;
import org.sputnikdev.bluetooth.manager.BluetoothInteractionException;
import org.sputnikdev.bluetooth.manager.NotReadyException;
import org.sputnikdev.bluetooth.manager.auth.BluetoothAuthenticationException;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Function;
import java.util.function.Predicate;
package org.sputnikdev.bluetooth.manager.impl;



class CompletableFutureService<G extends BluetoothGovernor> {

    private Logger logger = LoggerFactory.getLogger(CompletableFutureService.class);

    private final ConcurrentLinkedQueue<DeferredCompletableFuture<G, ?>> futures =
            new ConcurrentLinkedQueue<>();

    <V> CompletableFuture<V> submit(G governor, Predicate<G> predicate, Function<G, V> function) {
        DeferredCompletableFuture<G, V> future = new DeferredCompletableFuture<>(predicate, function);

        try {
            if (!predicate.test(governor)) {
                logger.debug("Future is not ready to be completed immediately: {} : {}", governor.getURL(), predicate);
                futures.add(future);
                return future;
            }

            logger.debug("Trying to complete future immediately: {} : {}", governor.getURL(), predicate);

            future.complete(function.apply(governor));
// Path: src/main/java/org/sputnikdev/bluetooth/manager/BluetoothGovernor.java
// public interface BluetoothGovernor {
// 
//     /**
//      * Returns the URL of the corresponding Bluetooth object.
//      * @return the URL of the corresponding Bluetooth object
//      */
//     URL getURL();
// 
//     /**
//      * Checks whether the governor is in state when its corresponding bluetooth object is acquired
//      * and ready for manipulations.
//      * @return true if the corresponding bluetooth object is acquired and ready for manipulations
//      */
//     boolean isReady();
// 
//     /**
//      * Returns type of the corresponding Bluetooth object.
//      * @return type of the corresponding Bluetooth object
//      */
//     BluetoothObjectType getType();
// 
//     /**
//      * Returns the date/time of last known successful interaction with the corresponding native object.
//      * @return the date/time of last known successful interaction with the corresponding native object
//      */
//     Instant getLastInteracted();
// 
//     /**
//      * An accept method of the visitor pattern to process different bluetooth governors at once.
//      * @param visitor bluetooth governor visitor
//      * @throws Exception in case of any error
//      */
//     void accept(BluetoothObjectVisitor visitor) throws Exception;
// 
//     /**
//      * Register a new governor listener.
//      * @param listener a new governor listener
//      */
//     void addGovernorListener(GovernorListener listener);
// 
//     /**
//      * Unregister a governor listener.
//      * @param listener a governor listener
//      */
//     void removeGovernorListener(GovernorListener listener);
// 
// 
//     <G extends BluetoothGovernor, V> CompletableFuture<V> when(Predicate<G> condition, Function<G, V> function);
// 
//     default <G extends BluetoothGovernor> CompletableFuture<Void> doWhen(Predicate<G> predicate,
//                                                                               Consumer<G> consumer) {
//         return when(predicate, g -> {
//             consumer.accept((G) this);
//             return null;
//         });
//     }
// 
//     /**
//      * Returns a completable future that gets completed when governor becomes ready.
//      * @param function a function that is invoked when governor becomes ready, the completable future is
//      *                 completed with the result of this function
//      * @param <G> bluetooth governor
//      * @param <V> returned value
//      * @return a completable future
//      */
//     default <G extends BluetoothGovernor, V> CompletableFuture<V> whenReady(Function<G, V> function) {
//         return when(BluetoothGovernor::isReady, function);
//     }
// 
// }
// 
// Path: src/main/java/org/sputnikdev/bluetooth/manager/BluetoothInteractionException.java
// public class BluetoothInteractionException extends RuntimeException {
// 
//     public BluetoothInteractionException() { }
// 
//     public BluetoothInteractionException(String message) {
//         super(message);
//     }
// 
//     public BluetoothInteractionException(String message, Throwable cause) {
//         super(message, cause);
//     }
// }
// 
// Path: src/main/java/org/sputnikdev/bluetooth/manager/NotReadyException.java
// public class NotReadyException extends RuntimeException {
// 
//     /**
//      * A constructor without message.
//      */
//     public NotReadyException() {
//         super();
//     }
// 
//     /**
//      * A constructor with a message.
//      * @param message a message
//      */
//     public NotReadyException(String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/org/sputnikdev/bluetooth/manager/auth/BluetoothAuthenticationException.java
// public class BluetoothAuthenticationException extends RuntimeException {
// 
//     public BluetoothAuthenticationException() { }
// 
//     public BluetoothAuthenticationException(String message) {
//         super(message);
//     }
// 
//     public BluetoothAuthenticationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


// Path: src/main/java/org/sputnikdev/bluetooth/manager/impl/CompletableFutureService.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sputnikdev.bluetooth.manager.BluetoothGovernor;
import org.sputnikdev.bluetooth.manager.BluetoothInteractionException;
import org.sputnikdev.bluetooth.manager.NotReadyException;
import org.sputnikdev.bluetooth.manager.auth.BluetoothAuthenticationException;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Function;
import java.util.function.Predicate;

package org.sputnikdev.bluetooth.manager.impl;



class CompletableFutureService<G extends BluetoothGovernor> {

    private Logger logger = LoggerFactory.getLogger(CompletableFutureService.class);

    private final ConcurrentLinkedQueue<DeferredCompletableFuture<G, ?>> futures =
            new ConcurrentLinkedQueue<>();

    <V> CompletableFuture<V> submit(G governor, Predicate<G> predicate, Function<G, V> function) {
        DeferredCompletableFuture<G, V> future = new DeferredCompletableFuture<>(predicate, function);

        try {
            if (!predicate.test(governor)) {
                logger.debug("Future is not ready to be completed immediately: {} : {}", governor.getURL(), predicate);
                futures.add(future);
                return future;
            }

            logger.debug("Trying to complete future immediately: {} : {}", governor.getURL(), predicate);

            future.complete(function.apply(governor));
        } catch (BluetoothInteractionException | NotReadyException | BluetoothAuthenticationException nativeException) {