io.reactivex.Notification Java Examples

The following examples show how to use io.reactivex.Notification. 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: TransactedCallableBuilder.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static <T> Flowable<Tx<T>> inTransaction(CallableBuilder b,
        Function<Single<Connection>, Flowable<Notification<T>>> f) {
    return Flowable.defer(() -> {
        AtomicReference<Connection> con = new AtomicReference<Connection>();
        // set the atomic reference when transactedConnection emits
        Single<Connection> transactedConnection = b.connection //
                .map(c -> Util.toTransactedConnection(con, c));
        return f.apply(transactedConnection) //
                .<Tx<T>>flatMap(n -> Tx.toTx(n, con.get(), b.db)) //
                .doOnNext(tx -> {
                    if (tx.isComplete()) {
                        ((TxImpl<T>) tx).connection().commit();
                    }
                });
    });
}
 
Example #2
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static Flowable<Notification<CallableResultSetN>> createWithNResultSets(Connection con, String sql,
        Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        List<Function<? super ResultSet, ?>> functions, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSetN>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        List<Flowable<?>> flowables = Lists.newArrayList();
                        int i = 0;
                        do {
                            Function<? super ResultSet, ?> f = functions.get(i);
                            flowables.add(createFlowable(stmt, f));
                            i++;
                        } while (stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
                        return Single.just(new CallableResultSetN(outputValues, flowables)).toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example #3
Source File: WindowIfChangedTest.java    From RxWindowIfChanged with Apache License 2.0 6 votes vote down vote up
@Test public void completeCompletesInner() {
  Observable<Message> messages = Observable.just(new Message("Bob", "Hello"));
  final AtomicInteger seen = new AtomicInteger();
  WindowIfChanged.create(messages, userSelector)
      .switchMap(
          new Function<GroupedObservable<String, Message>, Observable<Notification<String>>>() {
            @Override public Observable<Notification<String>> apply(
                GroupedObservable<String, Message> group) {
              final int count = seen.incrementAndGet();
              return group.map(new Function<Message, String>() {
                @Override public String apply(Message message) throws Exception {
                  return count + " " + message;
                }
              }).materialize();
            }
          })
      .test()
      .assertValues( //
          Notification.createOnNext("1 Bob Hello"), //
          Notification.<String>createOnComplete()) //
      .assertComplete();
}
 
Example #4
Source File: WindowIfChangedTest.java    From RxWindowIfChanged with Apache License 2.0 6 votes vote down vote up
@Test public void errorCompletesInner() {
  RuntimeException error = new RuntimeException("boom!");
  Observable<Message> messages = Observable.just( //
      Notification.createOnNext(new Message("Bob", "Hello")),
      Notification.createOnError(error)
  ).dematerialize();
  final AtomicInteger seen = new AtomicInteger();
  WindowIfChanged.create(messages, userSelector)
      .switchMap(
          new Function<GroupedObservable<String, Message>, Observable<Notification<String>>>() {
            @Override public Observable<Notification<String>> apply(
                GroupedObservable<String, Message> group) {
              final int count = seen.incrementAndGet();
              return group.map(new Function<Message, String>() {
                @Override public String apply(Message message) throws Exception {
                  return count + " " + message;
                }
              }).materialize();
            }
          })
      .test()
      .assertValues( //
          Notification.createOnNext("1 Bob Hello"), //
          Notification.<String>createOnComplete()) //
      .assertError(error);
}
 
Example #5
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static <T1, T2, T3> Flowable<Notification<CallableResultSet3<T1, T2, T3>>> createWithThreeResultSets(
        Connection con, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Function<? super ResultSet, ? extends T1> f1,
        Function<? super ResultSet, ? extends T2> f2, Function<? super ResultSet, ? extends T3> f3, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSet3<T1, T2, T3>>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        final Flowable<T1> flowable1 = createFlowable(stmt, f1);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T2> flowable2 = createFlowable(stmt, f2);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T3> flowable3 = createFlowable(stmt, f3);
                        return Single.just(
                                new CallableResultSet3<T1, T2, T3>(outputValues, flowable1, flowable2, flowable3))
                                .toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example #6
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
static Flowable<Notification<TupleN<Object>>> createWithNParameters( //
        Single<Connection> connection, //
        String sql, //
        Flowable<List<Object>> parameterGroups, //
        List<ParameterPlaceholder> parameterPlaceholders, //
        List<Class<?>> outClasses) {
    return connection //
            .toFlowable() //
            .flatMap( //
                    con -> createWithParameters( //
                            con, //
                            sql, //
                            parameterGroups, //
                            parameterPlaceholders, //
                            (stmt, parameters) -> createWithNParameters(stmt, parameters, parameterPlaceholders,
                                    outClasses)));
}
 
Example #7
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static <T1, T2> Flowable<Notification<CallableResultSet2<T1, T2>>> createWithTwoResultSets(Connection con,
        String sql, Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        Function<? super ResultSet, ? extends T1> f1, Function<? super ResultSet, ? extends T2> f2, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSet2<T1, T2>>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        final Flowable<T1> flowable1 = createFlowable(stmt, f1);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T2> flowable2 = createFlowable(stmt, f2);
                        return Single.just(new CallableResultSet2<T1, T2>(outputValues, flowable1, flowable2))
                                .toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example #8
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static <T1> Flowable<Notification<CallableResultSet1<T1>>> createWithOneResultSet(Connection con,
        String sql, Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        Function<? super ResultSet, ? extends T1> f1, int fetchSize) {
    log.debug("Update.create {}", sql);
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSet1<T1>>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        Flowable<T1> flowable1 = createFlowable(stmt, f1);
                        return Single.just(new CallableResultSet1<T1>(outputValues, flowable1)).toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example #9
Source File: RecordingObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
private Notification<T> takeNotification() {
    Notification<T> notification = events.pollFirst();
    if (notification == null) {
        throw new AssertionError("No event found!");
    }
    return notification;
}
 
Example #10
Source File: RecordingObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
public Throwable takeError() {
    Notification<T> notification = takeNotification();
    assertThat(notification.isOnError())
            .as("Expected onError event but was " + notification)
            .isTrue();
    return notification.getError();
}
 
Example #11
Source File: RecordingObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
public T takeValue() {
    Notification<T> notification = takeNotification();
    assertThat(notification.isOnNext())
            .as("Expected onNext event but was " + notification)
            .isTrue();
    return notification.getValue();
}
 
Example #12
Source File: RecordingObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
public void assertComplete() {
    Notification<T> notification = takeNotification();
    assertThat(notification.isOnComplete())
            .as("Expected onCompleted event but was " + notification)
            .isTrue();
    assertNoEvents();
}
 
Example #13
Source File: RecordingMaybeObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
public Throwable takeError() {
    Notification<T> notification = takeNotification();
    assertThat(notification.isOnError())
            .as("Expected onError event but was " + notification)
            .isTrue();
    return notification.getError();
}
 
Example #14
Source File: RecordingSingleObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
private Notification<T> takeNotification() {
    Notification<T> notification = events.pollFirst();
    if (notification == null) {
        throw new AssertionError("No event found!");
    }
    return notification;
}
 
Example #15
Source File: RecordingSingleObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
public T takeValue() {
    Notification<T> notification = takeNotification();
    assertThat(notification.isOnNext())
            .as("Expected onNext event but was " + notification)
            .isTrue();
    return notification.getValue();
}
 
Example #16
Source File: RecordingSingleObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
public Throwable takeError() {
    Notification<T> notification = takeNotification();
    assertThat(notification.isOnError())
            .as("Expected onError event but was " + notification)
            .isTrue();
    return notification.getError();
}
 
Example #17
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static <T1, T2, T3, T4> Flowable<Notification<CallableResultSet4<T1, T2, T3, T4>>> createWithFourResultSets(
        Single<Connection> connection, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Function<? super ResultSet, ? extends T1> f1,
        Function<? super ResultSet, ? extends T2> f2, Function<? super ResultSet, ? extends T3> f3,
        Function<? super ResultSet, ? extends T4> f4, int fetchSize) {
    return connection.toFlowable().flatMap(con -> createWithFourResultSets(con, sql, parameterGroups,
            parameterPlaceholders, f1, f2, f3, f4, fetchSize));
}
 
Example #18
Source File: Transformers.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<?> apply(final Observable<Object> o) throws Exception {
    return Observable.defer(new Callable<Observable<Object>>() {

        final long[] count = new long[1];

        @Override
        public Observable<Object> call() throws Exception {
            return o.materialize() //
                    .flatMap(new Function<Notification<Object>, Observable<Notification<Object>>>() {
                        @Override
                        public Observable<Notification<Object>> apply(Notification<Object> x) throws Exception {
                            if (x.isOnNext()) {
                                count[0]++;
                                if (count[0] > 1) {
                                    return Observable.just(x);
                                } else {
                                    return Observable.empty();
                                }
                            } else if (x.isOnComplete()) {
                                if (count[0] <= 1) {
                                    // complete the stream
                                    return Observable.just(x);
                                } else {
                                    // never complete
                                    return Observable.never();
                                }
                            } else {
                                // is onError
                                return Observable.just(x);
                            }
                        }
                    }) //
                    .dematerialize(Functions.<Notification<Object>>identity());
        }
    });
}
 
Example #19
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
private static <T> Flowable<Notification<T>> createWithParameters(Connection con, String sql,
        Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        BiFunction<NamedCallableStatement, List<Object>, Single<T>> single) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<T>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> single.apply(stmt, parameters).toFlowable()) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example #20
Source File: TransformerStateMachine.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public boolean tryOnError(Throwable e) {
    if (emitter.isCancelled()) {
        return false;
    } else {
        emitter.onNext(Notification.<Out>createOnError(e));
        return true;
    }
}
 
Example #21
Source File: RecordingMaybeObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
private Notification<T> takeNotification() {
    Notification<T> notification = events.pollFirst();
    if (notification == null) {
        throw new AssertionError("No event found!");
    }
    return notification;
}
 
Example #22
Source File: RecordingMaybeObserver.java    From retrocache with Apache License 2.0 5 votes vote down vote up
public T takeValue() {
    Notification<T> notification = takeNotification();
    assertThat(notification.isOnNext())
            .as("Expected onNext event but was " + notification)
            .isTrue();
    return notification.getValue();
}
 
Example #23
Source File: ExampleUnitTest.java    From RxAndroid-Sample with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaterializeObservable() throws InterruptedException {


    Observable.just("A", "B", "C", "D", "E", "F")
            .materialize()
            .subscribe(new Observer<Notification<String>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(Notification<String> stringNotification) {
                    /*
                     * From the notification object, we can check if the
                     * emitted item is:
                     * isOnNext() or isOnError() or isOnComplete()
                     *
                     * Here we can basically fetch items that are successful
                     * & omit items that resulted in error.
                     *
                     *  */
                    System.out.println(stringNotification.getValue());
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
}
 
Example #24
Source File: Update.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static Flowable<Notification<Integer>> create(Single<Connection> connection,
                                              Flowable<List<Object>> parameterGroups, String sql, int batchSize,
                                              boolean eagerDispose, int queryTimeoutSec) {
    return connection //
            .toFlowable() //
            .flatMap(con -> create(con, sql, parameterGroups, batchSize, eagerDispose, queryTimeoutSec), true,
                    1);
}
 
Example #25
Source File: Update.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
private static Flowable<Notification<Integer>> executeFinalBatch(NamedPreparedStatement ps,
        Notification<Integer> n, boolean outstandingBatch) throws SQLException {
    if (n.isOnComplete() && outstandingBatch) {
        log.debug("executing final batch");
        return toFlowable(ps.ps.executeBatch()) //
                .map(x -> Notification.createOnNext(x)) //
                .concatWith(Flowable.just(n));
    } else {
        return Flowable.just(n);
    }
}
 
Example #26
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static <T1, T2, T3> Flowable<Notification<CallableResultSet3<T1, T2, T3>>> createWithThreeResultSets(
        Single<Connection> connection, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Function<? super ResultSet, ? extends T1> f1,
        Function<? super ResultSet, ? extends T2> f2, Function<? super ResultSet, ? extends T3> f3, int fetchSize) {
    return connection.toFlowable().flatMap(con -> createWithThreeResultSets(con, sql, parameterGroups,
            parameterPlaceholders, f1, f2, f3, fetchSize));
}
 
Example #27
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static <T1, T2> Flowable<Notification<Tuple2<T1, T2>>> createWithTwoOutParameters(Single<Connection> connection,
        String sql, Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        Class<T1> cls1, Class<T2> cls2) {
    return connection.toFlowable().flatMap(con -> createWithParameters(con, sql, parameterGroups,
            parameterPlaceholders,
            (stmt, parameters) -> createWithTwoParameters(stmt, parameters, parameterPlaceholders, cls1, cls2)));
}
 
Example #28
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static <T1, T2, T3> Flowable<Notification<Tuple3<T1, T2, T3>>> createWithThreeOutParameters(
        Single<Connection> connection, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Class<T1> cls1, Class<T2> cls2, Class<T3> cls3) {
    return connection.toFlowable()
            .flatMap(con -> createWithParameters(con, sql, parameterGroups, parameterPlaceholders,
                    (stmt, parameters) -> createWithThreeParameters(stmt, parameters, parameterPlaceholders, cls1,
                            cls2, cls3)));
}
 
Example #29
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static <T1, T2, T3, T4> Flowable<Notification<Tuple4<T1, T2, T3, T4>>> createWithFourOutParameters(
        Single<Connection> connection, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Class<T1> cls1, Class<T2> cls2, Class<T3> cls3,
        Class<T4> cls4) {
    return connection.toFlowable()
            .flatMap(con -> createWithParameters(con, sql, parameterGroups, parameterPlaceholders,
                    (stmt, parameters) -> createWithFourParameters(stmt, parameters, parameterPlaceholders, cls1,
                            cls2, cls3, cls4)));
}
 
Example #30
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static <T1, T2> Flowable<Notification<CallableResultSet2<T1, T2>>> createWithTwoResultSets(
        Single<Connection> connection, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Function<? super ResultSet, ? extends T1> f1,
        Function<? super ResultSet, ? extends T2> f2, int fetchSize) {
    return connection.toFlowable().flatMap(
            con -> createWithTwoResultSets(con, sql, parameterGroups, parameterPlaceholders, f1, f2, fetchSize));
}