Why is there no ObservableQueues in JavaFX?

Why is there no ObservableQueue in JavaFX? If we look at the Java 9 documentation (just to see if there are any changes from 8) for FXCollections, we see static helper methods for creating sets, lists, and Observable maps. There are also some methods for creating Observable float and integer arrays. However, there is no way to create an ObservableQueue. The Queue interface in Java has many interesting implementations, including ArrayDeque, DelayQueue, ConcurrentLinkedQueue, PriorityQueue, etc. What is the logic of the reason there is no support for creating ObservableQueues in JavaFX.

+6
source share
1 answer

As @TomasMikula comments in @eckig's answer (now deleted), this is most likely not enough for an ObservableQueue . If you have a tough precedent, you should consider sending a function request .

Meanwhile, creating an opaque ObservableQueue implementation of Queue and adding “observability” by subclassing the ObservableListBase and wrapping the Queue implementation is not difficult. The Subclass ObservableListBase is the “fast” part, but also the “dirty” part, because you expand the List methods as well as the Queue methods; since an arbitrary Queue does not have get(int index) , the only way to implement this (I see) is to index over to index . Everything that get uses to iterate through an ObservableQueue , treating it as a List , will execute in O(n^2) time. With this caveat, the following should work very well:

 import java.util.LinkedList; import java.util.Queue; import javafx.collections.ObservableListBase; public class ObservableQueue<E> extends ObservableListBase<E> implements Queue<E> { private final Queue<E> queue ; /** * Creates an ObservableQueue backed by the supplied Queue. * Note that manipulations of the underlying queue will not result * in notification to listeners. * * @param queue */ public ObservableQueue(Queue<E> queue) { this.queue = queue ; } /** * Creates an ObservableQueue backed by a LinkedList. */ public ObservableQueue() { this(new LinkedList<>()); } @Override public boolean offer(E e) { beginChange(); boolean result = queue.offer(e); if (result) { nextAdd(queue.size()-1, queue.size()); } endChange(); return result ; } @Override public boolean add(E e) { beginChange() ; try { queue.add(e); nextAdd(queue.size()-1, queue.size()); return true ; } finally { endChange(); } } @Override public E remove() { beginChange(); try { E e = queue.remove(); nextRemove(0, e); return e; } finally { endChange(); } } @Override public E poll() { beginChange(); E e = queue.poll(); if (e != null) { nextRemove(0, e); } endChange(); return e ; } @Override public E element() { return queue.element(); } @Override public E peek() { return queue.peek(); } @Override public E get(int index) { Iterator<E> iterator = queue.iterator(); for (int i = 0; i < index; i++) iterator.next(); return iterator.next(); } @Override public int size() { return queue.size(); } } 

You can register a ListChangeListener with this to receive notification of changes in the queue. (Note: if you want to support extractors and update notifications, you will need to do some more work ...).

 import javafx.collections.ListChangeListener.Change; public class ObservableQueueTest { public static void main(String[] args) { ObservableQueue<String> oq = new ObservableQueue<>(); oq.addListener((Change<? extends String> change) -> { while (change.next()) { if (change.wasAdded()) { System.out.println("Added: "+change.getAddedSubList()); } if (change.wasRemoved()) { System.out.println("Removed: "+change.getRemoved()); } if (change.wasUpdated()) { System.out.println("Updated: "+oq.subList(change.getFrom(), change.getTo())); } if (change.wasReplaced()) { System.out.println("Replaced"); } } }); oq.offer("One"); oq.offer("Two"); oq.offer("Three"); System.out.println("Peek: "+oq.peek()); System.out.println("Remove..."); System.out.println(oq.remove()); System.out.println("Element:"); System.out.println(oq.element()); System.out.println("get(1): "+oq.get(1)); System.out.println("Poll: "); System.out.println(oq.poll()); System.out.println("Poll again:"); System.out.println(oq.poll()); System.out.println("Poll should return null:"); System.out.println(oq.poll()); System.out.println("Element should throw exception:"); System.out.println(oq.element()); } } 
+6
source

Source: https://habr.com/ru/post/982310/


All Articles