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

org.reactfx.collection.ValAsList Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.reactfx.collection;

import javafx.beans.value.ObservableValue;

import org.reactfx.Subscription;
import org.reactfx.util.Lists;
import org.reactfx.value.Val;

class ValAsList extends LiveListBase implements UnmodifiableByDefaultLiveList {
    private final ObservableValue underlying;

    ValAsList(ObservableValue underlying) {
        this.underlying = underlying;
    }

    @Override
    public int size() {
        return underlying.getValue() == null ? 0 : 1;
    }

    @Override
    public T get(int index) {
        Lists.checkIndex(index, size());
        return underlying.getValue();
    }

    @Override
    protected Subscription observeInputs() {
        return Val.observeChanges(underlying, (obs, oldVal, newVal) -> {
            if(oldVal == null) {
                assert newVal != null;
                fireElemInsertion(0);
            } else if(newVal == null) {
                assert oldVal != null; // superfluous, just for symmetry
                fireElemRemoval(0, oldVal);
            } else {
                fireElemReplacement(0, oldVal);
            }
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy