All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.grizzly.utils.Futures Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*
 * Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.grizzly.utils;

import java.util.concurrent.Future;

import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.Copyable;
import org.glassfish.grizzly.EmptyCompletionHandler;
import org.glassfish.grizzly.GrizzlyFuture;
import org.glassfish.grizzly.impl.FutureImpl;
import org.glassfish.grizzly.impl.ReadyFutureImpl;
import org.glassfish.grizzly.impl.SafeFutureImpl;
import org.glassfish.grizzly.impl.UnsafeFutureImpl;

/**
 * Set of {@link Future} utilities.
 *
 * @author Alexey Stashok
 */
public class Futures {

    /**
     * Returns thread-safe {@link FutureImpl} implementation. (Based on the JDK {@link java.util.concurrent.FutureTask}).
     *
     * @return thread-safe {@link FutureImpl} implementation.
     */
    public static  FutureImpl createSafeFuture() {
        return SafeFutureImpl.create();
    }

    /**
     * Returns non thread-safe {@link FutureImpl} implementation.
     *
     * @return non thread-safe {@link FutureImpl} implementation.
     */
    public static  FutureImpl createUnsafeFuture() {
        return UnsafeFutureImpl.create();
    }

    /**
     * Create a {@link Future}, which has a preset result.
     *
     * @param result the result
     * @return a {@link Future}, which has a preset result.
     */
    public static  GrizzlyFuture createReadyFuture(final R result) {
        return ReadyFutureImpl.create(result);
    }

    /**
     * Create a {@link Future}, which has a preset failure.
     *
     * @param error the failure
     * @return a {@link Future}, which has a preset failure.
     */
    public static  GrizzlyFuture createReadyFuture(final Throwable error) {
        return ReadyFutureImpl.create(error);
    }

    /**
     * Complete passed {@link FutureImpl} and {@link CompletionHandler} using the passed result object.
     *
     * @param future {@link FutureImpl} to be notified
     * @param completionHandler {@link CompletionHandler} to be notified
     * @param result the result
     */
    public static  void notifyResult(final FutureImpl future, final CompletionHandler completionHandler, final R result) {
        if (completionHandler != null) {
            completionHandler.completed(result);
        }

        if (future != null) {
            future.result(result);
        }
    }

    /**
     * Complete passed {@link FutureImpl} and {@link CompletionHandler} using the passed error
     *
     * @param future {@link FutureImpl} to be notified
     * @param completionHandler {@link CompletionHandler} to be notified
     * @param error the error.
     */
    public static  void notifyFailure(final FutureImpl future, final CompletionHandler completionHandler, final Throwable error) {
        if (completionHandler != null) {
            completionHandler.failed(error);
        }

        if (future != null) {
            future.failure(error);
        }
    }

    /**
     * Complete passed {@link FutureImpl} and {@link CompletionHandler} via the cancellation notification.
     *
     * @param future {@link FutureImpl} to be notified
     * @param completionHandler {@link CompletionHandler} to be notified
     */
    public static  void notifyCancel(final FutureImpl future, final CompletionHandler completionHandler) {
        if (completionHandler != null) {
            completionHandler.cancelled();
        }

        if (future != null) {
            future.cancel(false);
        }
    }

    /**
     * Creates {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl}. All the notifications
     * coming to the returned {@link CompletionHandler} will be passed to the passed {@link FutureImpl}.
     *
     * @return {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl}. All the notifications
     * coming to the returned {@link CompletionHandler} will be passed to the passed {@link FutureImpl}.
     */
    public static  CompletionHandler toCompletionHandler(final FutureImpl future) {
        return new FutureToCompletionHandler<>(future);
    }

    /**
     * Creates {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl} and
     * {@link CompletionHandler} objects. All the notifications coming to the returned {@link CompletionHandler} will be
     * passed to the {@link FutureImpl} and {@link CompletionHandler} passed as parameters.
     *
     * @return {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl} and
     * {@link CompletionHandler} objects. All the notifications coming to the returned {@link CompletionHandler} will be
     * passed to the {@link FutureImpl} and {@link CompletionHandler} passed as parameters.
     */
    public static  CompletionHandler toCompletionHandler(final FutureImpl future, final CompletionHandler completionHandler) {
        return new CompletionHandlerAdapter<>(future, completionHandler);
    }

    /**
     * Creates {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl}. All the notifications
     * coming to the returned {@link CompletionHandler} will be adapted using {@link GenericAdapter} and passed to
     * the {@link FutureImpl}.
     *
     * @return {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl}. All the notifications
     * coming to the returned {@link CompletionHandler} will be adapted using {@link GenericAdapter} and passed to
     * the {@link FutureImpl}.
     */
    public static  CompletionHandler toAdaptedCompletionHandler(final FutureImpl future, final GenericAdapter adapter) {
        return toAdaptedCompletionHandler(future, null, adapter);
    }

    /**
     * Creates {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl} and
     * {@link CompletionHandler}. All the notifications coming to the returned {@link CompletionHandler} will be
     * adapted using {@link GenericAdapter} and passed to the {@link FutureImpl} and {@link CompletionHandler}.
     *
     * @return {@link CompletionHandler}, which may serve as a bridge for passed {@link FutureImpl} and
     * {@link CompletionHandler}. All the notifications coming to the returned {@link CompletionHandler} will be
     * adapted using {@link GenericAdapter} and passed to the {@link FutureImpl} and {@link CompletionHandler}.
     */
    public static  CompletionHandler toAdaptedCompletionHandler(final FutureImpl future, final CompletionHandler completionHandler,
            final GenericAdapter adapter) {
        return new CompletionHandlerAdapter<>(future, completionHandler, adapter);
    }

    private static final class FutureToCompletionHandler extends EmptyCompletionHandler {

        private final FutureImpl future;

        public FutureToCompletionHandler(FutureImpl future) {
            this.future = future;
        }

        @Override
        public void cancelled() {
            future.cancel(false);
        }

        @Override
        @SuppressWarnings("unchecked")
        public void completed(E result) {
            if (result instanceof Copyable) {
                result = (E) ((Copyable) result).copy();
            }
            future.result(result);
        }

        @Override
        public void failed(final Throwable throwable) {
            future.failure(throwable);
        }
    }
}