Full outer join of two ordered observables

后端 未结 1 1245
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 10:26

Suppose we have two observables Observable o1 and Observable o2 and each observable is producing strictly increasing sequ

相关标签:
1条回答
  • 2021-01-21 11:11

    There is no single operator for this but it is possible to compose the behavior from standard and extension operators:

    static abstract class Pair implements Comparable<Pair> { 
        int value;
    
        @Override
        public int compareTo(Pair o) {
            return Integer.compare(value, o.value);
        }
    }
    
    static final class Left extends Pair {
        Left(int value) {
            this.value = value;
        }
    
        @Override
        public String toString() {
            return "[" + value + ", _]";
        }
    }
    
    static final class Right extends Pair {
        Right(int value) {
            this.value = value;
        }
    
        @Override
        public String toString() {
            return "[_, " + value + "]";
        }
    }
    
    static final class Both extends Pair {
        Both(int value) {
            this.value = value;
        }
    
        @Override
        public int hashCode() {
            return value;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Both) {
                return ((Both)obj).value == value;
            }
            return false;
        }
    
        @Override
        public String toString() {
            return "[" + value + ", " + value + "]";
        }
    }
    
    @SuppressWarnings("unchecked")
    @Test
    public void test() {
        Flowable<Integer> a = Flowable.just(0, 2, 3, 6);
        Flowable<Integer> b = Flowable.just(1, 2, 3, 4, 5, 6);
    
        Flowable.defer(() -> {
            boolean[] skip = { false };
            return Flowables.<Pair>orderedMerge(
                    a.<Pair>map(Left::new), b.<Pair>map(Right::new)
                )
                .distinctUntilChanged()
                .buffer(2, 1)
                .flatMapIterable(buf -> {
                    if (skip[0]) {
                        skip[0] = false;
                        return Collections.emptyList();
                    }
                    if (buf.size() == 2) {
                        if (buf.get(0).value == buf.get(1).value) {
                            skip[0] = true;
                            return Collections.singletonList(new Both(buf.get(0).value));
                        }
                        return buf.subList(0, 1);
                    }
                    return buf;
                });
        })
        .subscribe(System.out::println);
    }
    

    where Flowables.orderedMerge is in the RxJava 2 Extensions library.

    0 讨论(0)
提交回复
热议问题