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

The following examples show how to use io.smallrye.mutiny.infrastructure.Infrastructure#onMultiCreation() . 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: MultiSwitchOnEmpty.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(Subscriber<? super T> downstream) {
    if (downstream == null) {
        throw new NullPointerException("The subscriber must not be `null`");
    }
    Supplier<Multi<? extends T>> actual = () -> {
        Publisher<? extends T> publisher;
        try {
            publisher = supplier.get();
        } catch (Throwable e) {
            return Infrastructure.onMultiCreation(new FailedMulti<>(e));
        }
        if (publisher == null) {
            return Infrastructure.onMultiCreation(new FailedMulti<>(new NullPointerException(SUPPLIER_PRODUCED_NULL)));
        }
        if (publisher instanceof Multi) {
            //noinspection unchecked
            return (Multi<? extends T>) publisher;
        } else {
            return Multi.createFrom().publisher(publisher);
        }
    };

    Multi<? extends T> deferred = Multi.createFrom().deferred(actual);
    Multi<T> op = Infrastructure.onMultiCreation(new MultiSwitchOnEmptyOp<>(upstream(), deferred));
    op.subscribe(downstream);
}
 
Example 2
Source File: MultiRetry.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
/**
 * Produces a {@link Multi} resubscribing to the current {@link Multi} at most {@code numberOfAttempts} time,
 * until it gets items followed by the completion event. 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 Multi} retrying at most {@code numberOfAttempts} times to subscribe to the current
 *         {@link Multi} until it gets an item. When the number of attempt is reached, the last failure is propagated.
 */
public Multi<T> atMost(long numberOfAttempts) {
    ParameterValidation.positive(numberOfAttempts, "numberOfAttempts");
    if (backOffConfigured) {
        Function<Multi<Throwable>, Publisher<Long>> whenStreamFactory = ExponentialBackoff
                .randomExponentialBackoffFunction(numberOfAttempts, initialBackOff, maxBackoff, jitter,
                        Infrastructure.getDefaultWorkerPool());
        return Infrastructure.onMultiCreation(
                new MultiRetryWhenOp<>(upstream, whenStreamFactory));
    } else {
        return Infrastructure.onMultiCreation(new MultiRetryOp<>(upstream, numberOfAttempts));
    }

}
 
Example 3
Source File: MultiBroadcaster.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
private static <T> Multi<T> createPublishWithSubscribersThreshold(Multi<T> upstream, int numberOfSubscribers,
        boolean cancelWhenNoOneIsListening, Duration delayAfterLastDeparture) {
    if (cancelWhenNoOneIsListening) {
        if (delayAfterLastDeparture != null) {
            return Infrastructure.onMultiCreation(
                    MultiPublishOp.create(upstream).referenceCount(numberOfSubscribers, delayAfterLastDeparture));
        } else {
            // the duration can be `null`, it will be validated if not `null`.
            return Infrastructure
                    .onMultiCreation(MultiPublishOp.create(upstream).referenceCount(numberOfSubscribers, null));
        }
    } else {
        return Infrastructure.onMultiCreation(MultiPublishOp.create(upstream).connectAfter(numberOfSubscribers));
    }
}
 
Example 4
Source File: AbstractMulti.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
@Override
public Multi<T> runSubscriptionOn(Executor executor) {
    return Infrastructure.onMultiCreation(new MultiSubscribeOnOp<>(this, executor));
}
 
Example 5
Source File: MultiTransform.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public Multi<T> bySkippingFirstItems(long number) {
    return Infrastructure
            .onMultiCreation(MultiTransformation.skipFirst(upstream, positiveOrZero(number, "number")));
}
 
Example 6
Source File: MultiTransformation.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public static <T> Multi<T> skipWhile(Multi<T> upstream, Predicate<? super T> predicate) {
    return Infrastructure.onMultiCreation(new MultiSkipUntilOp<>(upstream, predicate));
}
 
Example 7
Source File: MultiTransformation.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public static <T> Multi<T> skipForDuration(Multi<T> upstream, Duration duration) {
    Multi<Long> ticks = Multi.createFrom().ticks().startingAfter(duration).every(duration);
    return Infrastructure.onMultiCreation(new MultiSkipUntilPublisherOp<>(upstream, ticks));
}
 
Example 8
Source File: MultiTransform.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public Multi<T> byTakingItemsWhile(Predicate<? super T> predicate) {
    return Infrastructure.onMultiCreation(MultiTransformation.takeWhile(upstream, nonNull(predicate, "predicate")));
}
 
Example 9
Source File: MultiTransform.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public Multi<T> byTakingFirstItems(long number) {
    return Infrastructure
            .onMultiCreation(MultiTransformation.takeFirst(upstream, positiveOrZero(number, "number")));
}
 
Example 10
Source File: AbstractMulti.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
@Override
public Multi<T> cache() {
    return Infrastructure.onMultiCreation(new MultiCacheOp<>(this));
}
 
Example 11
Source File: MultiTransformation.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public static <T> Multi<T> dropRepetitions(Multi<T> upstream) {
    return Infrastructure.onMultiCreation(new MultiDistinctUntilChangedOp<>(upstream));
}
 
Example 12
Source File: MultiTransform.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public Multi<T> bySkippingLastItems(int number) {
    return Infrastructure.onMultiCreation(MultiTransformation.skipLast(upstream, positiveOrZero(number, "number")));
}
 
Example 13
Source File: MultiConcat.java    From smallrye-mutiny with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link Multi} concatenating the items emitted by the given {@link Multi multis} /
 * {@link Publisher publishers}..
 *
 * @param iterable the publishers, must not be empty, must not contain {@code null}, must not be {@code null}
 * @param <T> the type of item
 * @return the new {@link Multi} emitting the items from the given set of {@link Multi} using a concatenation
 */
public <T> Multi<T> streams(Iterable<? extends Publisher<T>> iterable) {
    List<Publisher<T>> list = new ArrayList<>();
    iterable.forEach(list::add);
    //noinspection unchecked
    return Infrastructure.onMultiCreation(new MultiConcatOp<>(collectFailures, list.toArray(new Publisher[0])));
}
 
Example 14
Source File: MultiOnFailure.java    From smallrye-mutiny with Apache License 2.0 3 votes vote down vote up
/**
 * Recovers from the received failure (matching the predicate if set) by using a item generated by the given
 * function. The function is called when the failure is received.
 * <p>
 * If the function throws an exception, a {@link io.smallrye.mutiny.CompositeException} containing both the received
 * failure and the thrown exception is propagated downstream.
 *
 * @param function the function providing the fallback item. Must not be {@code null}, must not return {@code null}.
 * @return the new {@link Multi} that would emit the produced item in case the upstream sends a failure.
 */
public Multi<T> recoverWithItem(Function<? super Throwable, ? extends T> function) {
    nonNull(function, "function");
    return Infrastructure.onMultiCreation(new MultiFlatMapOnFailure<>(upstream, predicate, failure -> {
        T newResult = function.apply(failure);
        return Multi.createFrom().item(newResult);
    }));
}
 
Example 15
Source File: MultiOnFailure.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Recovers from the received failure (matching the predicate if set) with another {@link Multi}. This {@code multi}
 * is produced by the given function. The function must not return {@code null}.
 * <p>
 * In case of failure, the downstream switches to the produced {@link Multi}.
 * <p>
 * If the function throws an exception, a {@link io.smallrye.mutiny.CompositeException} containing both the received
 * failure and the thrown exception is propagated downstream.
 *
 * @param function the function providing the fallback Multi. Must not be {@code null}, must not return {@code null}.
 * @return the new {@link Multi} that would emit events from the multi produced by the given function in case the
 *         upstream sends a failure.
 */
