|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.joins; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.LinkedList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Queue; |
| 22 | +import rx.Notification; |
| 23 | +import rx.Observable; |
| 24 | +import rx.subscriptions.SingleAssignmentSubscription; |
| 25 | +import rx.util.functions.Action1; |
| 26 | + |
| 27 | +/** |
| 28 | + * Default implementation of a join observer. |
| 29 | + */ |
| 30 | +public final class JoinObserver1<T> extends ObserverBase<Notification<T>> implements JoinObserver { |
| 31 | + private Object gate; |
| 32 | + private final Observable<T> source; |
| 33 | + private final Action1<Throwable> onError; |
| 34 | + private final List<ActivePlan0> activePlans; |
| 35 | + private final Queue<Notification<T>> queue; |
| 36 | + private final SingleAssignmentSubscription subscription; |
| 37 | + private volatile boolean done; |
| 38 | + |
| 39 | + public JoinObserver1(Observable<T> source, Action1<Throwable> onError) { |
| 40 | + this.source = source; |
| 41 | + this.onError = onError; |
| 42 | + queue = new LinkedList<Notification<T>>(); |
| 43 | + subscription = new SingleAssignmentSubscription(); |
| 44 | + activePlans = new ArrayList<ActivePlan0>(); |
| 45 | + } |
| 46 | + public Queue<Notification<T>> queue() { |
| 47 | + return queue; |
| 48 | + } |
| 49 | + public void addActivePlan(ActivePlan0 activePlan) { |
| 50 | + activePlans.add(activePlan); |
| 51 | + } |
| 52 | + @Override |
| 53 | + public void subscribe(Object gate) { |
| 54 | + this.gate = gate; |
| 55 | + subscription.set(source.materialize().subscribe(this)); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void dequeue() { |
| 60 | + queue.remove(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void onNextCore(Notification<T> args) { |
| 65 | + synchronized (gate) { |
| 66 | + if (!done) { |
| 67 | + if (args.isOnError()) { |
| 68 | + onError.call(args.getThrowable()); |
| 69 | + return; |
| 70 | + } |
| 71 | + queue.add(args); |
| 72 | + |
| 73 | + // remark: activePlans might change while iterating |
| 74 | + for (ActivePlan0 a : new ArrayList<ActivePlan0>(activePlans)) { |
| 75 | + a.match(); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void onErrorCore(Throwable e) { |
| 83 | + // not expected |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected void onCompletedCore() { |
| 88 | + // not expected or ignored |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + void removeActivePlan(ActivePlan0 activePlan) { |
| 93 | + activePlans.remove(activePlan); |
| 94 | + if (activePlans.isEmpty()) { |
| 95 | + unsubscribe(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void unsubscribe() { |
| 101 | + if (!done) { |
| 102 | + done = true; |
| 103 | + subscription.unsubscribe(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments