| 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.flowable; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
import java.util.concurrent.ConcurrentLinkedQueue; |
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.junit.*; |
| 21 |
import org.reactivestreams.*; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.internal.operators.flowable.*; |
| 26 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 27 |
import io.reactivex.rxjava3.observers.*; |
| 28 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 29 |
import io.reactivex.rxjava3.subscribers.DefaultSubscriber; |
| 30 |
|
| 31 |
public class FlowableConversionTest extends RxJavaTest { |
| 32 |
|
| 33 |
public static class Cylon { } |
| 34 |
|
| 35 |
public static class Jail { |
| 36 |
Object cylon; |
| 37 |
|
| 38 |
Jail(Object cylon) { |
| 39 |
this.cylon = cylon; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public static class CylonDetectorObservable<T> { |
| 44 |
protected Publisher<T> onSubscribe; |
| 45 |
|
| 46 |
public static <T> CylonDetectorObservable<T> create(Publisher<T> onSubscribe) { |
| 47 |
return new CylonDetectorObservable<>(onSubscribe); |
| 48 |
} |
| 49 |
|
| 50 |
protected CylonDetectorObservable(Publisher<T> onSubscribe) { |
| 51 |
this.onSubscribe = onSubscribe; |
| 52 |
} |
| 53 |
|
| 54 |
public void subscribe(Subscriber<T> subscriber) { |
| 55 |
onSubscribe.subscribe(subscriber); |
| 56 |
} |
| 57 |
|
| 58 |
public <R> CylonDetectorObservable<R> lift(FlowableOperator<? extends R, ? super T> operator) { |
| 59 |
return x(new RobotConversionFunc<>(operator)); |
| 60 |
} |
| 61 |
|
| 62 |
public <O> O x(Function<Publisher<T>, O> operator) { |
| 63 |
try { |
| 64 |
return operator.apply(onSubscribe); |
| 65 |
} catch (Throwable ex) { |
| 66 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public <R> CylonDetectorObservable<? extends R> compose(Function<CylonDetectorObservable<? super T>, CylonDetectorObservable<? extends R>> transformer) { |
| 71 |
try { |
| 72 |
return transformer.apply(this); |
| 73 |
} catch (Throwable ex) { |
| 74 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public final CylonDetectorObservable<T> beep(Predicate<? super T> predicate) { |
| 79 |
return new CylonDetectorObservable<>(new FlowableFilter<>(Flowable.fromPublisher(onSubscribe), predicate)); |
| 80 |
} |
| 81 |
|
| 82 |
public final <R> CylonDetectorObservable<R> boop(Function<? super T, ? extends R> func) { |
| 83 |
return new CylonDetectorObservable<>(new FlowableMap<>(Flowable.fromPublisher(onSubscribe), func)); |
| 84 |
} |
| 85 |
|
| 86 |
public CylonDetectorObservable<String> DESTROY() { |
| 87 |
return boop(new Function<T, String>() { |
| 88 |
@Override |
| 89 |
public String apply(T t) { |
| 90 |
Object cylon = ((Jail) t).cylon; |
| 91 |
throwOutTheAirlock(cylon); |
| 92 |
if (t instanceof Jail) { |
| 93 |
String name = cylon.toString(); |
| 94 |
return "Cylon '" + name + "' has been destroyed"; |
| 95 |
} |
| 96 |
else { |
| 97 |
return "Cylon 'anonymous' has been destroyed"; |
| 98 |
} |
| 99 |
}}); |
| 100 |
} |
| 101 |
|
| 102 |
private static void throwOutTheAirlock(Object cylon) { |
| 103 |
// ... |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public static class RobotConversionFunc<T, R> implements Function<Publisher<T>, CylonDetectorObservable<R>> { |
| 108 |
private FlowableOperator<? extends R, ? super T> operator; |
| 109 |
|
| 110 |
public RobotConversionFunc(FlowableOperator<? extends R, ? super T> operator) { |
| 111 |
this.operator = operator; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public CylonDetectorObservable<R> apply(final Publisher<T> onSubscribe) { |
| 116 |
return CylonDetectorObservable.create(new Publisher<R>() { |
| 117 |
@Override |
| 118 |
public void subscribe(Subscriber<? super R> subscriber) { |
| 119 |
try { |
| 120 |
Subscriber<? super T> st = operator.apply(subscriber); |
| 121 |
try { |
| 122 |
onSubscribe.subscribe(st); |
| 123 |
} catch (Throwable e) { |
| 124 |
st.onError(e); |
| 125 |
} |
| 126 |
} catch (Throwable e) { |
| 127 |
subscriber.onError(e); |
| 128 |
} |
| 129 |
|
| 130 |
}}); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public static class ConvertToCylonDetector<T> implements FlowableConverter<T, CylonDetectorObservable<T>> { |
| 135 |
@Override |
| 136 |
public CylonDetectorObservable<T> apply(final Flowable<T> onSubscribe) { |
| 137 |
return CylonDetectorObservable.create(onSubscribe); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
public static class ConvertToObservable<T> implements Function<Publisher<T>, Flowable<T>> { |
| 142 |
@Override |
| 143 |
public Flowable<T> apply(final Publisher<T> onSubscribe) { |
| 144 |
return Flowable.fromPublisher(onSubscribe); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void conversionBetweenObservableClasses() { |
| 150 |
final TestObserver<String> to = new TestObserver<>(new DefaultObserver<String>() { |
| 151 |
|
| 152 |
@Override |
| 153 |
public void onComplete() { |
| 154 |
System.out.println("Complete"); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void onError(Throwable e) { |
| 159 |
System.out.println("error: " + e.getMessage()); |
| 160 |
e.printStackTrace(); |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public void onNext(String t) { |
| 165 |
System.out.println(t); |
| 166 |
} |
| 167 |
}); |
| 168 |
|
| 169 |
List<Object> crewOfBattlestarGalactica = Arrays.asList(new Object[] {"William Adama", "Laura Roslin", "Lee Adama", new Cylon()}); |
| 170 |
|
| 171 |
Flowable.fromIterable(crewOfBattlestarGalactica) |
| 172 |
.doOnNext(new Consumer<Object>() { |
| 173 |
@Override |
| 174 |
public void accept(Object pv) { |
| 175 |
System.out.println(pv); |
| 176 |
} |
| 177 |
}) |
| 178 |
.to(new ConvertToCylonDetector<>()) |
| 179 |
.beep(new Predicate<Object>() { |
| 180 |
@Override |
| 181 |
public boolean test(Object t) { |
| 182 |
return t instanceof Cylon; |
| 183 |
} |
| 184 |
}) |
| 185 |
.boop(new Function<Object, Object>() { |
| 186 |
@Override |
| 187 |
public Object apply(Object cylon) { |
| 188 |
return new Jail(cylon); |
| 189 |
} |
| 190 |
}) |
| 191 |
.DESTROY() |
| 192 |
.x(new ConvertToObservable<>()) |
| 193 |
.reduce("Cylon Detector finished. Report:\n", new BiFunction<String, String, String>() { |
| 194 |
@Override |
| 195 |
public String apply(String a, String n) { |
| 196 |
return a + n + "\n"; |
| 197 |
} |
| 198 |
}) |
| 199 |
.subscribe(to); |
| 200 |
|
| 201 |
to.assertNoErrors(); |
| 202 |
to.assertComplete(); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void convertToConcurrentQueue() { |
| 207 |
final AtomicReference<Throwable> thrown = new AtomicReference<>(null); |
| 208 |
final AtomicBoolean isFinished = new AtomicBoolean(false); |
| 209 |
ConcurrentLinkedQueue<? extends Integer> queue = Flowable.range(0, 5) |
| 210 |
.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 211 |
@Override |
| 212 |
public Publisher<Integer> apply(final Integer i) { |
| 213 |
return Flowable.range(0, 5) |
| 214 |
.observeOn(Schedulers.io()) |
| 215 |
.map(new Function<Integer, Integer>() { |
| 216 |
@Override |
| 217 |
public Integer apply(Integer k) { |
| 218 |
try { |
| 219 |
Thread.sleep(System.currentTimeMillis() % 100); |
| 220 |
} catch (InterruptedException e) { |
| 221 |
e.printStackTrace(); |
| 222 |
} |
| 223 |
return i + k; |
| 224 |
} |
| 225 |
}); |
| 226 |
} |
| 227 |
}) |
| 228 |
.to(new FlowableConverter<Integer, ConcurrentLinkedQueue<Integer>>() { |
| 229 |
@Override |
| 230 |
public ConcurrentLinkedQueue<Integer> apply(Flowable<Integer> onSubscribe) { |
| 231 |
final ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>(); |
| 232 |
onSubscribe.subscribe(new DefaultSubscriber<Integer>() { |
| 233 |
@Override |
| 234 |
public void onComplete() { |
| 235 |
isFinished.set(true); |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void onError(Throwable e) { |
| 240 |
thrown.set(e); |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public void onNext(Integer t) { |
| 245 |
q.add(t); |
| 246 |
}}); |
| 247 |
return q; |
| 248 |
} |
| 249 |
}); |
| 250 |
|
| 251 |
int x = 0; |
| 252 |
while (!isFinished.get()) { |
| 253 |
Integer i = queue.poll(); |
| 254 |
if (i != null) { |
| 255 |
x++; |
| 256 |
System.out.println(x + " item: " + i); |
| 257 |
} |
| 258 |
} |
| 259 |
Assert.assertNull(thrown.get()); |
| 260 |
} |
| 261 |
} |