|
15 | 15 | */
|
16 | 16 | package rx.internal.operators;
|
17 | 17 |
|
| 18 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 19 | + |
18 | 20 | import rx.Observable.Operator;
|
| 21 | +import rx.Producer; |
19 | 22 | import rx.Subscriber;
|
20 | 23 |
|
21 | 24 | /**
|
@@ -45,40 +48,70 @@ private OperatorElementAt(int index, T defaultValue, boolean hasDefault) {
|
45 | 48 | }
|
46 | 49 |
|
47 | 50 | @Override
|
48 |
| - public Subscriber<? super T> call(final Subscriber<? super T> subscriber) { |
49 |
| - return new Subscriber<T>(subscriber) { |
| 51 | + public Subscriber<? super T> call(final Subscriber<? super T> child) { |
| 52 | + Subscriber<T> parent = new Subscriber<T>() { |
50 | 53 |
|
51 | 54 | private int currentIndex = 0;
|
52 | 55 |
|
53 | 56 | @Override
|
54 | 57 | public void onNext(T value) {
|
55 |
| - if (currentIndex == index) { |
56 |
| - subscriber.onNext(value); |
57 |
| - subscriber.onCompleted(); |
58 |
| - } else { |
59 |
| - request(1); |
| 58 | + if (currentIndex++ == index) { |
| 59 | + child.onNext(value); |
| 60 | + child.onCompleted(); |
| 61 | + unsubscribe(); |
60 | 62 | }
|
61 |
| - currentIndex++; |
62 | 63 | }
|
63 | 64 |
|
64 | 65 | @Override
|
65 | 66 | public void onError(Throwable e) {
|
66 |
| - subscriber.onError(e); |
| 67 | + child.onError(e); |
67 | 68 | }
|
68 | 69 |
|
69 | 70 | @Override
|
70 | 71 | public void onCompleted() {
|
71 | 72 | if (currentIndex <= index) {
|
72 | 73 | // If "subscriber.onNext(value)" is called, currentIndex must be greater than index
|
73 | 74 | if (hasDefault) {
|
74 |
| - subscriber.onNext(defaultValue); |
75 |
| - subscriber.onCompleted(); |
| 75 | + child.onNext(defaultValue); |
| 76 | + child.onCompleted(); |
76 | 77 | } else {
|
77 |
| - subscriber.onError(new IndexOutOfBoundsException(index + " is out of bounds")); |
| 78 | + child.onError(new IndexOutOfBoundsException(index + " is out of bounds")); |
78 | 79 | }
|
79 | 80 | }
|
80 | 81 | }
|
| 82 | + |
| 83 | + @Override |
| 84 | + public void setProducer(Producer p) { |
| 85 | + child.setProducer(new InnerProducer(p)); |
| 86 | + } |
81 | 87 | };
|
| 88 | + child.add(parent); |
| 89 | + |
| 90 | + return parent; |
| 91 | + } |
| 92 | + /** |
| 93 | + * A producer that wraps another Producer and requests Long.MAX_VALUE |
| 94 | + * when the first positive request() call comes in. |
| 95 | + */ |
| 96 | + static class InnerProducer extends AtomicBoolean implements Producer { |
| 97 | + /** */ |
| 98 | + private static final long serialVersionUID = 1L; |
| 99 | + |
| 100 | + final Producer actual; |
| 101 | + |
| 102 | + public InnerProducer(Producer actual) { |
| 103 | + this.actual = actual; |
| 104 | + } |
| 105 | + @Override |
| 106 | + public void request(long n) { |
| 107 | + if (n < 0) { |
| 108 | + throw new IllegalArgumentException("n >= 0 required"); |
| 109 | + } |
| 110 | + if (n > 0 && compareAndSet(false, true)) { |
| 111 | + // trigger the fast-path since the operator is going |
| 112 | + // to skip all but the indexth element |
| 113 | + actual.request(Long.MAX_VALUE); |
| 114 | + } |
| 115 | + } |
82 | 116 | }
|
83 |
| - |
84 | 117 | }
|
0 commit comments