| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 22 |
import io.reactivex.rxjava3.observable.ObservableCovarianceTest.*; |
| 23 |
|
| 24 |
public class ObservableReduceTests extends RxJavaTest { |
| 25 |
|
| 26 |
@Test |
| 27 |
public void reduceIntsObservable() { |
| 28 |
Observable<Integer> o = Observable.just(1, 2, 3); |
| 29 |
int value = o.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 30 |
@Override |
| 31 |
public Integer apply(Integer t1, Integer t2) { |
| 32 |
return t1 + t2; |
| 33 |
} |
| 34 |
}).toObservable().blockingSingle(); |
| 35 |
|
| 36 |
assertEquals(6, value); |
| 37 |
} |
| 38 |
|
| 39 |
@SuppressWarnings("unused") |
| 40 |
@Test |
| 41 |
public void reduceWithObjectsObservable() { |
| 42 |
Observable<Movie> horrorMovies = Observable.<Movie> just(new HorrorMovie()); |
| 43 |
|
| 44 |
Observable<Movie> reduceResult = horrorMovies.scan(new BiFunction<Movie, Movie, Movie>() { |
| 45 |
@Override |
| 46 |
public Movie apply(Movie t1, Movie t2) { |
| 47 |
return t2; |
| 48 |
} |
| 49 |
}).takeLast(1); |
| 50 |
|
| 51 |
Observable<Movie> reduceResult2 = horrorMovies.reduce(new BiFunction<Movie, Movie, Movie>() { |
| 52 |
@Override |
| 53 |
public Movie apply(Movie t1, Movie t2) { |
| 54 |
return t2; |
| 55 |
} |
| 56 |
}).toObservable(); |
| 57 |
|
| 58 |
assertNotNull(reduceResult2); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Reduce consumes and produces T so can't do covariance. |
| 63 |
* |
| 64 |
* https://github.com/ReactiveX/RxJava/issues/360#issuecomment-24203016 |
| 65 |
*/ |
| 66 |
@Test |
| 67 |
public void reduceWithCovariantObjectsObservable() { |
| 68 |
Observable<Movie> horrorMovies = Observable.<Movie> just(new HorrorMovie()); |
| 69 |
|
| 70 |
Observable<Movie> reduceResult2 = horrorMovies.reduce(new BiFunction<Movie, Movie, Movie>() { |
| 71 |
@Override |
| 72 |
public Movie apply(Movie t1, Movie t2) { |
| 73 |
return t2; |
| 74 |
} |
| 75 |
}).toObservable(); |
| 76 |
|
| 77 |
assertNotNull(reduceResult2); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void reduceInts() { |
| 82 |
Observable<Integer> o = Observable.just(1, 2, 3); |
| 83 |
int value = o.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 84 |
@Override |
| 85 |
public Integer apply(Integer t1, Integer t2) { |
| 86 |
return t1 + t2; |
| 87 |
} |
| 88 |
}).blockingGet(); |
| 89 |
|
| 90 |
assertEquals(6, value); |
| 91 |
} |
| 92 |
|
| 93 |
@SuppressWarnings("unused") |
| 94 |
@Test |
| 95 |
public void reduceWithObjects() { |
| 96 |
Observable<Movie> horrorMovies = Observable.<Movie> just(new HorrorMovie()); |
| 97 |
|
| 98 |
Observable<Movie> reduceResult = horrorMovies.scan(new BiFunction<Movie, Movie, Movie>() { |
| 99 |
@Override |
| 100 |
public Movie apply(Movie t1, Movie t2) { |
| 101 |
return t2; |
| 102 |
} |
| 103 |
}).takeLast(1); |
| 104 |
|
| 105 |
Maybe<Movie> reduceResult2 = horrorMovies.reduce(new BiFunction<Movie, Movie, Movie>() { |
| 106 |
@Override |
| 107 |
public Movie apply(Movie t1, Movie t2) { |
| 108 |
return t2; |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
assertNotNull(reduceResult2); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Reduce consumes and produces T so can't do covariance. |
| 117 |
* |
| 118 |
* https://github.com/ReactiveX/RxJava/issues/360#issuecomment-24203016 |
| 119 |
*/ |
| 120 |
@Test |
| 121 |
public void reduceWithCovariantObjects() { |
| 122 |
Observable<Movie> horrorMovies = Observable.<Movie> just(new HorrorMovie()); |
| 123 |
|
| 124 |
Maybe<Movie> reduceResult2 = horrorMovies.reduce(new BiFunction<Movie, Movie, Movie>() { |
| 125 |
@Override |
| 126 |
public Movie apply(Movie t1, Movie t2) { |
| 127 |
return t2; |
| 128 |
} |
| 129 |
}); |
| 130 |
|
| 131 |
assertNotNull(reduceResult2); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Reduce consumes and produces T so can't do covariance. |
| 136 |
* |
| 137 |
* https://github.com/ReactiveX/RxJava/issues/360#issuecomment-24203016 |
| 138 |
*/ |
| 139 |
@Test |
| 140 |
public void reduceCovariance() { |
| 141 |
// must type it to <Movie> |
| 142 |
Observable<Movie> horrorMovies = Observable.<Movie> just(new HorrorMovie()); |
| 143 |
libraryFunctionActingOnMovieObservables(horrorMovies); |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* This accepts <Movie> instead of <? super Movie> since `reduce` can't handle covariants |
| 148 |
*/ |
| 149 |
public void libraryFunctionActingOnMovieObservables(Observable<Movie> obs) { |
| 150 |
|
| 151 |
obs.reduce(new BiFunction<Movie, Movie, Movie>() { |
| 152 |
@Override |
| 153 |
public Movie apply(Movie t1, Movie t2) { |
| 154 |
return t2; |
| 155 |
} |
| 156 |
}); |
| 157 |
} |
| 158 |
|
| 159 |
} |