hu.akarnokd.rxjava2.operators.ObservableTransformers Maven / Gradle / Ivy
Show all versions of rxjava2-extensions Show documentation
/*
* Copyright 2016-2018 David Karnok
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hu.akarnokd.rxjava2.operators;
import io.reactivex.*;
import io.reactivex.annotations.*;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.ObjectHelper;
/**
* Additional operators in the form of {@link ObservableTransformer},
* use {@link Observable#compose(ObservableTransformer)}
* to apply the operators to an existing sequence.
*
* @since 0.18.2
*/
public final class ObservableTransformers {
/**
* Utility class.
*/
private ObservableTransformers() {
throw new IllegalStateException("No instances!");
}
/**
* Returns the first index of an element that matches a predicate or -1L if no elements match.
* @param the upstream element type
* @param predicate the predicate called to test each item, returning true will
* stop the sequence and return the current item index
* @return the new ObservableTransformer instance
*
* @since 0.18.2
*/
@SchedulerSupport(SchedulerSupport.NONE)
@CheckReturnValue
@NonNull
public static ObservableTransformer indexOf(@NonNull Predicate super T> predicate) {
ObjectHelper.requireNonNull(predicate, "predicate is null");
return new ObservableIndexOf(null, predicate);
}
/**
* Schedules the event emission on a {@link Scheduler} and drops upstream values while
* the {@code onNext} with the current item is executing on that scheduler.
*
* Errors are delayed until all items that weren't dropped have been delivered.
* @param the element type
* @param scheduler the scheduler to use for emitting events on
* @return the new ObservableTransformer instance
* @see #observeOnLatest(Scheduler)
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
@CheckReturnValue
@NonNull
public static ObservableTransformer observeOnDrop(@NonNull Scheduler scheduler) {
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return new ObservableObserveOnDrop(null, scheduler);
}
/**
* Schedules the event emission on a {@link Scheduler} and keeps the latest upstream item
* while the downstream's {@code onNext} is executing so that it will resume
* with that latest value.
*
* Errors are delayed until the very last item has been delivered.
* @param the element type
* @param scheduler the scheduler to use for emitting events on
* @return the new ObservableTransformer instance
* @see #observeOnLatest(Scheduler)
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
@CheckReturnValue
@NonNull
public static ObservableTransformer observeOnLatest(@NonNull Scheduler scheduler) {
ObjectHelper.requireNonNull(scheduler, "scheduler is null");
return new ObservableObserveOnLatest(null, scheduler);
}
/**
* FlatMap only one {@link ObservableSource} at a time and ignore upstream values until it terminates.
*
* Errors are delayed until both the upstream and the active inner {@code ObservableSource} terminate.
* @param the upstream value type
* @param the output type
* @param mapper the function that takes an upstream item and returns a {@link ObservableSource}
* to be run exclusively until it finishes
* @return the new ObservableTransformer instance
* @since 0.19.0
*/
@SchedulerSupport(SchedulerSupport.NONE)
@CheckReturnValue
@NonNull
public static ObservableTransformer flatMapDrop(Function super T, ? extends ObservableSource extends R>> mapper) {
ObjectHelper.requireNonNull(mapper, "mapper is null");
return new ObservableFlatMapDrop(null, mapper);
}
/**
* FlatMap only one {@link ObservableSource} at a time and keep the latest upstream value until it terminates
* and resume with the {@code ObservableSource} mapped for that latest upstream value.
*
* Errors are delayed until both the upstream and the active inner {@code ObservableSource} terminate.
* @param the upstream value type
* @param the output type
* @param mapper the function that takes an upstream item and returns a {@link ObservableSource}
* to be run exclusively until it finishes
* @return the new ObservableTransformer instance
* @since 0.19.0
*/
@SchedulerSupport(SchedulerSupport.NONE)
@CheckReturnValue
@NonNull
public static ObservableTransformer flatMapLatest(Function super T, ? extends ObservableSource extends R>> mapper) {
ObjectHelper.requireNonNull(mapper, "mapper is null");
return new ObservableFlatMapLatest(null, mapper);
}
/**
* Allows an upstream error to jump over an inner transformation and is
* then reapplied once the inner transformation's returned Flowable terminates.
* @param the upstream value type
* @param the downstream value type
* @param transformer the transformation applied to the flow on a per-Subscriber basis
* @return the new FlowableTransformer instance
* @since 0.19.1
*/
public static ObservableTransformer errorJump(ObservableTransformer transformer) {
ObjectHelper.requireNonNull(transformer, "transformer");
return new ObservableErrorJump(null, transformer);
}
}