All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.ocelli.MutableInstance Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
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 s) {
                events.subscribe(s);
            }
        });
        this.events = events;
    }

    public void close() {
        events.onCompleted();
    }
    
    public void setState(boolean isUp) {
        events.onNext(isUp);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy