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

org.finos.tracdap.common.async.Flows Maven / Gradle / Ivy

Go to download

TRAC D.A.P. data library, interfaces and core functionality for working with primary data

The newest version!
/*
 * Licensed to the Fintech Open Source Foundation (FINOS) under one or
 * more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * FINOS licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.finos.tracdap.common.async;

import io.netty.util.concurrent.OrderedEventExecutor;
import org.finos.tracdap.common.async.flow.*;

import java.util.*;
import java.util.concurrent.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;


public class Flows {

    public static 
    Flow.Publisher publish(List source) {

        return new SourcePublisher<>(source);
    }

    public static 
    Flow.Publisher publish(Stream source) {

        return new SourcePublisher<>(source);
    }

    public static 
    Flow.Publisher waitForSignal(Flow.Publisher target, CompletionStage signal) {

        return new DelayedPublisher<>(target, signal);
    }

    public static 
    Flow.Subscriber waitForSignal(Flow.Subscriber target, CompletionStage signal) {

        return new DelayedSubscriber<>(target, signal);
    }

    public static 
    Flow.Publisher map(Flow.Publisher source, Function mapping) {

        return new MapProcessor<>(mapping, source);
    }

    public static 
    Flow.Subscriber map(Flow.Subscriber target, Function mapping) {

        return new MapProcessor<>(mapping, target);
    }

    public static 
    CompletionStage fold(Flow.Publisher source, BiFunction func, U acc) {

        var result = new CompletableFuture();
        var fold = new ReduceProcessor<>(func, result, acc);
        source.subscribe(fold);

        return result;
    }

    public static 
    Flow.Processor hub(OrderedEventExecutor executor) {

        return new HubProcessor<>(executor);
    }

    public static 
    CompletionStage first(Flow.Publisher publisher) {

        var firstFuture = new CompletableFuture();

        var subscriber = new FutureFirstItemSubscriber<>(firstFuture);
        publisher.subscribe(subscriber);

        return firstFuture;
    }

}