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

cz.o2.proxima.tools.groovy.WindowedStream Maven / Gradle / Ivy

/*
 * Copyright 2017-2022 O2 Czech Republic, a.s.
 *
 * 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 cz.o2.proxima.tools.groovy;

import cz.o2.proxima.repository.EntityDescriptor;
import cz.o2.proxima.storage.StreamElement;
import cz.o2.proxima.util.Pair;
import groovy.lang.Closure;
import groovy.transform.stc.ClosureParams;
import groovy.transform.stc.FromString;
import javax.annotation.Nullable;

/** A stream that is windowed. */
public interface WindowedStream extends Stream {

  /**
   * Reduce stream via given reducer.
   *
   * @param  key type
   * @param  value type
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of value
   * @param initialValue zero element
   * @param reducer the reduce function
   * @return reduced stream
   */
  default  WindowedStream> reduce(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor,
      V initialValue,
      @ClosureParams(value = FromString.class, options = "V, V") Closure reducer) {

    return reduce(null, keyExtractor, valueExtractor, initialValue, reducer);
  }

  /**
   * Reduce stream via given reducer.
   *
   * @param  key type
   * @param  value type
   * @param name name of the reduce operator
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of value
   * @param initialValue zero element
   * @param reducer the reduce function
   * @return reduced stream
   */
   WindowedStream> reduce(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor,
      V initialValue,
      @ClosureParams(value = FromString.class, options = "V, V") Closure reducer);

  /**
   * Reduce stream via given reducer.
   *
   * @param  key type
   * @param  value type
   * @param keyExtractor extractor of key
   * @param initialValue zero element
   * @param reducer the reduce function
   * @return reduced stream
   */
  default  WindowedStream> reduce(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      V initialValue,
      @ClosureParams(value = FromString.class, options = "V, V") Closure reducer) {

    return reduce(null, keyExtractor, initialValue, reducer);
  }

  /**
   * Reduce stream via given reducer.
   *
   * @param  key type
   * @param  value type
   * @param name name of the reduce operator
   * @param keyExtractor extractor of key
   * @param initialValue zero element
   * @param reducer the reduce function
   * @return reduced stream
   */
   WindowedStream> reduce(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      V initialValue,
      @ClosureParams(value = FromString.class, options = "V, V") Closure reducer);

  /**
   * Reduce stream to latest values only.
   *
   * @return reduced stream
   */
  default WindowedStream reduceToLatest() {
    return reduceToLatest(null);
  }

  /**
   * Reduce stream to latest values only.
   *
   * @param name name of the reduce operator
   * @return reduced stream
   */
  WindowedStream reduceToLatest(@Nullable String name);

  /**
   * Reduce stream with reduce function taking list of values.
   *
   * @param  key type
   * @param  value type
   * @param keyExtractor extractor of key
   * @param listReduce the reduce function taking list of elements
   * @return reduced stream
   */
  default  WindowedStream> groupReduce(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(
              value = FromString.class,
              options = {"Object, List"})
          Closure> listReduce) {

    return groupReduce(null, keyExtractor, listReduce);
  }

  /**
   * Reduce stream with reduce function taking list of values.
   *
   * @param  key type
   * @param  value type
   * @param name name of the group reduce operator
   * @param keyExtractor extractor of key
   * @param listReduce the reduce function taking list of elements
   * @return reduced stream
   */
   WindowedStream> groupReduce(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(
              value = FromString.class,
              options = {"Object, List"})
          Closure> listReduce);

  /**
   * Apply combine transform to stream.
   *
   * @param  key type
   * @param  value type
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of value
   * @param initial zero element
   * @param combine combine function
   * @return the new stream
   */
  default  WindowedStream> combine(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor,
      V initial,
      @ClosureParams(value = FromString.class, options = "V, V") Closure combine) {

    return combine(null, keyExtractor, valueExtractor, initial, combine);
  }

  /**
   * Apply combine transform to stream.
   *
   * @param  key type
   * @param  value type
   * @param name name of the combine operator
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of value
   * @param initial zero element
   * @param combine combine function
   * @return the new stream
   */
   WindowedStream> combine(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor,
      V initial,
      @ClosureParams(value = FromString.class, options = "V, V") Closure combine);

  /**
   * Apply combine transform to stream.
   *
   * @param  key type
   * @param keyExtractor extractor of key
   * @param initial zero element
   * @param combine combine function
   * @return the new stream
   */
  default  WindowedStream> combine(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      T initial,
      @ClosureParams(value = FromString.class, options = "T, T") Closure combine) {

    return combine(null, keyExtractor, initial, combine);
  }

  /**
   * Apply combine transform to stream.
   *
   * @param  key type
   * @param name name of the combine operator
   * @param keyExtractor extractor of key
   * @param initial zero element
   * @param combine combine function
   * @return the new stream
   */
   WindowedStream> combine(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      T initial,
      @ClosureParams(value = FromString.class, options = "T, T") Closure combine);

  /**
   * Count elements of stream by key.
   *
   * @param  key type
   * @param keyExtractor extractor of key
   * @return stream with elements counted
   */
  default  WindowedStream> countByKey(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor) {

    return countByKey(null, keyExtractor);
  }

  /**
   * Count elements of stream by key.
   *
   * @param  key type
   * @param name name of the countByKey operator
   * @param keyExtractor extractor of key
   * @return stream with elements counted
   */
   WindowedStream> countByKey(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor);

  /**
   * Average elements of stream.
   *
   * @param valueExtractor extractor of double value to be averaged
   * @return the stream with average values
   */
  default WindowedStream average(
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor) {

    return average(null, valueExtractor);
  }

  /**
   * Average elements of stream.
   *
   * @param name name of the average operator
   * @param valueExtractor extractor of double value to be averaged
   * @return the stream with average values
   */
  WindowedStream average(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor);

  /**
   * Average elements of stream by key.
   *
   * @param  key type
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of double value
   * @return stream with average values per key
   */
  default  WindowedStream> averageByKey(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor) {

    return averageByKey(null, keyExtractor, valueExtractor);
  }

  /**
   * Average elements of stream by key.
   *
   * @param  key type
   * @param name name of the averageByKey operator
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of double value
   * @return stream with average values per key
   */
   WindowedStream> averageByKey(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor);

  /**
   * Join with other stream.
   *
   * @param  type of join key
   * @param  type of other stream
   * @param right the right stream
   * @param leftKey extractor applied on left stream
   * @param rightKey extractor applied on right stream
   * @return joined stream
   */
  default  WindowedStream> join(
      WindowedStream right,
      @ClosureParams(value = FromString.class, options = "T") Closure leftKey,
      @ClosureParams(value = FromString.class, options = "T") Closure rightKey) {

    return join(null, right, leftKey, rightKey);
  }

  /**
   * Join with other stream.
   *
   * @param  type of join key
   * @param  type of other stream
   * @param name name of the join operator
   * @param right the right stream
   * @param leftKey extractor applied on left stream
   * @param rightKey extractor applied on right stream
   * @return joined stream
   */
   WindowedStream> join(
      @Nullable String name,
      WindowedStream right,
      @ClosureParams(value = FromString.class, options = "T") Closure leftKey,
      @ClosureParams(value = FromString.class, options = "T") Closure rightKey);

  /**
   * Left join with other stream.
   *
   * @param  type of join key
   * @param  type of other stream
   * @param right the right stream
   * @param leftKey extractor applied on left stream
   * @param rightKey extractor applied on right stream
   * @return joined stream
   */
  default  WindowedStream> leftJoin(
      WindowedStream right,
      @ClosureParams(value = FromString.class, options = "T") Closure leftKey,
      @ClosureParams(value = FromString.class, options = "T") Closure rightKey) {

    return leftJoin(null, right, leftKey, rightKey);
  }

  /**
   * Left join with other stream.
   *
   * @param  type of join key
   * @param  type of other stream
   * @param name name of the join operator
   * @param right the right stream
   * @param leftKey extractor applied on left stream
   * @param rightKey extractor applied on right stream
   * @return joined stream
   */
   WindowedStream> leftJoin(
      @Nullable String name,
      WindowedStream right,
      @ClosureParams(value = FromString.class, options = "T") Closure leftKey,
      @ClosureParams(value = FromString.class, options = "T") Closure rightKey);

  /**
   * Sort stream.
   *
   * @param compareFn comparison function
   * @return sorted stram
   */
  default WindowedStream sorted(
      @ClosureParams(value = FromString.class, options = "T") Closure compareFn) {

    return sorted(null, compareFn);
  }

  /**
   * Sort stream.
   *
   * @param name name of the sort operator
   * @param compareFn comparison function
   * @return sorted stram
   */
  WindowedStream sorted(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure compareFn);

  /**
   * Sort stream consisting of {@link Comparable}s.
   *
   * @return sorted stream
   */
  default WindowedStream> sorted() {
    return sorted((String) null);
  }

  /**
   * Sort stream consisting of {@link Comparable}s.
   *
   * @param name name of the sort operator
   * @return sorted stream
   */
  WindowedStream> sorted(@Nullable String name);

  /**
   * Count elements.
   *
   * @return stream with element counts
   */
  default WindowedStream count() {
    return count(null);
  }

  /**
   * Count elements.
   *
   * @param name name of the count operator
   * @return stream with element counts
   */
  WindowedStream count(@Nullable String name);

  /**
   * Sum elements.
   *
   * @param valueExtractor extractor of double value
   * @return stream with sums
   */
  default WindowedStream sum(
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor) {

    return sum(null, valueExtractor);
  }

  /**
   * Sum elements.
   *
   * @param name name of the sum operator
   * @param valueExtractor extractor of double value
   * @return stream with sums
   */
  WindowedStream sum(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor);

  /**
   * Sum elements by key.
   *
   * @param  type of key
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of double value
   * @return stream with sums per key
   */
  default  WindowedStream> sumByKey(
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor) {

    return sumByKey(null, keyExtractor, valueExtractor);
  }

  /**
   * Sum elements by key.
   *
   * @param  type of key
   * @param name name of the sumByKey operator
   * @param keyExtractor extractor of key
   * @param valueExtractor extractor of double value
   * @return stream with sums per key
   */
   WindowedStream> sumByKey(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure keyExtractor,
      @ClosureParams(value = FromString.class, options = "T") Closure valueExtractor);

  /**
   * Output distinct elements.
   *
   * @return stream with distinct elements
   */
  default WindowedStream distinct() {
    return distinct((String) null);
  }

  /**
   * Output distinct elements.
   *
   * @param name name of the distinct operator
   * @return stream with distinct elements
   */
  WindowedStream distinct(@Nullable String name);

  /**
   * Output distinct elements through given mapper.
   *
   * @param mapper map values by given function before comparison
   * @return distinct stream
   */
  default WindowedStream distinct(
      @ClosureParams(value = FromString.class, options = "T") Closure mapper) {

    return distinct(null, mapper);
  }

  /**
   * Output distinct elements through given mapper.
   *
   * @param name name of the distinct operator
   * @param mapper map values by given function before comparison
   * @return distinct stream
   */
  WindowedStream distinct(
      @Nullable String name,
      @ClosureParams(value = FromString.class, options = "T") Closure mapper);

  /**
   * Specify early emitting for windowed operations
   *
   * @param duration the duration (in processing time) of the early emitting
   * @return stream with early emitting specified
   */
  WindowedStream withEarlyEmitting(long duration);

  /**
   * Specify allowed lateness for windowed operations.
   *
   * @param lateness the allowed lateness
   * @return stream with allowed lateness specified
   */
  WindowedStream withAllowedLateness(long lateness);

  // overrides of Stream methods with fixed return types

  @Override
  default  WindowedStream flatMap(Closure> mapper) {
    return flatMap(null, mapper);
  }

  @Override
   WindowedStream flatMap(@Nullable String name, Closure> mapper);

  @Override
  default  WindowedStream map(Closure mapper) {
    return map(null, mapper);
  }

  @Override
   WindowedStream map(@Nullable String name, Closure mapper);

  @Override
  default WindowedStream filter(Closure predicate) {
    return filter(null, predicate);
  }

  @Override
  WindowedStream filter(@Nullable String name, Closure predicate);

  @Override
  default WindowedStream assignEventTime(Closure assigner) {
    return assignEventTime(null, assigner);
  }

  @Override
  WindowedStream assignEventTime(@Nullable String name, Closure assigner);

  @Override
  default WindowedStream> withWindow() {
    return withWindow(null);
  }

  @Override
  WindowedStream> withWindow(@Nullable String name);

  @Override
  default WindowedStream> withTimestamp() {
    return withTimestamp(null);
  }

  @Override
  WindowedStream> withTimestamp(@Nullable String name);

  @Override
  default WindowedStream asUnbounded() {
    return this;
  }

  @Override
   WindowedStream asStreamElements(
      RepositoryProvider repoProvider,
      EntityDescriptor entity,
      Closure keyExtractor,
      Closure attributeExtractor,
      Closure valueExtractor,
      Closure timeExtractor);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy