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

rx.javafx.sources.ObservableMapSource Maven / Gradle / Ivy

package rx.javafx.sources;

import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;
import rx.Observable;
import rx.schedulers.JavaFxScheduler;
import rx.subscriptions.JavaFxSubscriptions;

import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;

public final class ObservableMapSource {

    private ObservableMapSource() {}

    public static  Observable> fromObservableMap(final ObservableMap source) {
        return Observable.create((Observable.OnSubscribe>) subscriber -> {
            MapChangeListener listener = c -> subscriber.onNext(source);
            source.addListener(listener);
            subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> source.removeListener(listener)));
        }).startWith(source).subscribeOn(JavaFxScheduler.getInstance());
    }

    public static  Observable> fromObservableMapAdds(final ObservableMap source) {

        return Observable.create((Observable.OnSubscribe>) subscriber -> {

            MapChangeListener listener = c -> {

                if (c.wasAdded()) {
                   subscriber.onNext(new SimpleEntry(c.getKey(),c.getValueAdded()));
                }

            };
            source.addListener(listener);
            subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> source.removeListener(listener)));

        }).subscribeOn(JavaFxScheduler.getInstance());
    }

    public static  Observable> fromObservableMapRemovals(final ObservableMap source) {

        return Observable.create((Observable.OnSubscribe>) subscriber -> {

            MapChangeListener listener = c -> {

                if (c.wasRemoved()) {
                    subscriber.onNext(new SimpleEntry(c.getKey(),c.getValueRemoved()));
                }

            };
            source.addListener(listener);
            subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> source.removeListener(listener)));

        }).subscribeOn(JavaFxScheduler.getInstance());
    }

    public static  Observable> fromObservableMapChanges(final ObservableMap source) {

        return Observable.create((Observable.OnSubscribe>) subscriber -> {

            MapChangeListener listener = c -> {

                if (c.wasRemoved()) {
                    subscriber.onNext(new MapChange(c.getKey(),c.getValueRemoved(),Flag.REMOVED));
                }
                if (c.wasAdded()) {
                    subscriber.onNext(new MapChange(c.getKey(),c.getValueAdded(),Flag.ADDED));
                }

            };
            source.addListener(listener);
            subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> source.removeListener(listener)));

        }).subscribeOn(JavaFxScheduler.getInstance());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy