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

com.oracle.coherence.client.FutureStreamObserver Maven / Gradle / Ivy

/*
 * Copyright (c) 2020 Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */

package com.oracle.coherence.client;

import io.grpc.stub.StreamObserver;

import java.util.concurrent.CompletableFuture;

import java.util.function.BiFunction;

/**
 * A {@link StreamObserver} that completes a {@link CompletableFuture}.
 *
 * @param   the type of element observed
 * @param   the type of the {@link CompletableFuture}
 *
 * @author Jonathan Knight  2020.09.22
 * @since 20.06
 */
class FutureStreamObserver
        implements StreamObserver
    {
    // ----- constructors ---------------------------------------------------

    /**
     * Constructs a new {@code FutureStreamObserver}.
     *
     * @param future        the {@link CompletableFuture} to notify
     * @param initialResult the initial result
     * @param function      the function to call for each entry within the stream
     */
    FutureStreamObserver(CompletableFuture future, R initialResult, BiFunction function)
        {
        f_future = future;
        m_result = initialResult;
        f_function = function;
        }

    // ----- StreamObserver interface ---------------------------------------

    @Override
    public void onNext(T t)
        {
        if (!f_future.isDone())
            {
            m_result = f_function.apply(t, m_result);
            }
        }

    @Override
    public void onError(Throwable throwable)
        {
        if (!f_future.isDone())
            {
            f_future.completeExceptionally(throwable);
            }
        }

    @Override
    public void onCompleted()
        {
        if (!f_future.isDone())
            {
            f_future.complete(m_result);
            }
        }

    // ----- data members ---------------------------------------------------

    /**
     * The {@link CompletableFuture} to notify.
     */
    protected final CompletableFuture f_future;

    /**
     * Callback function for entry processing.
     */
    protected final BiFunction f_function;

    /**
     * The result after the callback is applied.
     */
    protected R m_result;
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy