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

reactor.groovy.ext.StreamExtensions.groovy Maven / Gradle / Ivy

There is a newer version: 2.0.8.RELEASE
Show newest version
/*
 * Copyright (c) 2011-2015 Pivotal Software Inc., Inc. All Rights Reserved.
 *
 * 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 reactor.groovy.ext

import groovy.transform.CompileStatic
import org.reactivestreams.Processor
import org.reactivestreams.Publisher
import org.reactivestreams.Subscriber
import reactor.fn.BiFunction
import reactor.fn.Consumer
import reactor.fn.Function
import reactor.fn.Predicate
import reactor.fn.tuple.Tuple2
import reactor.io.codec.Codec
import reactor.rx.BiStreams
import reactor.rx.IOStreams
import reactor.rx.Promise
import reactor.rx.Stream
import reactor.rx.action.Control

/**
 * Glue for Groovy closures and operator overloading applied to Stream, Composable,
 * Promise and Deferred.
 * Also gives convenient Deferred handling by providing map/filter/consume operations
 *
 * @author Jon Brisbin
 * @author Stephane Maldini
 */
@CompileStatic
class StreamExtensions {

  /**
   * Alias
   */
  // PAIR Streams
  static  Stream> reduceByKey(final Publisher> selfType,
                                                 BiFunction accumulator) {
    BiStreams.reduceByKey(selfType, accumulator)
  }

  static  Stream> scanByKey(final Publisher> selfType,
                                               BiFunction accumulator) {
    BiStreams.scanByKey(selfType, accumulator)
  }

  // IO Streams
  static  Stream decode(final Publisher publisher, Codec codec) {
    IOStreams.decode(codec, publisher)
  }

  /**
   * Operator overloading
   */
  static  Stream mod(final Stream selfType, final BiFunction other) {
    selfType.reduce other
  }

  //Mapping
  static > E or(final Stream selfType, final E other) {
    selfType.broadcastTo(other)
  }

  static  Stream or(final Stream selfType, final Function other) {
    selfType.map other
  }

  static  Stream or(final Promise selfType, final Function other) {
    selfType.stream().map(other)
  }

  //Filtering
  static  Stream and(final Stream selfType, final Predicate other) {
    selfType.filter other
  }

  static  Stream and(final Promise selfType, final Predicate other) {
    selfType.stream().filter(other)
  }

  //Consuming
  static  Control leftShift(final Stream selfType, final Consumer other) {
    selfType.consume other
  }

  static  Promise leftShift(final Promise selfType, final Consumer other) {
    selfType.onSuccess other
  }

  //Consuming
  static > P leftShift(final P selfType, final T value) {
    selfType.onNext(value)
    selfType
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy