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

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

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

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

/**
 * A processor which emits a given value between the incoming value stream.
 *
 * @param  the value type.
 * @author Werner Donné
 * @since 1.0
 */
public class Separator extends Buffered {
  private final Supplier value;
  private boolean first = true;

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

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

  public static  Processor separator(final T value) {
    return new Separator<>(value);
  }

  public static  Processor separator(final Supplier value) {
    return new Separator<>(value);
  }

  @Override
  protected boolean onNextAction(final T value) {
    final List values = new ArrayList<>();

    if (first) {
      first = false;
    } else {
      values.add(this.value.get());
    }

    values.add(value);
    addValues(values);
    emit();

    return true;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy