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

com.github.thorbenkuck.network.stream.EventStream Maven / Gradle / Ivy

The newest version!
package com.github.thorbenkuck.network.stream;

import java.util.function.Function;
import java.util.function.Supplier;

public interface EventStream {

    static  EventStream wrap(EventStream eventStream) {
        if (!(eventStream instanceof ManagedEventStream)) {
            throw new IllegalArgumentException("A ManagedEventStream is required!");
        }
        return new DelegatingEventStream((ManagedEventStream) eventStream);
    }

    static  EventStream readFrom(DataStream dataStream) {
        ManagedEventStream managedEventStream = ManagedEventStream.sequential();
        dataStream.subscribe(managedEventStream::push);
        return managedEventStream;
    }

    Subscription subscribe(Subscriber subscriber);

     EventStream pipe(Transformation transformation, Supplier> eventStreamFunction);

    default  EventStream pipe(Transformation transformation) {
        return pipe(transformation, DataStream::sequential);
    }

    default Subscription endIn(Sink sink) {
        return subscribe(sink::push);
    }

    default Subscription branchTo(DataStream eventStream) {
        return subscribe(eventStream::push);
    }

    default  Subscription connectTo(DataStream eventStream, Function function) {
        return subscribe(t -> eventStream.push(function.apply(t)));
    }

}