66 lines
2.1 KiB
Java
66 lines
2.1 KiB
Java
|
|
package rx.subjects;
|
||
|
|
|
||
|
|
import java.util.concurrent.ConcurrentHashMap;
|
||
|
|
import rx.Observable;
|
||
|
|
import rx.Observer;
|
||
|
|
import rx.Subscription;
|
||
|
|
import rx.util.AtomicObservableSubscription;
|
||
|
|
import rx.util.SynchronizedObserver;
|
||
|
|
import rx.util.functions.Func1;
|
||
|
|
|
||
|
|
public class Subject<T> extends Observable<T> implements Observer<T> {
|
||
|
|
public static <T> Subject<T> create() {
|
||
|
|
final ConcurrentHashMap<Subscription, Observer<T>> observers = new ConcurrentHashMap<Subscription, Observer<T>>();
|
||
|
|
|
||
|
|
Func1<Observer<T>, Subscription> onSubscribe = new Func1<Observer<T>, Subscription>() {
|
||
|
|
@Override
|
||
|
|
public Subscription call(Observer<T> observer) {
|
||
|
|
final AtomicObservableSubscription subscription = new AtomicObservableSubscription();
|
||
|
|
|
||
|
|
subscription.wrap(new Subscription() {
|
||
|
|
@Override
|
||
|
|
public void unsubscribe() {
|
||
|
|
// on unsubscribe remove it from the map of outbound observers to notify
|
||
|
|
observers.remove(subscription);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// on subscribe add it to the map of outbound observers to notify
|
||
|
|
observers.put(subscription, new SynchronizedObserver<T>(observer, subscription));
|
||
|
|
return subscription;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return new Subject<T>(onSubscribe, observers);
|
||
|
|
}
|
||
|
|
|
||
|
|
private final ConcurrentHashMap<Subscription, Observer<T>> observers;
|
||
|
|
|
||
|
|
protected Subject(Func1<Observer<T>, Subscription> onSubscribe, ConcurrentHashMap<Subscription, Observer<T>> observers) {
|
||
|
|
super(onSubscribe);
|
||
|
|
this.observers = observers;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onCompleted() {
|
||
|
|
for (Observer<T> observer : observers.values()) {
|
||
|
|
observer.onCompleted();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onError(Exception e) {
|
||
|
|
for (Observer<T> observer : observers.values()) {
|
||
|
|
observer.onError(e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onNext(T args) {
|
||
|
|
for (Observer<T> observer : observers.values()) {
|
||
|
|
observer.onNext(args);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|