Change the license to GPL v3 to be compatible with Apache License 2.0.
This commit is contained in:
1 parent
f0212ea4eb
commit
d3ad805ad3
331 files changed
+12761
-1878
No files matched your search
@@ -0,0 +1,65 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user