io.baratine.pipe.Pipe Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
*
* This file is part of Baratine(TM)(TM)
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Baratine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Baratine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Baratine; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package io.baratine.pipe;
import java.util.function.Consumer;
import java.util.function.Function;
import io.baratine.pipe.Credits.OnAvailable;
import io.baratine.pipe.PipeStatic.PipeOutResultImpl;
import io.baratine.pipe.PipeStatic.ResultPipeInHandlerImpl;
import io.baratine.pipe.PipeStatic.ResultPipeInImpl;
import io.baratine.service.Cancel;
import io.baratine.service.Result;
/**
* {@code Pipe} sends a sequence of values from a source to a sink.
*/
public interface Pipe
{
public static final int PREFETCH_DEFAULT = 0;
public static final int PREFETCH_DISABLE = -1;
public static final int CREDIT_DISABLE = -1;
/**
* Supplies the next value.
*/
void next(T value);
/**
* Completes sending the values to the client and signals to the client
* that no more values are expected.
*/
void close();
/**
* Signals a failure.
*
* The pipe is closed on failure.
*/
void fail(Throwable exn);
/**
* Returns the credit sequence for the queue.
*/
default Credits credits()
{
throw new IllegalStateException(getClass().getName());
}
/**
* Subscriber callback to get the Credits for the pipe.
*/
default void credits(Credits credits)
{
}
/**
* True if the pipe has been closed or cancelled.
*/
default boolean isClosed()
{
return false;
}
public static PipeOutBuilder out(Result> result)
{
return new PipeOutResultImpl<>(result);
}
public static PipeOutBuilder out(Function,OnAvailable> onOk)
{
return new PipeOutResultImpl<>(onOk);
}
public static PipeOutBuilder out(OnAvailable flow)
{
return new PipeOutResultImpl<>(flow);
}
public static PipeInBuilder in(Pipe pipe)
{
return new ResultPipeInImpl<>(pipe);
}
public static PipeInBuilder in(Consumer next)
{
return new ResultPipeInImpl<>(next);
}
public static Pipe in(InHandler handler)
{
return new ResultPipeInHandlerImpl(handler);
}
public interface InHandler
{
void handle(T next, Throwable exn, boolean isCancel);
}
public interface PipeOutBuilder extends ResultPipeOut
{
PipeOutBuilder flow(OnAvailable flow);
PipeOutBuilder fail(Consumer onFail);
}
public interface PipeInBuilder extends ResultPipeIn
{
PipeInBuilder ok(Consumer onOkSubscription);
PipeInBuilder fail(Consumer onFail);
PipeInBuilder close(Runnable onClose);
PipeInBuilder credits(long initialCredit);
PipeInBuilder prefetch(int prefetch);
PipeInBuilder capacity(int size);
ResultPipeIn chain(Credits creditsNext);
}
/**
* {@code FlowIn} controls the pipe credits from the subscriber
*/
/*
public interface FlowIn extends Credits, Cancel
{
}
*/
/**
* {@code FlowOut} is a callback to wake the publisher when credits are
* available for the pipe.
*
* Called after the publisher would block, calculated as when the number
* of {@code OutPipe.next()} calls match a previous {@code OutPipe.credits()}.
*/
/*
public interface FlowOut
{
void ready(T pipe);
default void fail(Throwable exn)
{
}
default void cancel()
{
}
}
*/
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy