Skip to content

Instantly share code, notes, and snippets.

@operando
Last active July 10, 2018 01:36
Show Gist options
  • Save operando/2add7bbad535bc30f7340bf8c04660d7 to your computer and use it in GitHub Desktop.
Save operando/2add7bbad535bc30f7340bf8c04660d7 to your computer and use it in GitHub Desktop.
package rx.playground;
import rx.Observable;
import rx.subjects.BehaviorSubject;
import rx.subjects.SerializedSubject;
public class Variable<T> {
private T value;
private final SerializedSubject<T, T> serializedSubject;
public Variable(T value) {
this.value = value;
serializedSubject = new SerializedSubject<>(BehaviorSubject.create(value));
}
public synchronized T get() {
return value;
}
public synchronized void set(T value) {
this.value = value;
serializedSubject.onNext(this.value);
}
public Observable<T> asObservable() {
return serializedSubject.asObservable();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment