V
- The type of value elements in this list.R
- The type of reflected value elements in the mirror list.public class MirrorList<V,R> extends ObservableList<V>
<V>
, which is the generic type of
this
list.<R>
, which is the generic type of
the list returned by getMirrorList()
MirrorList.Mirror
provides the V ->
R
and R -> V
methods, which are
used to reflect values from one MirrorList
to the other.
Elements added to either sides of the MirrorList
are "lazy-reflected"
-- i.e. instead of reflecting elements upon their addition to the list,
elements are reflected upon retrieval from the list.
The getMirrorList()
method returns the MirrorList
instance
of type <R>
that represents the one-to-one mapping with its mirror of
type <V>
(i.e. this
instance). Changes to the
MirrorList
will be reflected in this
instance, and
vice-versa.
Modifier and Type | Class and Description |
---|---|
static interface |
MirrorList.Mirror<V,R>
Interface providing methods for the reflection of one type of object to
another, and vice-versa.
|
ObservableList.ObservableListIterator, ObservableList.ObservableSubList
Modifier and Type | Field and Description |
---|---|
protected boolean |
inited |
protected MirrorList<R,V> |
mirrorList |
protected static Object |
PENDING
Object to be used in the underlying lists to represent that a value is
pending reflection via the methods in
mirror upon the invocation
of beforeGet(int,ListIterator) . |
protected Iterator<?> |
targetLock |
protected boolean |
unlocked |
target
modCount
Modifier | Constructor and Description |
---|---|
|
MirrorList(List<V> values,
List<R> reflections,
MirrorList.Mirror<V,R> mirror)
Creates a new
MirrorList with the specified target lists and
MirrorList.Mirror . |
protected |
MirrorList(MirrorList<R,V> mirrorList,
List<V> values,
MirrorList.Mirror<V,R> mirror)
Creates a new
MirrorList with the specified lists and mirror. |
Modifier and Type | Method and Description |
---|---|
protected void |
afterAdd(int index,
V element,
RuntimeException e)
Callback method that is invoked immediately after an element is added to
the enclosed
List . |
protected void |
afterGet(int index,
V element,
ListIterator<? super V> iterator,
RuntimeException e)
Callback method that is invoked immediately after an element is retrieved
from the enclosed
List . |
protected void |
afterRemove(Object element,
RuntimeException e)
Callback method that is invoked immediately after an element is removed
from the enclosed
List . |
protected void |
afterSet(int index,
V oldElement,
RuntimeException e)
Callback method that is invoked immediately after an element is set at an
index to the enclosed
List . |
protected boolean |
beforeAdd(int index,
V element)
Callback method that is invoked immediately before an element is added to
the enclosed
List . |
protected void |
beforeGet(int index,
ListIterator<V> iterator)
Callback method that is invoked immediately before an element is retrieved
from the enclosed
List . |
protected boolean |
beforeRemove(int index)
Callback method that is invoked immediately before an element is removed
from the enclosed
List . |
protected boolean |
beforeSet(int index,
V newElement)
Callback method that is invoked immediately before an element is set at an
index in the enclosed
List . |
MirrorList.Mirror<V,R> |
getMirror()
Returns the
MirrorList.Mirror . |
MirrorList<R,V> |
getMirrorList()
Returns the
MirrorList instance of type <R> that represents
the one-to-one mapping with its mirror of type <V> (i.e. |
protected MirrorList.Mirror<R,V> |
getReverseMirror()
Returns the reverse
MirrorList.Mirror , and caches it for subsequent retrieval,
avoiding reinstantiation. |
protected void |
lock(boolean unlocked)
Locks the
underlying list to detect concurrent
modification. |
protected MirrorList<V,R> |
newInstance(List<V> values,
List<R> reflections)
Factory method that returns a new instance of a
MirrorList with the
specified target lists. |
protected MirrorList<R,V> |
newMirrorInstance(List<R> values)
Factory method that returns a new mirror instance of a
MirrorList with the specified target list. |
MirrorList<R,V> |
reverse()
|
int |
size() |
MirrorList<V,R> |
subList(int fromIndex,
int toIndex) |
protected boolean |
unlock()
Unlocks the
underlying list and checks for concurrent
modification. |
add, add, addAll, addAll, addFast, clear, contains, containsAll, equals, forEach, get, getFast, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, newListIterator, newSubList, parallelStream, remove, remove, removeAll, removeFast, removeIf, replaceAll, retainAll, set, sort, spliterator, stream, toArray, toArray, toString
isEmpty, superForEach, superParallelStream, superRemoveIf, superReplaceAll, superSort, superSpliterator, superStream
removeRange
protected static final Object PENDING
mirror
upon the invocation
of beforeGet(int,ListIterator)
.protected MirrorList<R,V> mirrorList
protected Iterator<?> targetLock
protected boolean inited
protected boolean unlocked
public MirrorList(List<V> values, List<R> reflections, MirrorList.Mirror<V,R> mirror)
MirrorList
with the specified target lists and
MirrorList.Mirror
. The specified target lists are meant to be empty, as they
become the underlying lists of the new MirrorList
instance.
The specified MirrorList.Mirror
provides the
V -> R
and
R -> V
methods, which are used to
reflect object values from one MirrorList
to the other.
values
- The underlying list of type <V>
.reflections
- The underlying list of type <R>
.mirror
- The MirrorList.Mirror
specifying the
V -> R
and
R -> V
methods.NullPointerException
- If any of the specified parameters is null.protected MirrorList(MirrorList<R,V> mirrorList, List<V> values, MirrorList.Mirror<V,R> mirror)
MirrorList
with the specified lists and mirror. This
method is specific for the construction of a reflected MirrorList
instance.mirrorList
- The MirrorList
for which this
list will
be a reflection. Likewise, this
list will be a reflection
for mirrorList
.values
- The underlying list of type <V>
.mirror
- The MirrorList.Mirror
specifying the
V -> R
and
R -> V
methods.protected MirrorList<V,R> newInstance(List<V> values, List<R> reflections)
MirrorList
with the
specified target lists. This method is intended to be overridden by
subclasses in order to provide instances of the subclass.values
- The underlying list of type <V>
.reflections
- The underlying list of type <R>
.MirrorList
with the specified target
lists.protected MirrorList<R,V> newMirrorInstance(List<R> values)
MirrorList
with the specified target list. This method is intended
to be overridden by subclasses in order to provide instances of the
subclass.values
- The underlying list of type <V>
.MirrorList
with the specified target
lists.public MirrorList<R,V> getMirrorList()
MirrorList
instance of type <R>
that represents
the one-to-one mapping with its mirror of type <V>
(i.e.
this
instance). Changes to the MirrorList
will be reflected
in this
instance, and vice-versa.MirrorList
instance of type <R>
that retains
the one-to-one mapping with its mirror of type <V>
(i.e.
this
instance).public MirrorList<R,V> reverse()
MirrorList
, effectively switching <V>
to
<R>
, and vice-versa. This method is the in-place version of
getMirrorList()
.
If this MirrorList
has a not-null mirrorList
, the internal
states of the two lists are swapped, thus reversing the mirrorList
as well.
<V>
and <R>
types reversed.public MirrorList.Mirror<V,R> getMirror()
MirrorList.Mirror
.MirrorList.Mirror
.protected MirrorList.Mirror<R,V> getReverseMirror()
MirrorList.Mirror
, and caches it for subsequent retrieval,
avoiding reinstantiation.MirrorList.Mirror
.protected void lock(boolean unlocked)
underlying list
to detect concurrent
modification. Specifically, this method calls target.iterator()
and saves its reference for examination during the next
method call that is made to the MirrorList
. If the call to
Iterator.next()
on the saved iterator
results
in a ConcurrentModificationException
, it means that the
underlying list
was modified outside of the
MirrorList
. Such modifications are not allowed, because they risk
compromising the data integrity of the this list and its
mirrorList
.unlocked
- If true
, this list is unlocked and will be locked;
if false
, this list is already locked and will not be
relocked.protected boolean unlock() throws ConcurrentModificationException
underlying list
and checks for concurrent
modification. Specifically, this method calls Iterator.next()
on
targetLock
, which may result in a
ConcurrentModificationException
if the underlying
list
was modified outside of this MirrorList
.ConcurrentModificationException
- If the underlying
list
was modified outside of the MirrorList
public int size()
size
in interface Collection<V>
size
in interface List<V>
size
in class ObservableList<V>
protected boolean beforeAdd(int index, V element)
ObservableList
List
.
Note: It is possible for index
to be -1
, in case the
add operation is executed from an Iterator
when a prior
Iterator.remove()
or ListIterator.add(Object)
or
ListIterator.set(Object)
has already been called.
beforeAdd
in class ObservableList<V>
index
- The index for the element to be added to the enclosed
List
.element
- The element to be added to the enclosed List
.true
, the subsequent add
operation will be performed; if this method returns false
,
the subsequent add operation will not be performed.protected void afterAdd(int index, V element, RuntimeException e)
ObservableList
List
.afterAdd
in class ObservableList<V>
index
- The index of the element added to the enclosed List
.element
- The element to be added to the enclosed List
.e
- A RuntimeException
that occurred during the add operation,
or null
if no exception occurred.protected boolean beforeRemove(int index)
ObservableList
List
.
Note: It is possible for index
to be -1
, in case the
remove operation is executed from an Iterator
when a prior
Iterator.remove()
or ListIterator.add(Object)
or
ListIterator.set(Object)
has already been called.
beforeRemove
in class ObservableList<V>
index
- The index of the element to be removed from the enclosed
List
.true
, the subsequent remove
operation will be performed; if this method returns false
,
the subsequent remove operation will not be performed.protected void afterRemove(Object element, RuntimeException e)
ObservableList
List
.afterRemove
in class ObservableList<V>
element
- The element removed from the enclosed List
, or
attempted to be removed from the List
in case of a
RuntimeException
.e
- A RuntimeException
that occurred during the remove
operation, or null
if no exception occurred.protected boolean beforeSet(int index, V newElement)
ObservableList
List
.
Note: It is possible for index
to be -1
, in case the
set operation is executed from an Iterator
when a prior
Iterator.remove()
or ListIterator.add(Object)
or
ListIterator.set(Object)
has already been called.
beforeSet
in class ObservableList<V>
index
- The index for the element to be set in the enclosed
List
.newElement
- The element to be set in the enclosed List
.true
, the subsequent set
operation will be performed; if this method returns false
,
the subsequent set operation will not be performed.protected void afterSet(int index, V oldElement, RuntimeException e)
ObservableList
List
.afterSet
in class ObservableList<V>
index
- The index of the element set in the enclosed List
.oldElement
- The old element at the index of the enclosed
List
.e
- A RuntimeException
that occurred during the set
operation, or null
if no exception occurred.protected void beforeGet(int index, ListIterator<V> iterator)
ObservableList
List
.beforeGet
in class ObservableList<V>
index
- The index of the element to be retrieved from the enclosed
List
.iterator
- The ListIterator
instance if the get is a result of
an iterator reference, otherwise null
.protected void afterGet(int index, V element, ListIterator<? super V> iterator, RuntimeException e)
ObservableList
List
.afterGet
in class ObservableList<V>
index
- The index of the element retrieved from the enclosed
List
.element
- The element retrieved from the enclosed List
.iterator
- The Iterator
instance if the get is a result of an
iterator reference, otherwise null
.e
- A RuntimeException
that occurred during the get operation,
or null
if no exception occurred.public MirrorList<V,R> subList(int fromIndex, int toIndex)
DelegateList
The class DelegateList
does not itself implement
#subList(int,int)
, so calling this method on an instance of a
subclass of DelegateList
that does not override this method will
result in a UnsupportedOperationException
.
Copyright © 2020 LibJ. All rights reserved.