Decoration observed

Is it possible to decorate Observable<>, Single<>, Maybe<>, Flowable<>in the rx-java?
For instance. eg:

public final class NonEmptyStringSource extends Observable<String> {

    private final Observable<String> source;

    public NonEmptyStringSource(final Observable<String> source) {
        this.source = source.filter(s -> s.length() > 0);
    }

    @Override
    protected void subscribeActual(final Observer<? super String> observer) {
        this.source.subscribe(observer);
    }
}

Does this approach have some pitfalls?
Is he safe?

+4
source share
2 answers

Unlike 1.x, this template in 2.x is not fined and is almost similar to how standard operators are implemented. Depending on your needs, you can use instead ObservableTransformer:

ObservableTransformer<String, String> t = 
    upstream -> upstream.filter(s -> s.length() > 0);

Observable.fromArray("a", "b", "", "d", "", "f")
.compose(t)
.subscribe(System.out::println, Throwable::printStackTrace);
+3
source

I would advise against this, as it just confuses what is actually happening.

filter inline :

 .filter(StringUtils::isNotBlank)

Apache Commons-Lang, .

+1

Source: https://habr.com/ru/post/1674591/


All Articles