ReactiveX/RxJavaString

how about matchers for RXJavaString?

ArekCzarnik opened this issue · 1 comments

I try to work with matchers over long string files... and experiment with this implementation.

example could be:

Pattern pattern = Pattern.compile("(<div id=\"(?<id>.*)\" data-template=\"(?<template>.*)\" data-src=\"(?<src>.*)\">(?<fallback>.*)<\\/div>)");

String content = "blabla....";

Observable<Map> matcherMap = StringObservable.matcher(content, pattern,"id","template","src","fallback");

Implantation:


public static Observable<Map> matcher(final Observable<String> src, final Pattern pattern, final String... names) {
        return src.lift(new Operator<Map, String>() {
            @Override
            public Subscriber<? super String> call(final Subscriber<? super Map> subscriber) {

                return new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        if (!subscriber.isUnsubscribed())
                            subscriber.onCompleted();
                    }

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

                    @Override
                    public void onNext(final String segment) {
                        final Matcher matcher = pattern.matcher(segment);
                        while (!subscriber.isUnsubscribed() && matcher.find()) {
                            Map matcherMap = new HashMap();
                            for (String name : Arrays.asList(names)) {
                               matcherMap.put(name,matcher.group(name));
                            }
                            subscriber.onNext(matcherMap);
                        }
                    }
                };
            }
        });
    }

I think this was covered in #26. Thanks for the contribution.