|
| 1 | +package rx.subjects; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 7 | +import java.util.concurrent.atomic.AtomicReference; |
| 8 | + |
| 9 | +import junit.framework.Assert; |
| 10 | + |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import rx.observables.Notification; |
| 14 | +import rx.observables.Observable; |
| 15 | +import rx.observables.Observer; |
| 16 | +import rx.observables.Subscription; |
| 17 | +import rx.util.AtomicObservableSubscription; |
| 18 | +import rx.util.AtomicObserver; |
| 19 | +import rx.util.functions.Action1; |
| 20 | +import rx.util.functions.Func1; |
| 21 | + |
| 22 | +public class Subject<T> extends Observable<T> implements Observer<T> { |
| 23 | + public static <T> Subject<T> create() { |
| 24 | + final ConcurrentHashMap<Subscription, Observer<T>> observers = new ConcurrentHashMap<Subscription, Observer<T>>(); |
| 25 | + |
| 26 | + Func1<Observer<T>, Subscription> onSubscribe = new Func1<Observer<T>, Subscription>() { |
| 27 | + @Override |
| 28 | + public Subscription call(Observer<T> observer) { |
| 29 | + final AtomicObservableSubscription subscription = new AtomicObservableSubscription(); |
| 30 | + |
| 31 | + subscription.wrap(new Subscription() { |
| 32 | + @Override |
| 33 | + public void unsubscribe() { |
| 34 | + // on unsubscribe remove it from the map of outbound observers to notify |
| 35 | + observers.remove(subscription); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + // on subscribe add it to the map of outbound observers to notify |
| 40 | + observers.put(subscription, new AtomicObserver<T>(observer, subscription)); |
| 41 | + return subscription; |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + return new Subject<T>(onSubscribe, observers); |
| 46 | + } |
| 47 | + |
| 48 | + private final ConcurrentHashMap<Subscription, Observer<T>> observers; |
| 49 | + |
| 50 | + protected Subject(Func1<Observer<T>, Subscription> onSubscribe, ConcurrentHashMap<Subscription, Observer<T>> observers) { |
| 51 | + super(onSubscribe); |
| 52 | + this.observers = observers; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void onCompleted() { |
| 57 | + for (Observer<T> observer : observers.values()) { |
| 58 | + observer.onCompleted(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onError(Exception e) { |
| 64 | + for (Observer<T> observer : observers.values()) { |
| 65 | + observer.onError(e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onNext(T args) { |
| 71 | + for (Observer<T> observer : observers.values()) { |
| 72 | + observer.onNext(args); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static class UnitTest { |
| 77 | + @Test |
| 78 | + public void test() { |
| 79 | + Subject<Integer> subject = Subject.<Integer> create(); |
| 80 | + final AtomicReference<List<Notification<String>>> actualRef = new AtomicReference<List<Notification<String>>>(); |
| 81 | + |
| 82 | + Observable<List<Notification<Integer>>> wNotificationsList = subject.materialize().toList(); |
| 83 | + wNotificationsList.subscribe(new Action1<List<Notification<String>>>() { |
| 84 | + @Override |
| 85 | + public void call(List<Notification<String>> actual) { |
| 86 | + actualRef.set(actual); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + Subscription sub = Observable.create(new Func1<Observer<Integer>, Subscription>() { |
| 91 | + @Override |
| 92 | + public Subscription call(final Observer<Integer> observer) { |
| 93 | + final AtomicBoolean stop = new AtomicBoolean(false); |
| 94 | + new Thread() { |
| 95 | + @Override |
| 96 | + public void run() { |
| 97 | + int i = 1; |
| 98 | + while (!stop.get()) { |
| 99 | + observer.onNext(i++); |
| 100 | + } |
| 101 | + observer.onCompleted(); |
| 102 | + } |
| 103 | + }.start(); |
| 104 | + return new Subscription() { |
| 105 | + @Override |
| 106 | + public void unsubscribe() { |
| 107 | + stop.set(true); |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | + }).subscribe(subject); |
| 112 | + // the subject has received an onComplete from the first subscribe because |
| 113 | + // it is synchronous and the next subscribe won't do anything. |
| 114 | + Observable.toObservable(-1, -2, -3).subscribe(subject); |
| 115 | + |
| 116 | + List<Notification<Integer>> expected = new ArrayList<Notification<Integer>>(); |
| 117 | + expected.add(new Notification<Integer>(-1)); |
| 118 | + expected.add(new Notification<Integer>(-2)); |
| 119 | + expected.add(new Notification<Integer>(-3)); |
| 120 | + expected.add(new Notification<Integer>()); |
| 121 | + Assert.assertTrue(actualRef.get().containsAll(expected)); |
| 122 | + |
| 123 | + sub.unsubscribe(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments