Repository navigation
Expand file tree
/
Copy pathOperatorScan.java
More file actions
173 lines (154 loc) · 6.76 KB
/
Copy pathOperatorScan.java
File metadata and controls
173 lines (154 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.internal.operators;
import java.util.concurrent.atomic.AtomicBoolean;
import rx.Observable.Operator;
import rx.Producer;
import rx.Subscriber;
import rx.exceptions.Exceptions;
import rx.exceptions.OnErrorThrowable;
import rx.functions.Func0;
import rx.functions.Func2;
/**
* Returns an Observable that applies a function to the first item emitted by a source Observable, then feeds
* the result of that function along with the second item emitted by an Observable into the same function, and
* so on until all items have been emitted by the source Observable, emitting the result of each of these
* iterations.
* <p>
* <img width="640" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/scan.png" alt="">
* <p>
* This sort of function is sometimes called an accumulator.
* <p>
* Note that when you pass a seed to {@code scan} the resulting Observable will emit that seed as its
* first emitted item.
*/
public final class OperatorScan<R, T> implements Operator<R, T> {
private final Func0<R> initialValueFactory;
private final Func2<R, ? super T, R> accumulator;
// sentinel if we don't receive an initial value
private static final Object NO_INITIAL_VALUE = new Object();
/**
* Applies an accumulator function over an observable sequence and returns each intermediate result with the
* specified source and accumulator.
*
* @param initialValue
* the initial (seed) accumulator value
* @param accumulator
* an accumulator function to be invoked on each element from the sequence
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212007.aspx">Observable.Scan(TSource, TAccumulate) Method (IObservable(TSource), TAccumulate, Func(TAccumulate, TSource,
* TAccumulate))</a>
*/
public OperatorScan(final R initialValue, Func2<R, ? super T, R> accumulator) {
this(new Func0<R>() {
@Override
public R call() {
return initialValue;
}
}, accumulator);
}
public OperatorScan(Func0<R> initialValueFactory, Func2<R, ? super T, R> accumulator) {
this.initialValueFactory = initialValueFactory;
this.accumulator = accumulator;
}
/**
* Applies an accumulator function over an observable sequence and returns each intermediate result with the
* specified source and accumulator.
*
* @param accumulator
* an accumulator function to be invoked on each element from the sequence
* @see <a href="http://msdn.microsoft.com/en-us/library/hh211665.aspx">Observable.Scan(TSource) Method (IObservable(TSource), Func(TSource, TSource, TSource))</a>
*/
@SuppressWarnings("unchecked")
public OperatorScan(final Func2<R, ? super T, R> accumulator) {
this((R) NO_INITIAL_VALUE, accumulator);
}
@Override
public Subscriber<? super T> call(final Subscriber<? super R> child) {
return new Subscriber<T>(child) {
private final R initialValue = initialValueFactory.call();
private R value = initialValue;
boolean initialized = false;
@SuppressWarnings("unchecked")
@Override
public void onNext(T currentValue) {
emitInitialValueIfNeeded(child);
if (this.value == NO_INITIAL_VALUE) {
// if there is NO_INITIAL_VALUE then we know it is type T for both so cast T to R
this.value = (R) currentValue;
} else {
try {
this.value = accumulator.call(this.value, currentValue);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
child.onError(OnErrorThrowable.addValueAsLastCause(e, currentValue));
return;
}
}
child.onNext(this.value);
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onCompleted() {
emitInitialValueIfNeeded(child);
child.onCompleted();
}
private void emitInitialValueIfNeeded(final Subscriber<? super R> child) {
if (!initialized) {
initialized = true;
// we emit first time through if we have an initial value
if (initialValue != NO_INITIAL_VALUE) {
child.onNext(initialValue);
}
}
}
/**
* We want to adjust the requested value by subtracting 1 if we have an initial value
*/
@Override
public void setProducer(final Producer producer) {
child.setProducer(new Producer() {
final AtomicBoolean once = new AtomicBoolean();
final AtomicBoolean excessive = new AtomicBoolean();
@Override
public void request(long n) {
if (once.compareAndSet(false, true)) {
if (initialValue == NO_INITIAL_VALUE || n == Long.MAX_VALUE) {
producer.request(n);
} else if (n == 1) {
excessive.set(true);
producer.request(1); // request at least 1
} else {
// n != Long.MAX_VALUE && n != 1
producer.request(n - 1);
}
} else {
// pass-thru after first time
if (n > 1 // avoid to request 0
&& excessive.compareAndSet(true, false) && n != Long.MAX_VALUE) {
producer.request(n - 1);
} else {
producer.request(n);
}
}
}
});
}
};
}
}