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

net.pincette.rs.AfterIfMany Maven / Gradle / Ivy

There is a newer version: 3.7.1
Show newest version
package net.pincette.rs;

import static net.pincette.util.Collections.list;

import java.util.concurrent.Flow.Processor;
import java.util.function.Supplier;

/**
 * A processor which emits a given value after all incoming values have been emitted, only when
 * there is more than one event.
 *
 * @param  the value type.
 * @author Werner Donné
 * @since 3.5
 */
public class AfterIfMany extends Buffered {
  private final Supplier value;
  private int seen;

  public AfterIfMany(final T value) {
    this(() -> value);
  }

  public AfterIfMany(final Supplier value) {
    super(1);
    this.value = value;
  }

  public static  Processor afterIfMany(final T value) {
    return new AfterIfMany<>(value);
  }

  public static  Processor afterIfMany(final Supplier value) {
    return new AfterIfMany<>(value);
  }

  @Override
  public void last() {
    if (seen > 1) {
      addValues(list(value.get()));
    }
  }

  @Override
  protected boolean onNextAction(final T value) {
    ++seen;
    addValues(list(value));
    emit();

    return true;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy