|
| 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.operators; |
| 17 | + |
| 18 | +import static org.mockito.Mockito.*; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import rx.Observable; |
| 23 | +import rx.Observer; |
| 24 | +import rx.util.functions.Func1; |
| 25 | +import rx.util.functions.Func2; |
| 26 | + |
| 27 | +/** |
| 28 | + * A few operators for implementing the averaging operation. |
| 29 | + * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.average%28v=vs.103%29.aspx">MSDN: Observable.Average</a> |
| 30 | + */ |
| 31 | +public final class OperationAverage { |
| 32 | + private static final class Tuple2<T> { |
| 33 | + private final T current; |
| 34 | + private final Integer count; |
| 35 | + |
| 36 | + private Tuple2(T v1, Integer v2) { |
| 37 | + current = v1; |
| 38 | + count = v2; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static Observable<Integer> average(Observable<Integer> source) { |
| 43 | + return source.reduce(new Tuple2<Integer>(0, 0), new Func2<Tuple2<Integer>, Integer, Tuple2<Integer>>() { |
| 44 | + @Override |
| 45 | + public Tuple2<Integer> call(Tuple2<Integer> accu, Integer next) { |
| 46 | + return new Tuple2<Integer>(accu.current + next, accu.count + 1); |
| 47 | + } |
| 48 | + }).map(new Func1<Tuple2<Integer>, Integer>() { |
| 49 | + @Override |
| 50 | + public Integer call(Tuple2<Integer> result) { |
| 51 | + return result.current / result.count; // may throw DivisionByZero, this should be correct... |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + public static Observable<Long> averageLongs(Observable<Long> source) { |
| 57 | + return source.reduce(new Tuple2<Long>(0L, 0), new Func2<Tuple2<Long>, Long, Tuple2<Long>>() { |
| 58 | + @Override |
| 59 | + public Tuple2<Long> call(Tuple2<Long> accu, Long next) { |
| 60 | + return new Tuple2<Long>(accu.current + next, accu.count + 1); |
| 61 | + } |
| 62 | + }).map(new Func1<Tuple2<Long>, Long>() { |
| 63 | + @Override |
| 64 | + public Long call(Tuple2<Long> result) { |
| 65 | + return result.current / result.count; // may throw DivisionByZero, this should be correct... |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public static Observable<Float> averageFloats(Observable<Float> source) { |
| 71 | + return source.reduce(new Tuple2<Float>(0.0f, 0), new Func2<Tuple2<Float>, Float, Tuple2<Float>>() { |
| 72 | + @Override |
| 73 | + public Tuple2<Float> call(Tuple2<Float> accu, Float next) { |
| 74 | + return new Tuple2<Float>(accu.current + next, accu.count + 1); |
| 75 | + } |
| 76 | + }).map(new Func1<Tuple2<Float>, Float>() { |
| 77 | + @Override |
| 78 | + public Float call(Tuple2<Float> result) { |
| 79 | + if (result.count == 0) { |
| 80 | + throw new ArithmeticException("divide by zero"); |
| 81 | + } |
| 82 | + return result.current / result.count; |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + public static Observable<Double> averageDoubles(Observable<Double> source) { |
| 88 | + return source.reduce(new Tuple2<Double>(0.0d, 0), new Func2<Tuple2<Double>, Double, Tuple2<Double>>() { |
| 89 | + @Override |
| 90 | + public Tuple2<Double> call(Tuple2<Double> accu, Double next) { |
| 91 | + return new Tuple2<Double>(accu.current + next, accu.count + 1); |
| 92 | + } |
| 93 | + }).map(new Func1<Tuple2<Double>, Double>() { |
| 94 | + @Override |
| 95 | + public Double call(Tuple2<Double> result) { |
| 96 | + if (result.count == 0) { |
| 97 | + throw new ArithmeticException("divide by zero"); |
| 98 | + } |
| 99 | + return result.current / result.count; |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + public static class UnitTest { |
| 105 | + @SuppressWarnings("unchecked") |
| 106 | + Observer<Integer> w = mock(Observer.class); |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + Observer<Long> wl = mock(Observer.class); |
| 109 | + @SuppressWarnings("unchecked") |
| 110 | + Observer<Float> wf = mock(Observer.class); |
| 111 | + @SuppressWarnings("unchecked") |
| 112 | + Observer<Double> wd = mock(Observer.class); |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testAverageOfAFewInts() throws Throwable { |
| 116 | + Observable<Integer> src = Observable.from(1, 2, 3, 4, 6); |
| 117 | + average(src).subscribe(w); |
| 118 | + |
| 119 | + verify(w, times(1)).onNext(anyInt()); |
| 120 | + verify(w).onNext(3); |
| 121 | + verify(w, never()).onError(any(Throwable.class)); |
| 122 | + verify(w, times(1)).onCompleted(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testEmptyAverage() throws Throwable { |
| 127 | + Observable<Integer> src = Observable.from(); |
| 128 | + average(src).subscribe(w); |
| 129 | + |
| 130 | + verify(w, never()).onNext(anyInt()); |
| 131 | + verify(w, times(1)).onError(any(ArithmeticException.class)); |
| 132 | + verify(w, never()).onCompleted(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testAverageOfAFewLongs() throws Throwable { |
| 137 | + Observable<Long> src = Observable.from(1L, 2L, 3L, 4L, 6L); |
| 138 | + averageLongs(src).subscribe(wl); |
| 139 | + |
| 140 | + verify(wl, times(1)).onNext(anyLong()); |
| 141 | + verify(wl).onNext(3L); |
| 142 | + verify(wl, never()).onError(any(Throwable.class)); |
| 143 | + verify(wl, times(1)).onCompleted(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testEmptyAverageLongs() throws Throwable { |
| 148 | + Observable<Long> src = Observable.from(); |
| 149 | + averageLongs(src).subscribe(wl); |
| 150 | + |
| 151 | + verify(wl, never()).onNext(anyLong()); |
| 152 | + verify(wl, times(1)).onError(any(ArithmeticException.class)); |
| 153 | + verify(wl, never()).onCompleted(); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testAverageOfAFewFloats() throws Throwable { |
| 158 | + Observable<Float> src = Observable.from(1.0f, 2.0f); |
| 159 | + averageFloats(src).subscribe(wf); |
| 160 | + |
| 161 | + verify(wf, times(1)).onNext(anyFloat()); |
| 162 | + verify(wf).onNext(1.5f); |
| 163 | + verify(wf, never()).onError(any(Throwable.class)); |
| 164 | + verify(wf, times(1)).onCompleted(); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testEmptyAverageFloats() throws Throwable { |
| 169 | + Observable<Float> src = Observable.from(); |
| 170 | + averageFloats(src).subscribe(wf); |
| 171 | + |
| 172 | + verify(wf, never()).onNext(anyFloat()); |
| 173 | + verify(wf, times(1)).onError(any(ArithmeticException.class)); |
| 174 | + verify(wf, never()).onCompleted(); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testAverageOfAFewDoubles() throws Throwable { |
| 179 | + Observable<Double> src = Observable.from(1.0d, 2.0d); |
| 180 | + averageDoubles(src).subscribe(wd); |
| 181 | + |
| 182 | + verify(wd, times(1)).onNext(anyDouble()); |
| 183 | + verify(wd).onNext(1.5d); |
| 184 | + verify(wd, never()).onError(any(Throwable.class)); |
| 185 | + verify(wd, times(1)).onCompleted(); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testEmptyAverageDoubles() throws Throwable { |
| 190 | + Observable<Double> src = Observable.from(); |
| 191 | + averageDoubles(src).subscribe(wd); |
| 192 | + |
| 193 | + verify(wd, never()).onNext(anyDouble()); |
| 194 | + verify(wd, times(1)).onError(any(ArithmeticException.class)); |
| 195 | + verify(wd, never()).onCompleted(); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments