Java Code Examples for io.smallrye.mutiny.infrastructure.Infrastructure#onUniCreation()

The following examples show how to use io.smallrye.mutiny.infrastructure.Infrastructure#onUniCreation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UniOnNull.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
/**
 * If the current {@link Uni} fires {@code null} as item, the produced {@link Uni} emits the events produced
 * by an {@link Uni} supplied using the passed {@link Supplier}
 *
 * @param supplier the supplier to use to produce the uni, must not be {@code null}, must not return {@code null}s
 * @return the new {@link Uni}
 */
public Uni<T> switchTo(Supplier<Uni<? extends T>> supplier) {
    nonNull(supplier, "supplier");

    Uni<T> uni = upstream.onItem().produceUni(res -> {
        if (res != null) {
            return Uni.createFrom().item(res);
        } else {
            Uni<? extends T> produced;
            try {
                produced = supplier.get();
            } catch (Throwable e) {
                return Uni.createFrom().failure(e);
            }

            if (produced == null) {
                return Uni.createFrom().failure(new NullPointerException(SUPPLIER_PRODUCED_NULL));
            } else {
                return produced;
            }
        }
    });
    return Infrastructure.onUniCreation(uni);
}
 
Example 2
Source File: UniOnNull.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
/**
 * If the current {@link Uni} fires {@code null} as item, the produced {@link Uni} emits a failure produced
 * using the given {@link Supplier}.
 *
 * @param supplier the supplier to produce the failure, must not be {@code null}, must not produce {@code null}
 * @return the new {@link Uni}
 */
public Uni<T> failWith(Supplier<? extends Throwable> supplier) {
    nonNull(supplier, "supplier");

    return Infrastructure.onUniCreation(upstream.onItem().produceUni((item, emitter) -> {
        if (item != null) {
            emitter.complete(item);
            return;
        }
        Throwable throwable;
        try {
            throwable = supplier.get();
        } catch (Throwable e) {
            emitter.fail(e);
            return;
        }

        if (throwable == null) {
            emitter.fail(new NullPointerException(SUPPLIER_PRODUCED_NULL));
        } else {
            emitter.fail(throwable);
        }
    }));
}
 
Example 3
Source File: UniOnNull.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
/**
 * Provides a default item if the current {@link Uni} fires {@code null} as item.
 * The new item is supplied by the given {@link Supplier}.
 *
 * @param supplier the supplier to produce the new item, must not be {@code null}, must not produce {@code null}
 * @return the new {@link Uni}
 */
public Uni<T> continueWith(Supplier<? extends T> supplier) {
    nonNull(supplier, "supplier");
    return Infrastructure.onUniCreation(upstream.onItem().apply(res -> {
        if (res != null) {
            return res;
        }
        T outcome = supplier.get();
        if (outcome == null) {
            throw new NullPointerException(SUPPLIER_PRODUCED_NULL);
        }
        return outcome;
    }));
}
 
Example 4
Source File: AbstractUni.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
@Override
public <T2> Uni<Tuple2<T, T2>> and(Uni<T2> other) {
    return Infrastructure.onUniCreation(
            new UniAndGroup<>(this).uni(ParameterValidation.nonNull(other, "other")).asTuple());
}
 
Example 5
Source File: UniOnTimeout.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public Uni<T> failWith(Supplier<? extends Throwable> supplier) {
    validate(timeout, "timeout");
    nonNull(supplier, "supplier");
    return Infrastructure.onUniCreation(new UniFailOnTimeout<>(failure, timeout, supplier, executor));
}
 
Example 6
Source File: UniOnTimeout.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public Uni<T> fail() {
    return Infrastructure.onUniCreation(failWith(TimeoutException::new));
}
 
Example 7
Source File: AbstractUni.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
@Override
public Uni<T> emitOn(Executor executor) {
    return Infrastructure.onUniCreation(
            new UniEmitOn<>(this, ParameterValidation.nonNull(executor, "executor")));
}
 
Example 8
Source File: UniAndGroupIterable.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public <O> Uni<O> combinedWith(Function<List<?>, O> function) {
    return Infrastructure
            .onUniCreation(new UniAndCombination<>(source, unis, nonNull(function, "function"), collectFailures));
}
 
Example 9
Source File: UniRetry.java    From smallrye-mutiny with Apache License 2.0 3 votes vote down vote up
/**
 * Produces a {@link Uni} resubscribing to the current {@link Uni} at most {@code numberOfAttempts} time, until it
 * gets an item (potentially {@code null}). On every failure, it re-subscribes.
 * <p>
 * If the number of attempt is reached, the last failure is propagated.
 *
 * @param numberOfAttempts the number of attempt, must be greater than zero
 * @return a new {@link Uni} retrying at most {@code numberOfAttempts} times to subscribe to the current {@link Uni}
 *         until it gets an item. When the number of attempt is reached, the last failure is propagated. If the back-off
 *         has been configured, a delay is introduced between the attempts.
 */
public Uni<T> atMost(long numberOfAttempts) {
    if (!backOffConfigured) {
        return Infrastructure.onUniCreation(new UniRetryAtMost<>(upstream, predicate, numberOfAttempts));
    } else {
        Function<Multi<Throwable>, Publisher<Long>> factory = ExponentialBackoff
                .randomExponentialBackoffFunction(numberOfAttempts,
                        initialBackOffDuration, maxBackoffDuration, jitter, Infrastructure.getDefaultWorkerPool());
        return upstream.toMulti().onFailure().retry().when(factory).toUni();
    }
}
 
Example 10
Source File: UniOnEvent.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Attaches an action executed when the {@link Uni} has received a {@link UniSubscription} from upstream.
 * The downstream does not have received the subscription yet. It will be done once the action completes.
 * <p>
 * This method is not intended to cancel the subscription. It's the responsibility of the subscriber to do so.
 *
 * @param consumer the callback, must not be {@code null}
 * @return a new {@link Uni}
 */
public Uni<T> subscribed(Consumer<? super UniSubscription> consumer) {
    return Infrastructure.onUniCreation(new UniOnSubscription<>(upstream, nonNull(consumer, "consumer")));
}
 
Example 11
Source File: UniOnEvent.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Attaches an action executed when a subscription is cancelled.
 * The upstream is not cancelled yet, but will when the callback completes.
 *
 * @param runnable the callback, must not be {@code null}
 * @return a new {@link Uni}
 */
public Uni<T> cancellation(Runnable runnable) {
    return Infrastructure.onUniCreation(new UniOnCancellation<>(upstream, nonNull(runnable, "runnable")));
}
 
Example 12
Source File: UniOnItem.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Transforms the received item asynchronously, forwarding the events emitted by another {@link CompletionStage}
 * produced by the given {@code mapper}.
 * <p>
 * The mapper is called with the item event of the current {@link Uni} and produces an {@link CompletionStage},
 * possibly using another type of item ({@code R}). The events fired by produced {@link CompletionStage} are
 * forwarded to the {@link Uni} returned by this method.
 * <p>
 * *
 *
 * @param mapper the function called with the item of this {@link Uni} and producing the {@link CompletionStage},
 *        must not be {@code null}, must not return {@code null}.
 * @param <R> the type of item
 * @return a new {@link Uni} that would fire events from the uni produced by the mapper function, possibly
 *         in an asynchronous manner.
 */
public <R> Uni<R> produceCompletionStage(Function<? super T, ? extends CompletionStage<? extends R>> mapper) {
    return Infrastructure.onUniCreation(new UniFlatMapCompletionStageOnItem<>(upstream, mapper));
}
 
Example 13
Source File: UniOnEvent.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Attaches an action that is executed when the {@link Uni} emits an item or a failure or when the subscriber
 * cancels the subscription. Unlike {@link #termination(Functions.TriConsumer)}, the callback does not receive
 * the item, failure or cancellation.
 *
 * @param action the action to run, must not be {@code null}
 * @return the new {@link Uni}
 */
public Uni<T> termination(Runnable action) {
    Runnable runnable = nonNull(action, "action");
    return Infrastructure.onUniCreation(new UniOnTermination<>(upstream, (i, f, c) -> runnable.run()));
}
 
Example 14
Source File: UniOnFailure.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Produces a new {@link Uni} invoking the given callback when this {@link Uni} emits a failure (matching the
 * predicate if set).
 * <p>
 * If the callback throws an exception, a {@link io.smallrye.mutiny.CompositeException} is propagated downstream.
 * This exception is composed by the received failure and the thrown exception.
 *
 * @param callback the callback, must not be {@code null}
 * @return the new {@link Uni}
 */
public Uni<T> invoke(Consumer<Throwable> callback) {
    return Infrastructure.onUniCreation(
            new UniOnItemConsume<>(upstream, null, nonNull(callback, "callback"), predicate));
}
 
Example 15
Source File: UniCreate.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link Uni} deferring the logic to the given consumer. The consumer can be used with callback-based
 * APIs to fire at most one item (potentially {@code null}), or a failure event.
 * <p>
 * Using this method, you can produce a {@link Uni} based on listener or callbacks APIs. You register the listener
 * in the consumer and emits the item / failure events when the listener is invoked. Don't forget to unregister
 * the listener on cancellation.
 * <p>
 * Note that the emitter only forwards the first event, subsequent events are dropped.
 * <p>
 * If the consumer throws an exception, a failure event with the exception is fired if the first event was already
 * fired.
 *
 * @param consumer callback receiving the {@link UniEmitter} and events downstream. The callback is
 *        called for each subscriber (at subscription time). Must not be {@code null}
 * @param <T> the type of item
 * @return the produced {@link Uni}
 */
