netflix.ocelli.MutableInstance Maven / Gradle / Ivy
package netflix.ocelli;
import rx.Subscriber;
import rx.functions.Func1;
import rx.subjects.BehaviorSubject;
/**
* Representation of an Instance that can be manually manipulated to set it's up/down
* state as well as final completion (i.e. removal from the pool).
*
* @author elandau
*
* @param
*/
public class MutableInstance extends Instance {
public static MutableInstance from(T value) {
return from(value, true);
}
public static MutableInstance from(T value, boolean initialState) {
return from(value, BehaviorSubject.create(initialState));
}
public static MutableInstance from(T value, BehaviorSubject subject) {
return new MutableInstance(value, subject);
}
public static Func1> toMember() {
return new Func1>() {
@Override
public MutableInstance call(T t) {
return from(t);
}
};
}
private final BehaviorSubject events;
private MutableInstance(T value, final BehaviorSubject events) {
super(value, new OnSubscribe() {
@Override
public void call(Subscriber super Boolean> s) {
events.subscribe(s);
}
});
this.events = events;
}
public void close() {
events.onCompleted();
}
public void setState(boolean isUp) {
events.onNext(isUp);
}
}