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

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

The newest version!
/*
 * Copyright (c) 2000, 2024, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * https://oss.oracle.com/licenses/upl.
 */

package com.oracle.coherence.grpc.client.common;

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
 */
public class FutureStreamObserver
        extends BaseFutureStreamObserver
    {
    // ----- 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
     */
    public FutureStreamObserver(CompletableFuture future, R initialResult, BiFunction function)
        {
        f_future   = future;
        m_result   = initialResult;
        f_function = function;
        }

    // ----- accessors ------------------------------------------------------

    @Override
    public CompletableFuture future()
        {
        return f_future;
        }

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

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

    @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