public Multi<T> recoverWithMulti(Function<? super Throwable, ? extends Multi<? extends T>> function) {
    return Infrastructure.onMultiCreation(new MultiFlatMapOnFailure<>(upstream, predicate,
            nonNull(function, "function")));
}
 
Example 16
Source File: MultiOnItem.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Produces a new {@link Multi} invoking the given function for each item emitted by the upstream {@link Multi}.
 * <p>
 * The function receives the received item as parameter, and can transform it. The returned object is sent
 * downstream as {@code item} event.
 * <p>
 *
 * @param mapper the mapper function, must not be {@code null}
 * @param <R> the type of item produced by the mapper function
 * @return the new {@link Multi}
 */
public <R> Multi<R> apply(Function<? super T, ? extends R> mapper) {
    return Infrastructure.onMultiCreation(new MultiMapOp<>(upstream, nonNull(mapper, "mapper")));
}
 
Example 17
Source File: MultiOnCompletion.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * When the upstream {@link Multi} completes, it continues with the events fired by a {@link Publisher} produces
 * with the given {@link Supplier}.
 *
 * @param supplier the supplier to use to produce the publisher, must not be {@code null}, must not return {@code null}s
 * @return the new {@link Uni}
 */
public Multi<T> switchTo(Supplier<Publisher<? extends T>> supplier) {
    return Infrastructure.onMultiCreation(new MultiSwitchOnCompletion<>(upstream, nonNull(supplier, "supplier")));
}
 
Example 18
Source File: MultiOnItem.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Produces a {@link Multi} that fires results coming from the reduction of the item emitted by this current
 * {@link Multi} by the passed {@code accumulator} reduction function. The produced multi emits the intermediate
 * results.
 * <p>
 * Unlike {@link #scan(Supplier, BiFunction)}, this operator doesn't take an initial value but takes the first
 * item emitted by this {@link Multi} as initial value.
 *
 * @param accumulator the reduction {@link BiFunction}, the resulting {@link Multi} emits the results of this method.
 *        The method is called for every item emitted by this Multi.
 * @return the produced {@link Multi}
 */
public Multi<T> scan(BinaryOperator<T> accumulator) {
    return Infrastructure.onMultiCreation(new MultiScanOp<>(upstream, accumulator));
}
 
Example 19
Source File: MultiCreate.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link Multi} that fires the completion event without having emitted any items.
 * An empty {@link Multi} does not fires a failure event either.
 *
 * @param <T> the virtual type of item
 * @return an empty {@link Multi}
 */
public <T> Multi<T> empty() {
    return Infrastructure.onMultiCreation(EmptyMulti.empty());
}
 
Example 20
Source File: MultiGroupIntoMultis.java    From smallrye-mutiny with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link Multi} that emits windows of items collected from the observed {@link Multi}.
 * <p>
 * The resulting {@link Multi} emits {@link Multi multis} every {@code skip} items, each containing {@code size}
 * items. Each emitted {@code Multi} is completed once the last item is emitted.
 * <p>
 * When the upstream {@link Multi} sends the completion event, the {@link Multi} emits the current Multi (and the
 * completion event) and sends the completion event. This last {@code Multi} may not contain {@code size} items.
 * If the upstream {@link Multi} sends the completion event before having emitted any event, the completion event is
 * propagated immediately.
 * <p>
 * If the upstream {@link Multi} sends a failure, the failure is propagated immediately.
 *
 * @param size the max number of item in each emitted {@link Multi}, must be positive
 * @param skip the number of items skipped before starting a new multi. If {@code skip} and {@code size} are equal,
 *        this operation is similar to {@link #of(int)}. Must be positive and non-0
 * @return a Multi emitting multis of at most {@code size} items from the upstream Multi.
 */
public Multi<Multi<T>> of(int size, int skip) {
    return Infrastructure
            .onMultiCreation(MultiCollector.multi(upstream, positive(size, "size"), positive(skip, "skip")));
}