Search in sources :

Example 1 with Operator

use of rx.Observable.Operator in project tutorials by eugenp.

the class RxJavaCustomOperatorUnitTest method whenUseFunctionOperator_thenSuccess.

@Test
public void whenUseFunctionOperator_thenSuccess() {
    final Operator<String, String> cleanStringFn = subscriber -> new Subscriber<String>(subscriber) {

        @Override
        public void onCompleted() {
            if (!subscriber.isUnsubscribed()) {
                subscriber.onCompleted();
            }
        }

        @Override
        public void onError(Throwable t) {
            if (!subscriber.isUnsubscribed()) {
                subscriber.onError(t);
            }
        }

        @Override
        public void onNext(String str) {
            if (!subscriber.isUnsubscribed()) {
                final String result = str.replaceAll("[^A-Za-z0-9]", "");
                subscriber.onNext(result);
            }
        }
    };
    final List<String> results = new ArrayList<>();
    Observable.from(Arrays.asList("ap_p-l@e", "or-an?ge")).lift(cleanStringFn).subscribe(results::add);
    assertThat(results, notNullValue());
    assertThat(results, hasSize(2));
    assertThat(results, hasItems("apple", "orange"));
}
Also used : Arrays(java.util.Arrays) Transformer(rx.Observable.Transformer) Subscriber(rx.Subscriber) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Test(org.junit.Test) Matchers.hasItems(org.hamcrest.Matchers.hasItems) Observable(rx.Observable) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) ToLength.toLength(com.baeldung.rxjava.operator.ToLength.toLength) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ToCleanString.toCleanString(com.baeldung.rxjava.operator.ToCleanString.toCleanString) Operator(rx.Observable.Operator) Subscriber(rx.Subscriber) ArrayList(java.util.ArrayList) ToCleanString.toCleanString(com.baeldung.rxjava.operator.ToCleanString.toCleanString) Test(org.junit.Test)

Aggregations

ToCleanString.toCleanString (com.baeldung.rxjava.operator.ToCleanString.toCleanString)1 ToLength.toLength (com.baeldung.rxjava.operator.ToLength.toLength)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Matchers.hasItems (org.hamcrest.Matchers.hasItems)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)1 Assert.assertThat (org.junit.Assert.assertThat)1 Test (org.junit.Test)1 Observable (rx.Observable)1 Operator (rx.Observable.Operator)1 Transformer (rx.Observable.Transformer)1 Subscriber (rx.Subscriber)1