// Path: common/src/main/java/net/javacrumbs/futureconverter/common/internal/ValueSource.java
// public interface ValueSource<T> {
//     /**
//      * Used to notify target object about changes in the original object.
//      */
//     void addCallbacks(Consumer<T> successCallback, Consumer<Throwable> failureCallback);
// 
//     /**
//      * Cancels execution of the original object if cancel is called on the target object
//      */
//     boolean cancel(boolean mayInterruptIfRunning);
// }
// 
// Path: common/src/main/java/net/javacrumbs/futureconverter/common/internal/ValueSourceFuture.java
// public abstract class ValueSourceFuture<T> extends FutureWrapper<T> implements ValueSource<T> {
//     protected ValueSourceFuture(Future<T> wrappedFuture) {
//         super(wrappedFuture);
//     }
// }


import net.javacrumbs.futureconverter.common.internal.ValueSource;
import net.javacrumbs.futureconverter.common.internal.ValueSourceFuture;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
    public static <T> ValueSource<T> createValueSource(CompletableFuture<T> completableFuture) {
        if (completableFuture instanceof ValueSourcebackedCompletableFuture) {
            return ((ValueSourcebackedCompletableFuture<T>) completableFuture).getValueSource();
        } else {
            return new CompletableFuturebackedValueSource<>(completableFuture);
        }
    }

    /**
     * CompletableFuture that takes values from the ValueSource. CompletableFuture is a class, not
     * an interface so we can not just forward events from the ValueSource, we to always instantiate the class.
     */
    private static final class ValueSourcebackedCompletableFuture<T> extends CompletableFuture<T> {
        private final ValueSource<T> valueSource;

        private ValueSourcebackedCompletableFuture(ValueSource<T> valueSource) {
            this.valueSource = valueSource;
            valueSource.addCallbacks(this::complete, this::completeExceptionally);
        }

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            if (isDone()) {
                return false;
            }
            boolean result = valueSource.cancel(mayInterruptIfRunning);
            super.cancel(mayInterruptIfRunning);
            return result;
        }

// Path: common/src/main/java/net/javacrumbs/futureconverter/common/internal/ValueSource.java
// public interface ValueSource<T> {
//     /**
//      * Used to notify target object about changes in the original object.
//      */
//     void addCallbacks(Consumer<T> successCallback, Consumer<Throwable> failureCallback);
// 
//     /**
//      * Cancels execution of the original object if cancel is called on the target object
//      */
//     boolean cancel(boolean mayInterruptIfRunning);
// }
// 
// Path: common/src/main/java/net/javacrumbs/futureconverter/common/internal/ValueSourceFuture.java
// public abstract class ValueSourceFuture<T> extends FutureWrapper<T> implements ValueSource<T> {
//     protected ValueSourceFuture(Future<T> wrappedFuture) {
//         super(wrappedFuture);
//     }
// }


// Path: java8-common/src/main/java/net/javacrumbs/futureconverter/java8common/Java8FutureUtils.java
import net.javacrumbs.futureconverter.common.internal.ValueSource;
import net.javacrumbs.futureconverter.common.internal.ValueSourceFuture;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

    public static <T> ValueSource<T> createValueSource(CompletableFuture<T> completableFuture) {
        if (completableFuture instanceof ValueSourcebackedCompletableFuture) {
            return ((ValueSourcebackedCompletableFuture<T>) completableFuture).getValueSource();
        } else {
            return new CompletableFuturebackedValueSource<>(completableFuture);
        }
    }

    /**
     * CompletableFuture that takes values from the ValueSource. CompletableFuture is a class, not
     * an interface so we can not just forward events from the ValueSource, we to always instantiate the class.
     */
    private static final class ValueSourcebackedCompletableFuture<T> extends CompletableFuture<T> {
        private final ValueSource<T> valueSource;

        private ValueSourcebackedCompletableFuture(ValueSource<T> valueSource) {
            this.valueSource = valueSource;
            valueSource.addCallbacks(this::complete, this::completeExceptionally);
        }

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            if (isDone()) {
                return false;
            }
            boolean result = valueSource.cancel(mayInterruptIfRunning);
            super.cancel(mayInterruptIfRunning);
            return result;
        }

        private ValueSource<T> getValueSource() {