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"));
}
Aggregations