io.reactivex.ObservableOperator Java Examples

The following examples show how to use io.reactivex.ObservableOperator. 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: OperatorsTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 6 votes vote down vote up
public static <T> ObservableOperator<T,T> doOnEmpty(Action action){
    return observer -> new DisposableObserver<T>() {
        boolean isEmpty = true;
        @Override
        public void	onNext(T value) {
            isEmpty = false;
            observer.onNext(value);
        }
        @Override
        public void onError(Throwable t) {
            observer.onError(t);
        }
        @Override
        public void onComplete() {
            if (isEmpty) {
                try {
                    action.run();
                } catch (Exception e) {
                    onError(e);
                    return;
                }
            }
            observer.onComplete();
        }
    };
}
 
Example #2
Source File: SqlBrite.java    From sqlbrite with Apache License 2.0 3 votes vote down vote up
/**
 * Creates an {@linkplain ObservableOperator operator} which transforms a query returning a
 * single row to a {@code T} using {@code mapper}. Use with {@link Observable#lift}.
 * <p>
 * It is an error for a query to pass through this operator with more than 1 row in its result
 * set. Use {@code LIMIT 1} on the underlying SQL query to prevent this. Result sets with 0 rows
 * emit {@code defaultValue}.
 * <p>
 * This operator emits {@code defaultValue} if {@code null} is returned from {@link #run()}.
 *
 * @param mapper Maps the current {@link Cursor} row to {@code T}. May not return null.
 * @param defaultValue Value returned if result set is empty
 */
@SuppressWarnings("ConstantConditions") // Public API contract.
@CheckResult @NonNull
public static <T> ObservableOperator<T, Query> mapToOneOrDefault(
    @NonNull Function<Cursor, T> mapper, @NonNull T defaultValue) {
  if (defaultValue == null) throw new NullPointerException("defaultValue == null");
  return new QueryToOneOperator<>(mapper, defaultValue);
}
 
Example #3
Source File: SqlBrite.java    From sqlbrite with Apache License 2.0 3 votes vote down vote up
/**
 * Creates an {@linkplain ObservableOperator operator} which transforms a query returning a
 * single row to a {@code Optional<T>} using {@code mapper}. Use with {@link Observable#lift}.
 * <p>
 * It is an error for a query to pass through this operator with more than 1 row in its result
 * set. Use {@code LIMIT 1} on the underlying SQL query to prevent this. Result sets with 0 rows
 * emit {@link Optional#empty() Optional.empty()}.
 * <p>
 * This operator ignores {@code null} cursors returned from {@link #run()}.
 *
 * @param mapper Maps the current {@link Cursor} row to {@code T}. May not return null.
 */
@RequiresApi(Build.VERSION_CODES.N) //
@CheckResult @NonNull //
public static <T> ObservableOperator<Optional<T>, Query> mapToOptional(
    @NonNull Function<Cursor, T> mapper) {
  return new QueryToOptionalOperator<>(mapper);
}
 
Example #4
Source File: SqlBrite.java    From sqlbrite with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an {@linkplain ObservableOperator operator} which transforms a query returning a
 * single row to a {@code T} using {@code mapper}. Use with {@link Observable#lift}.
 * <p>
 * It is an error for a query to pass through this operator with more than 1 row in its result
 * set. Use {@code LIMIT 1} on the underlying SQL query to prevent this. Result sets with 0 rows
 * do not emit an item.
 * <p>
 * This operator ignores {@code null} cursors returned from {@link #run()}.
 *
 * @param mapper Maps the current {@link Cursor} row to {@code T}. May not return null.
 */
@CheckResult @NonNull //
public static <T> ObservableOperator<T, Query> mapToOne(@NonNull Function<Cursor, T> mapper) {
  return new QueryToOneOperator<>(mapper, null);
}
 
Example #5
Source File: SqlBrite.java    From sqlbrite with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an {@linkplain ObservableOperator operator} which transforms a query to a
 * {@code List<T>} using {@code mapper}. Use with {@link Observable#lift}.
 * <p>
 * Be careful using this operator as it will always consume the entire cursor and create objects
 * for each row, every time this observable emits a new query. On tables whose queries update
 * frequently or very large result sets this can result in the creation of many objects.
 * <p>
 * This operator ignores {@code null} cursors returned from {@link #run()}.
 *
 * @param mapper Maps the current {@link Cursor} row to {@code T}. May not return null.
 */
@CheckResult @NonNull
public static <T> ObservableOperator<List<T>, Query> mapToList(
    @NonNull Function<Cursor, T> mapper) {
  return new QueryToListOperator<>(mapper);
}