public <T> Uni<T> emitter(Consumer<UniEmitter<? super T>> consumer) {
    Consumer<UniEmitter<? super T>> actual = ParameterValidation.nonNull(consumer, "consumer");
    return Infrastructure.onUniCreation(new UniCreateWithEmitter<>(actual));
}
 
Example 16
Source File: UniCreate.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link Uni} that {@link Supplier#get supplies} an {@link Uni} to subscribe to for each
 * {@link UniSubscriber}. The supplier is called at subscription time.
 * <p>
 * In practice, it defers the {@link Uni} creation at subscription time and allows each subscriber to get different
 * {@link Uni}. So, it does not create the {@link Uni} until an {@link UniSubscriber subscriber} subscribes, and
 * creates a fresh {@link Uni} for each subscriber.
 * <p>
 * Unlike {@link #item(Supplier)}, the supplier produces an {@link Uni} (and not an item).
 * <p>
 * If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
 * {@code null}, a failure event containing a {@link NullPointerException} is fired.
 *
 * @param supplier the supplier, must not be {@code null}, must not produce {@code null}
 * @param <T> the type of item
 * @return the produced {@link Uni}
 */
public <T> Uni<T> deferred(Supplier<? extends Uni<? extends T>> supplier) {
    Supplier<? extends Uni<? extends T>> actual = ParameterValidation.nonNull(supplier, "supplier");
    return Infrastructure.onUniCreation(new UniCreateFromDeferredSupplier<>(actual));
}
 
Example 17
Source File: UniOnItemDelay.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Delays the item emission by a specific duration.
 *
 * @param duration the duration of the delay, must not be {@code null}, must be strictly positive.
 * @return the produced {@link Uni}
 */
public Uni<T> by(Duration duration) {
    return Infrastructure.onUniCreation(new UniDelayOnItem<>(upstream, duration, executor));
}
 
Example 18
Source File: UniOnItem.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Produces a new {@link Uni} invoking the given callback when the {@code item} event is fired. Note that the
 * item can be {@code null}.
 *
 * @param callback the callback, must not be {@code null}
 * @return the new {@link Uni}
 */
public Uni<T> invoke(Consumer<? super T> callback) {
    return Infrastructure.onUniCreation(
            new UniOnItemConsume<>(upstream, nonNull(callback, "callback"), null, null));
}
 
Example 19
Source File: UniOnItem.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Produces a new {@link Uni} invoking the given function when the current {@link Uni} fires the {@code item} event.
 * The function receives the item as parameter, and can transform it. The returned object is sent downstream
 * as {@code item}.
 * <p>
 * For asynchronous composition, see {@link #produceUni(Function)}.
 *
 * @param mapper the mapper function, must not be {@code null}
 * @param <R> the type of Uni item
 * @return the new {@link Uni}
 */
public <R> Uni<R> apply(Function<? super T, ? extends R> mapper) {
    return Infrastructure.onUniCreation(new UniOnItemMap<>(upstream, mapper));
}
 
Example 20
Source File: UniOnItemOrFailure.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Transforms the received item or failure asynchronously, forwarding the events emitted by another {@link Uni}
 * produced by the given {@code mapper}.
 * <p>
 * Note that the item can be {@code null}, so detecting failures must be done by checking whether the {@code failure}
 * parameter is {@code null}.
 * <p>
 * The mapper is called with the item produced by the upstream or the propagated failure. It produces an {@link Uni},
 * possibly using another type of item ({@code R}). It can be used to recover from a failure. The events fired by
 * the produced {@link Uni} are forwarded to the {@link Uni} returned by this method.
 * <p>
 * This operation is generally named {@code flatMap}.
 *
 * @param mapper the function called with the item and failure sent by the upstream {@link Uni} to produce another
 *        {@link Uni}, must not be {@code null}, must not return {@code null}.
 * @param <R> the type of item
 * @return a new {@link Uni} that would fire events from the uni produced by the mapper function, possibly
 *         in an asynchronous manner.
 */
public <R> Uni<R> produceUni(BiFunction<? super T, Throwable, ? extends Uni<? extends R>> mapper) {
    return Infrastructure.onUniCreation(new UniOnItemOrFailureFlatMap<>(upstream, mapper));
}