将ObservableList绑定到"可替换的" ImmutableList?

时间:2014-12-22 21:24:24

标签: binding javafx guava javafx-8 observable

我是JavaFX和属性绑定的新手,所以我希望确保尽可能地以最佳方式接近它。我想将TableView绑定到一个ImmutableList,它可以随时用另一个ImmutableList“替换”(比如从数据库中重新提取数据)。所以我想我必须创建List的包装器实现并使其可观察,并且能够随时交换内部支持ImmutableList并实现Observable以通知侦听器。在开始制作这个包装之前,有没有更有效的方法呢?

*** **** UPDATE

我为此设置了一个包装器,到目前为止我的测试已经成功。这是主要方法......

public static void main(String[] args) {
    ImmutableList<String> immutableList1 = ImmutableList.of("ABQDAL", "DALHOU", "HOUPHX");
    ImmutableList<String> immutableList2 = ImmutableList.of("CHILAX", "BURCUN", "SFOPHX");

    System.out.println("IMMUTABLE LIST 1: " + immutableList1);
    System.out.println("IMMUTABLE LIST 2: " + immutableList2);

    System.out.println("CREATING OBSERVABLE IMMUTABLE LIST");
    ObservableImmutableList<String> observableImmutableList = ObservableImmutableList.of(immutableList1);

    System.out.println("OBSERVABLE IMMUTABLE LIST: " + observableImmutableList);

    System.out.println("ADDING LISTENER!");

    InvalidationListener listener = observable -> System.out.println("INVALIDATED!!!");

    observableImmutableList.addListener(listener);

    System.out.println("SWAPPING OBSERVABLE IMMUTABLE LIST wITH IMMUTABLE LIST 2!");
    observableImmutableList.set(immutableList2);
    System.out.println("OBSERVABLE IMMUTABLE LIST " + observableImmutableList);
}

这是班级。关键方法是重载set(ImmutableList<T> immutableList),它使用新的ImmutableList交换旧的ImmutableList,并通知所有侦听器无效。有人看到任何潜在的陷阱,或者这个生产准备好了吗?

import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

import javafx.beans.InvalidationListener;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

import com.google.common.collect.ImmutableList;

public final class ObservableImmutableList<T> implements ObservableList<T> {
    private volatile ImmutableList<T> backingList;
    private final CopyOnWriteArrayList<ListChangeListener<? super T>> listeners = new CopyOnWriteArrayList<>();
    private final CopyOnWriteArrayList<InvalidationListener> invalidationListeners = new CopyOnWriteArrayList<>();

    private ObservableImmutableList(ImmutableList<T> immutableList) {
        this.backingList = immutableList;
    }

    public static <T> ObservableImmutableList<T> of(ImmutableList<T> immutableList) {
        return new ObservableImmutableList<T>(immutableList);
    }

    public void set(ImmutableList<T> immutableList) { 
        this.backingList = immutableList;
        invalidationListeners.forEach(l -> l.invalidated(this));
    }
    @Override
    public int size() {
        return backingList.size();
    }

    @Override
    public boolean isEmpty() {
        return backingList.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return backingList.contains(o);
    }

    @Override
    public Iterator<T> iterator() {
        return backingList.iterator();
    }

    @Override
    public Object[] toArray() {
        return backingList.toArray();
    }

    @Override
    public <B> B[] toArray(B[] a) {
        return backingList.toArray(a);
    }

    @Override @Deprecated
    public boolean add(T e) {
        return backingList.add(e);
    }

    @Override @Deprecated
    public boolean remove(Object o) {
        return backingList.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return backingList.containsAll(c);
    }

    @Override @Deprecated
    public boolean addAll(Collection<? extends T> c) {
        return backingList.addAll(c);
    }

    @Override @Deprecated
    public boolean addAll(int index, Collection<? extends T> c) {
        return backingList.addAll(index, c);
    }

    @Override @Deprecated
    public boolean removeAll(Collection<?> c) {
        return backingList.removeAll(c);
    }

    @Override @Deprecated
    public boolean retainAll(Collection<?> c) {
        return backingList.retainAll(c);
    }

    @Override @Deprecated
    public void clear() {
        backingList.clear();
    }

    @Override
    public T get(int index) {
        return backingList.get(index);
    }

    @Override @Deprecated
    public T set(int index, T element) {
        return backingList.set(index, element);
    }

    @Override @Deprecated
    public void add(int index, T element) {
        backingList.add(index, element);
    }

    @Override @Deprecated
    public T remove(int index) {
        return backingList.remove(index);
    }

    @Override
    public int indexOf(Object o) {
        return backingList.indexOf(o);
    }

    @Override
    public int lastIndexOf(Object o) {
        return backingList.lastIndexOf(o);
    }

    @Override
    public ListIterator<T> listIterator() {
        return backingList.listIterator();
    }

    @Override
    public ListIterator<T> listIterator(int index) {
        return backingList.listIterator(index);
    }

    @Override
    public ImmutableList<T> subList(int fromIndex, int toIndex) {
        return backingList.subList(fromIndex, toIndex);
    }

    @Override
    public void addListener(InvalidationListener listener) {
        invalidationListeners.add(listener);
    }

    @Override
    public void removeListener(InvalidationListener listener) {
        invalidationListeners.remove(listener);
    }

    @Override
    public void addListener(ListChangeListener<? super T> listener) {
        listeners.add(listener);
    }

    @Override
    public void removeListener(ListChangeListener<? super T> listener) {
        listeners.remove(listener);
    }

    @Override @Deprecated
    public boolean addAll(T... elements) {
        return backingList.addAll(ImmutableList.copyOf(elements));
    }

    @Override @Deprecated
    public boolean setAll(T... elements) {
        return false;
    }

    @Override @Deprecated
    public boolean setAll(Collection<? extends T> col) {
        return false;
    }

    @Override @Deprecated
    public boolean removeAll(T... elements) {
        return backingList.removeAll(ImmutableList.copyOf(elements));
    }

    @Override @Deprecated
    public boolean retainAll(T... elements) {
        return false;
    }

    @Override @Deprecated
    public void remove(int from, int to) {
    }

    @Override
    public String toString() { 
        return backingList.toString();
    }

}

0 个答案:

没有答案