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

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

package net.pincette.rs;

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

/**
 * Emits the values until it receives one that matches the predicate, which is also emitted.
 *
 * @param  the value type.
 * @author Werner Donn\u00e9
 * @since 1.4
 */
public class Until extends PassThrough {
  private final Predicate predicate;
  private boolean done;

  public Until(final Predicate predicate) {
    this.predicate = predicate;
  }

  public static  Processor until(final Predicate predicate) {
    return new Until<>(predicate);
  }

  @Override
  public void onNext(final T value) {
    if (!done) {
      done = predicate.test(value);
      super.onNext(value);

      if (done) {
        complete();
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy