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

ratpack.stream.TransformablePublisher Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed 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 ratpack.stream;

import org.reactivestreams.Publisher;
import ratpack.exec.ExecControl;
import ratpack.exec.Promise;
import ratpack.func.Action;
import ratpack.func.Function;

/**
 * A wrapper over a {@link Publisher} that makes it more convenient to chain transformations of different kinds.
 * 

* Note that this type implements the publisher interface, so behaves just like the publisher that it is wrapping with respect to the {@link Publisher#subscribe(org.reactivestreams.Subscriber)} method. * * @param the type of item emitted by this publisher */ public interface TransformablePublisher extends Publisher { /** * See {@link ratpack.stream.Streams#map(Publisher, Function)}. * * @param function the transformation * @param the type of transformed item * @return the transformed publisher */ default TransformablePublisher map(Function function) { return Streams.map(this, function); } /** * See {@link ratpack.stream.Streams#flatMap(Publisher, Function)}. * * @param function the transformation * @param the type of transformed item * @return the transformed publisher */ default TransformablePublisher flatMap(Function> function) { return Streams.flatMap(this, function); } /** * See {@link ratpack.stream.Streams#buffer(Publisher)}. * * @return a buffering publisher */ default TransformablePublisher buffer() { return Streams.buffer(this); } /** * See {@link ratpack.stream.Streams#gate(Publisher, Action)}. * * @param valveReceiver an action that receives a runnable “valve” that when run allows request to start flowing upstream * @return a publisher that is logically equivalent to the given publisher as far as subscribers are concerned */ default TransformablePublisher gate(Action valveReceiver) { return Streams.gate(this, valveReceiver); } /** * See {@link ratpack.stream.Streams#wiretap(Publisher, Action)}. * * @param listener the listener for emitted items * @return a publisher that is logically equivalent to the given publisher as far as subscribers are concerned */ default TransformablePublisher wiretap(Action> listener) { return Streams.wiretap(this, listener); } /** * See {@link ratpack.stream.Streams#multicast(Publisher)}. * * @return a publisher that respects back pressure for each of its subscribers */ default TransformablePublisher multicast() { return Streams.multicast(this); } /** * See {@link ratpack.stream.Streams#toPromise(Publisher)}. * * @return a promise for this publisher's single item */ default Promise toPromise() { return Streams.toPromise(this); } /** * See {@link ratpack.stream.Streams#toPromise(ExecControl, Publisher)}. * * @param execControl the exec control to create the promise from * @return a promise for this publisher's single item */ default Promise toPromise(ExecControl execControl) { return Streams.toPromise(execControl, this); } /** * Convenience method to allow a non Ratpack publisher transform method to be hooked in. *

* This transformable publisher will be given to the function, that should return a new publisher. * The returned publisher will then be wrapped in a transformable wrapper which will be returned by this method. * * @param transformer a publisher transformer * @param the type of transformed item * @return a publisher that respects back pressure for each of its subscribers */ default TransformablePublisher transform(java.util.function.Function, ? extends Publisher> transformer) { return Streams.transformable(transformer.apply(this)); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy