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

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

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

import java.util.Optional;
import java.util.concurrent.Flow.Processor;
import net.pincette.function.SideEffect;

/**
 * This buffered processor delegates the stateful encoding of the incoming value stream to an
 * encoder. The reactive co-ordination is taken care of.
 *
 * @param  the incoming value type.
 * @param  the outgoing value type.
 * @author Werner Donn\u00e9
 * @since 3.0
 */
public class Encode extends Buffered {
  private final Encoder encoder;

  public Encode(final Encoder encoder) {
    super(1);
    this.encoder = encoder;
  }

  public static  Processor encode(final Encoder encoder) {
    return new Encode<>(encoder);
  }

  @Override
  protected void last() {
    Optional.of(encoder.complete()).filter(values -> !values.isEmpty()).ifPresent(this::addValues);
  }

  @Override
  protected boolean onNextAction(final T value) {
    return Optional.of(encoder.encode(value))
        .filter(values -> !values.isEmpty())
        .map(
            values ->
                SideEffect.run(
                        () -> {
                          addValues(values);
                          emit();
                        })
                    .andThenGet(() -> true))
        .orElse(false);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy