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

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

package net.pincette.rs;

import static net.pincette.util.Util.rethrow;
import static net.pincette.util.Util.tryToDoRethrow;

import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;
import net.pincette.function.ConsumerWithException;
import net.pincette.function.RunnableWithException;

/**
 * Provides constructors to which lambdas can be given. It requests values from the received
 * subscription one by one.
 *
 * @param  the parameterized type.
 * @author Werner Donn\u00e9
 * @since 1.0
 */
public class LambdaSubscriber implements Subscriber {
  private final RunnableWithException complete;
  private final ConsumerWithException error;
  private final ConsumerWithException next;
  private final ConsumerWithException subscribe;
  private Subscription subscription;

  public LambdaSubscriber(final ConsumerWithException next) {
    this(next, null, null, null);
  }

  public LambdaSubscriber(
      final ConsumerWithException next, final RunnableWithException complete) {
    this(next, complete, null, null);
  }

  public LambdaSubscriber(
      final ConsumerWithException next,
      final RunnableWithException complete,
      final ConsumerWithException error) {
    this(next, complete, error, null);
  }

  public LambdaSubscriber(
      final ConsumerWithException next,
      final RunnableWithException complete,
      final ConsumerWithException error,
      final ConsumerWithException subscribe) {
    this.next = next;
    this.complete = complete;
    this.error = error;
    this.subscribe = subscribe;
  }

  public static  Subscriber lambdaSubscriber(final ConsumerWithException next) {
    return new LambdaSubscriber<>(next);
  }

  public static  Subscriber lambdaSubscriber(
      final ConsumerWithException next, final RunnableWithException complete) {
    return new LambdaSubscriber<>(next, complete);
  }

  public static  Subscriber lambdaSubscriber(
      final ConsumerWithException next,
      final RunnableWithException complete,
      final ConsumerWithException error) {
    return new LambdaSubscriber<>(next, complete, error);
  }

  public static  Subscriber lambdaSubscriber(
      final ConsumerWithException next,
      final RunnableWithException complete,
      final ConsumerWithException error,
      final ConsumerWithException subscribe) {
    return new LambdaSubscriber<>(next, complete, error, subscribe);
  }

  public void onComplete() {
    if (complete != null) {
      tryToDoRethrow(complete);
    }
  }

  public void onError(final Throwable t) {
    if (t == null) {
      throw new NullPointerException("Can't throw null.");
    }

    if (error != null) {
      tryToDoRethrow(() -> error.accept(t));
    } else {
      rethrow(t);
    }
  }

  public void onNext(final T o) {
    if (o == null) {
      throw new NullPointerException("Can't emit null.");
    }

    if (subscription != null) {
      if (next != null) {
        tryToDoRethrow(() -> next.accept(o));
      }

      subscription.request(1);
    }
  }

  public void onSubscribe(final Subscription s) {
    if (s == null) {
      throw new NullPointerException("A subscription can't be null.");
    }

    if (this.subscription != null) {
      s.cancel();
    } else {
      if (subscribe != null) {
        tryToDoRethrow(() -> subscribe.accept(s));
      }

      subscription = s;
      s.request(1);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy