public abstract class SparseThreadProxyEventList<E> extends TransformedList<E,E> implements RandomAccess, SparseEventList<E>
EventList
that only forwards its events on a proxy thread, regardless of the thread of
their origin.
While the proxy thread is not executing, any number of events may arrive. This means that
multiple changes may have occurred before the proxy can notify its listeners. One problem created
by this scenario is that some of these changes may contradict one another. For example, an
inserted item may be later removed before either event is processed. To overcome this problem,
this class uses the change-contradiction resolving logic of ListEventAssembler
.
The flow of events is as follows:
listChanged(ListEvent)
.
At some point in the future, after one or more events have been enqueued, the proxy thread executes and processes its queue:
SortedList
Comparator
.
SparseThreadProxyEventList
itself. It listens to its own
event because this event will be free of conflicts. It applies the changes to its own private
copy of the data.
The SparseThreadProxyEventList
keeps a private copy of the elements of the source
EventList
. This enables interested classes to read a consistent (albeit potentially out
of date) view of the data at all times.
Important: ThreadProxyEventList relies heavily on
its ability to pause changes to its source EventList while it is updating its private copy of the
source data. It does this by acquiring the writeLock for the list pipeline. This implies that
ALL code which accesses the pipeline must be thread-safe (or the acquisition of
the writeLock will be meaningless). See EventList
for an example of thread safe code.
SparseList.Span
source
publisher, readWriteLock, updates
Constructor and Description |
---|
SparseThreadProxyEventList(SparseEventList<E> source)
Create a
SparseThreadProxyEventList which delivers changes to the given
source on a particular Thread , called the proxy Thread of a
subclasses choosing. |
Modifier and Type | Method and Description |
---|---|
boolean |
addAll(int index,
Collection<? extends E> values)
Inserts all of the elements in the specified collection into this list at the specified
position (optional operation).
|
void |
addNulls(int index,
int length)
Add/insert a sequence of
null at the given index (in)to the list. |
E |
clear(int index)
Clear the element at the given index.
|
void |
dispose()
Releases the resources consumed by this
TransformedList so that it may eventually be
garbage collected. |
boolean |
equals(Object object)
Compares the specified object with this list for equality.
|
E |
get(int index)
Returns the element at the specified position in this list.
|
E |
getNullElement(int index) |
int |
hashCode()
Returns the hash code value for this list.
|
int |
indexOf(Object object)
Returns the index in this list of the first occurrence of the specified element, or -1 if this
list does not contain this element.
|
protected boolean |
isWritable()
Gets whether the source
EventList is writable via this API. |
void |
listChanged(ListEvent<E> listChanges)
When the underlying list changes, this notification allows the object to repaint itself or
update itself as necessary.
|
protected abstract void |
schedule(Runnable runnable,
boolean deferred)
Schedule the specified runnable to be executed on the proxy thread.
|
int |
size()
Returns the number of elements in this list.
|
Iterator<SparseList.Span> |
spanIterator()
Return an iterator over the populated
SparseList.Span s of this sparse list. |
Iterator<SparseList.Span> |
spanIterator(int min,
int max)
Return an iterator over the populated
SparseList.Span s of this sparse list. |
add, clear, getSourceIndex, remove, removeAll, retainAll, set
add, addAll, addListEventListener, contains, containsAll, getPublisher, getReadWriteLock, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeListEventListener, subList, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addListEventListener, getPublisher, getReadWriteLock, removeListEventListener
add, add, addAll, clear, contains, containsAll, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, subList, toArray, toArray
parallelStream, removeIf, stream
public SparseThreadProxyEventList(SparseEventList<E> source)
SparseThreadProxyEventList
which delivers changes to the given
source
on a particular Thread
, called the proxy Thread
of a
subclasses choosing. The Thread
used depends on the implementation of
schedule(Runnable, boolean)
.source
- the EventList
for which to proxy eventspublic final void listChanged(ListEvent<E> listChanges)
It is mandatory that the calling thread has obtained the write lock on the source list. This is because the calling thread will have written to the source list to cause this event. This condition guarantees that no writes can occur while the listener is handling this event. It is an error to write to the source list while processing an event.
listChanged
in interface ListEventListener<E>
listChanged
in class TransformedList<E,E>
listChanges
- a ListEvent
describing the changes to the listprotected abstract void schedule(Runnable runnable, boolean deferred)
runnable
- a unit of work to be executed on the proxy threaddeferred
- TODOpublic final int size()
Integer.MAX_VALUE
elements, returns Integer.MAX_VALUE
.size
in interface Collection<E>
size
in interface List<E>
size
in class TransformedList<E,E>
public final E get(int index)
protected final boolean isWritable()
EventList
is writable via this API.
Extending classes must override this method in order to make themselves writable.
isWritable
in class TransformedList<E,E>
public void dispose()
TransformedList
so that it may eventually be
garbage collected.
A TransformedList
will be garbage collected without a call to dispose()
, but
not before its source EventList
is garbage collected. By calling #dispose()
,
you allow the TransformedList
to be garbage collected before its source
EventList
. This is necessary for situations where a TransformedList
is
short-lived but its source EventList
is long-lived.
Warning: It is an error to call any method on a
TransformedList
after it has been disposed.
public boolean addAll(int index, Collection<? extends E> values)
TransformedList
public Iterator<SparseList.Span> spanIterator()
SparseList
SparseList.Span
s of this sparse list. A span is a
consecutive range of non-null
entries in the list.spanIterator
in interface SparseList<E>
public Iterator<SparseList.Span> spanIterator(int min, int max)
SparseList
SparseList.Span
s of this sparse list. A span is a
consecutive range of non-null
entries in the list. Only entries within the range
[min..max]
are considered.spanIterator
in interface SparseList<E>
min
- the minimum index to considermax
- the maximum index to considerpublic void addNulls(int index, int length)
SparseList
null
at the given index (in)to the list.addNulls
in interface SparseList<E>
public int hashCode()
AbstractEventList
hashCode = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); }This ensures that
list1.equals(list2)
implies that
list1.hashCode()==list2.hashCode()
for any two lists, list1
and
list2
, as required by the general contract of Object.hashCode
.hashCode
in interface Collection<E>
hashCode
in interface List<E>
hashCode
in class AbstractEventList<E>
Object.hashCode()
,
Object.equals(Object)
,
AbstractEventList.equals(Object)
public boolean equals(Object object)
AbstractEventList
true
if and only if
the specified object is also a list, both lists have the same size, and all corresponding pairs
of elements in the two lists are equal. (Two elements e1
and e2
are
equal if (e1==null ? e2==null :
e1.equals(e2))
.) In other words, two lists are defined to be equal if they contain the
same elements in the same order. This definition ensures that the equals method works properly
across different implementations of the List
interface.equals
in interface Collection<E>
equals
in interface List<E>
equals
in class AbstractEventList<E>
object
- the object to be compared for equality with this list.true
if the specified object is equal to this list.public E getNullElement(int index)
getNullElement
in interface SparseEventList<E>
null
-element).public int indexOf(Object object)
AbstractEventList
i
such
that (o==null ? get(i)==null : o.equals(get(i)))
, or -1 if there is no such index.public E clear(int index)
SparseList
List.remove(int)
, the indices of all
other elements are unaffected by this operation.clear
in interface SparseList<E>
Copyright © 2024 levigo holding gmbh. All rights reserved.