|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package rx.internal.operators; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
| 19 | + |
| 20 | +import rx.*; |
| 21 | +import rx.Observable.Operator; |
| 22 | +import rx.functions.Func2; |
| 23 | +import rx.observers.SerializedSubscriber; |
| 24 | + |
| 25 | +/** |
| 26 | + * Combines values from two sources only when the main source emits. |
| 27 | + * @param <T> the element type of the main observable |
| 28 | + * @param <U> the element type of the other observable that is merged into the main |
| 29 | + * @param <R> the result element type |
| 30 | + */ |
| 31 | +public final class OperatorWithLatestFrom<T, U, R> implements Operator<R, T> { |
| 32 | + final Func2<? super T, ? super U, ? extends R> resultSelector; |
| 33 | + final Observable<? extends U> other; |
| 34 | + /** Indicates the other has not yet emitted a value. */ |
| 35 | + static final Object EMPTY = new Object(); |
| 36 | + |
| 37 | + public OperatorWithLatestFrom(Observable<? extends U> other, Func2<? super T, ? super U, ? extends R> resultSelector) { |
| 38 | + this.other = other; |
| 39 | + this.resultSelector = resultSelector; |
| 40 | + } |
| 41 | + @Override |
| 42 | + public Subscriber<? super T> call(Subscriber<? super R> child) { |
| 43 | + // onError and onCompleted may happen either from the main or from other. |
| 44 | + final SerializedSubscriber<R> s = new SerializedSubscriber<R>(child, false); |
| 45 | + child.add(s); |
| 46 | + |
| 47 | + final AtomicReference<Object> current = new AtomicReference<Object>(EMPTY); |
| 48 | + |
| 49 | + final Subscriber<T> subscriber = new Subscriber<T>(s, true) { |
| 50 | + @Override |
| 51 | + public void onNext(T t) { |
| 52 | + Object o = current.get(); |
| 53 | + if (o != EMPTY) { |
| 54 | + try { |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + U u = (U)o; |
| 57 | + R result = resultSelector.call(t, u); |
| 58 | + |
| 59 | + s.onNext(result); |
| 60 | + } catch (Throwable e) { |
| 61 | + onError(e); |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + @Override |
| 67 | + public void onError(Throwable e) { |
| 68 | + s.onError(e); |
| 69 | + s.unsubscribe(); |
| 70 | + } |
| 71 | + @Override |
| 72 | + public void onCompleted() { |
| 73 | + s.onCompleted(); |
| 74 | + s.unsubscribe(); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + Subscriber<U> otherSubscriber = new Subscriber<U>() { |
| 79 | + @Override |
| 80 | + public void onNext(U t) { |
| 81 | + current.set(t); |
| 82 | + } |
| 83 | + @Override |
| 84 | + public void onError(Throwable e) { |
| 85 | + s.onError(e); |
| 86 | + s.unsubscribe(); |
| 87 | + } |
| 88 | + @Override |
| 89 | + public void onCompleted() { |
| 90 | + if (current.get() == EMPTY) { |
| 91 | + s.onCompleted(); |
| 92 | + s.unsubscribe(); |
| 93 | + } |
| 94 | + } |
| 95 | + }; |
| 96 | + s.add(subscriber); |
| 97 | + s.add(otherSubscriber); |
| 98 | + |
| 99 | + other.unsafeSubscribe(otherSubscriber); |
| 100 | + |
| 101 | + return subscriber; |
| 102 | + } |
| 103 | +} |
0 commit comments