是否有类似ObservableConcurrentLinkedDeque的东西?

时间:2015-10-07 17:33:46

标签: java model-view-controller javafx

在我的代码中,我需要一个ConcurrentLinkedDeque,但是我想将这个Deque biiderectional绑定到JavaFX中的TableView或者至少将Deque的大小绑定到PieChart,这是做这样的事情的常用方法。是否有类似ObservableConcurrentLinkedDeque的东西我可以使用而不是ConcurrentLinkedDeque并直接绑定到TableView?

1 个答案:

答案 0 :(得分:1)

JavaFX库中没有这样的实现。

请注意,实现任何类型的并发集合以用作RAILS_ENV=production PIDFILE=./resque.pid BACKGROUND=yes QUEUE="*" rake resque:work >> worker1.log (或绑定到其状态的任何其他JavaFX节点)的后备列表实际上没有意义。一旦将其用作UI节点的后备数据,就只能从JavaFX线程访问它,因此使其成为线程安全是多余的。因此,您可以减少要求一个也是TableView的可观察列表。

为此,您可以继承ModifiableObservableListBase,将其委托给Deque,并实施LinkedList,同时将这些方法委托给Deque。当您调用修改列表的LinkedList方法时,您只需要小心触发更改。如下所示:

Deque

用法示例:

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;

import javafx.collections.ModifiableObservableListBase;


public class ObservableLinkedList<T> extends ModifiableObservableListBase<T> implements Deque<T> {

    private final LinkedList<T> list = new LinkedList<>();

    @Override
    public void addFirst(T e) {
        list.addFirst(e);
        beginChange();
        nextAdd(0, 1);
        ++modCount ;
        endChange();
    }

    @Override
    public void addLast(T e) {
        list.addLast(e);
        int size = list.size();
        beginChange();
        nextAdd(size-1, size);
        ++modCount ;
        endChange();
    }

    @Override
    public boolean offerFirst(T e) {
        addFirst(e);
        return true ;
    }

    @Override
    public boolean offerLast(T e) {
        addLast(e);
        return true ;
    }

    @Override
    public T removeFirst() {
        T old = list.removeFirst() ;
        beginChange();
        nextRemove(0, old);
        ++modCount ;
        endChange();
        return old ;
    }

    @Override
    public T removeLast() {
        T old = list.removeLast() ;
        beginChange();
        nextRemove(list.size(), old);
        ++modCount ;
        endChange();
        return old ;
    }

    @Override
    public T pollFirst() {
        T result = list.pollFirst();
        if (result != null) {
            beginChange();
            nextRemove(0, result);
            ++modCount ;
            endChange();
        }
        return result ;
    }

    @Override
    public T pollLast() {
        T result = list.pollLast();
        if (result != null) {
            beginChange();
            nextRemove(list.size(), result);
            ++modCount ;
            endChange();
        }
        return result ;
    }

    @Override
    public T getFirst() {
        return list.getFirst() ;
    }

    @Override
    public T getLast() {
        return list.getLast() ;
    }

    @Override
    public T peekFirst() {
        return list.peekFirst() ;
    }

    @Override
    public T peekLast() {
        return list.peekLast() ;
    }

    @Override
    public boolean removeFirstOccurrence(Object o) {

        // not efficient: maybe a more efficient way, but we need the index...
        int index = list.indexOf(o);
        if (index > -1) {
            remove(index);
            return true ;
        } else {
            return false ;
        }
    }

    @Override
    public boolean removeLastOccurrence(Object o) {

        // not efficient: maybe a more efficient way, but we need the index...
        int index = list.lastIndexOf(o);
        if (index > -1) {
            remove(index);
            return true ;
        } else {
            return false ;
        }
    }

    @Override
    public boolean offer(T e) {
        return offerLast(e);
    }

    @Override
    public T remove() {
        return removeFirst();
    }

    @Override
    public T poll() {
        return pollFirst();
    }

    @Override
    public T element() {
        return getFirst();
    }

    @Override
    public T peek() {
        return peekFirst();
    }

    @Override
    public void push(T e) {
        addFirst(e);
    }

    @Override
    public T pop() {
        return removeFirst();
    }

    @Override
    public Iterator<T> descendingIterator() {
        return list.descendingIterator();
    }

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

    @Override
    public int size() {
        return list.size();
    }

    @Override
    protected void doAdd(int index, T element) {
        list.add(index, element);
    }

    @Override
    protected T doSet(int index, T element) {
        return list.set(index, element);
    }

    @Override
    protected T doRemove(int index) {
        return list.remove(index);
    }

}