1
- /**
2
- * Copyright 2014 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 .math .operators ;
17
-
18
- import rx .Observable .Operator ;
19
- import rx .Subscriber ;
20
- import rx .functions .Func1 ;
21
-
22
- /**
23
- * Compute the average by extracting double values from the source via an
24
- * extractor function.
25
- *
26
- * @param <T>
27
- * the source value type
28
- */
29
- public final class OperatorAverageDouble <T > implements Operator <Double , T > {
30
- final Func1 <? super T , Double > valueExtractor ;
31
-
32
- public OperatorAverageDouble (Func1 <? super T , Double > valueExtractor ) {
33
- this .valueExtractor = valueExtractor ;
34
- }
35
-
36
- @ Override
37
- public Subscriber <? super T > call (Subscriber <? super Double > child ) {
38
- return new AverageObserver (child );
39
- }
40
-
41
- /** Computes the average. */
42
- private final class AverageObserver extends Subscriber <T > {
43
- final Subscriber <? super Double > child ;
44
- double sum ;
45
- int count ;
46
-
47
- public AverageObserver (Subscriber <? super Double > subscriber ) {
48
- super (subscriber );
49
- this .child = subscriber ;
50
- }
51
-
52
- @ Override
53
- public void onNext (T args ) {
54
- sum += valueExtractor .call (args );
55
- count ++;
56
- }
57
-
58
- @ Override
59
- public void onError (Throwable e ) {
60
- child .onError (e );
61
- }
62
-
63
- @ Override
64
- public void onCompleted () {
65
- if (count > 0 ) {
66
- try {
67
- child .onNext (sum / count );
68
- } catch (Throwable t ) {
69
- child .onError (t );
70
- return ;
71
- }
72
- child .onCompleted ();
73
- } else {
74
- child .onError (new IllegalArgumentException ("Sequence contains no elements" ));
75
- }
76
- }
77
-
78
- }
1
+ /**
2
+ * Copyright 2014 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 .math .operators ;
17
+
18
+ import rx .Observable .Operator ;
19
+ import rx .Subscriber ;
20
+ import rx .functions .Func1 ;
21
+
22
+ /**
23
+ * Compute the average by extracting double values from the source via an
24
+ * extractor function.
25
+ *
26
+ * @param <T>
27
+ * the source value type
28
+ */
29
+ public final class OperatorAverageDouble <T > implements Operator <Double , T > {
30
+ final Func1 <? super T , Double > valueExtractor ;
31
+
32
+ public OperatorAverageDouble (Func1 <? super T , Double > valueExtractor ) {
33
+ this .valueExtractor = valueExtractor ;
34
+ }
35
+
36
+ @ Override
37
+ public Subscriber <? super T > call (Subscriber <? super Double > child ) {
38
+ return new AverageObserver (child );
39
+ }
40
+
41
+ /** Computes the average. */
42
+ private final class AverageObserver extends Subscriber <T > {
43
+ final Subscriber <? super Double > child ;
44
+ double sum ;
45
+ int count ;
46
+
47
+ public AverageObserver (Subscriber <? super Double > subscriber ) {
48
+ super (subscriber );
49
+ this .child = subscriber ;
50
+ }
51
+
52
+ @ Override
53
+ public void onNext (T args ) {
54
+ sum += valueExtractor .call (args );
55
+ count ++;
56
+ }
57
+
58
+ @ Override
59
+ public void onError (Throwable e ) {
60
+ child .onError (e );
61
+ }
62
+
63
+ @ Override
64
+ public void onCompleted () {
65
+ if (count > 0 ) {
66
+ try {
67
+ child .onNext (sum / count );
68
+ } catch (Throwable t ) {
69
+ child .onError (t );
70
+ return ;
71
+ }
72
+ child .onCompleted ();
73
+ } else {
74
+ child .onError (new IllegalArgumentException ("Sequence contains no elements" ));
75
+ }
76
+ }
77
+
78
+ }
79
79
}
0 commit comments