K
- The type of keys maintained by this map.V
- The type of value elements in this map.R
- The type of reflected value elements in the mirror map.public class MirrorMap<K,V,R> extends ObservableMap<K,V>
<V>
, which is the generic type of
this
map.<R>
, which is the generic type of
the map returned by getMirrorMap()
MirrorMap.Mirror
provides the
V -> R
and
R -> V
methods, which are
used to reflect values from one MirrorMap
to the other.
Entries added to either sides of the MirrorMap
are "lazy-reflected"
-- i.e. instead of reflecting entries upon their addition to the map, entries
are reflected upon retrieval from the map.
The getMirrorMap()
method returns the MirrorMap
instance of
type <K,R>
that represents the one-to-one mapping with its mirror of
type <K,V>
(i.e. this
instance). Changes to the
MirrorMap
will be reflected in this
instance, and vice-versa.
Modifier and Type | Class and Description |
---|---|
static interface |
MirrorMap.Mirror<K,V,R>
Interface providing methods for the reflection of one type of object to
another, and vice-versa.
|
protected class |
MirrorMap.ObservableMirrorEntrySet |
ObservableMap.ObservableEntrySet, ObservableMap.ObservableKeySet
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
protected boolean |
inited |
protected MirrorMap<K,R,V> |
mirrorMap |
protected static Object |
PENDING
Object to be used in the underlying maps to represent that a value is
pending reflection via the methods in
mirror upon the invocation
of beforeGet(Object) . |
protected Iterator<?> |
targetLock |
entrySet, keySet, values
target
Modifier | Constructor and Description |
---|---|
|
MirrorMap(Map<K,V> values,
Map<K,R> reflections,
MirrorMap.Mirror<K,V,R> mirror)
Creates a new
MirrorMap with the specified target maps and
MirrorMap.Mirror . |
protected |
MirrorMap(MirrorMap<K,R,V> mirrorMap,
Map<K,V> values,
MirrorMap.Mirror<K,V,R> mirror)
Creates a new
MirrorMap with the specified maps and mirror. |
Modifier and Type | Method and Description |
---|---|
protected void |
afterContainsKey(Object key,
boolean result,
RuntimeException e)
Callback method that is invoked immediately after a key is dereferenced for
inclusion via
ObservableMap.containsKey(Object) . |
protected void |
afterContainsValue(Object value,
boolean result,
RuntimeException e)
Callback method that is invoked immediately after a value is dereferenced
for inclusion via
ObservableMap.containsValue(Object) . |
protected void |
afterGet(Object key,
V value,
RuntimeException e)
Callback method that is invoked immediately after a value is retrieved from
the enclosed
Map . |
protected void |
afterPut(K key,
V oldValue,
V newValue,
RuntimeException e)
Callback method that is invoked immediately after an entry is put into the
enclosed
Map . |
protected void |
afterRemove(Object key,
V value,
RuntimeException e)
Callback method that is invoked immediately after an entry is removed from
the enclosed
Map . |
protected boolean |
beforeContainsKey(Object key)
Callback method that is invoked immediately before a key is dereferenced
for inclusion via
ObservableMap.containsKey(Object) . |
protected void |
beforeContainsValue(Object value)
Callback method that is invoked immediately before a value is dereferenced
for inclusion via
ObservableMap.containsValue(Object) . |
protected void |
beforeGet(Object key)
Callback method that is invoked immediately before a value is retrieved via
ObservableMap.get(Object) . |
protected boolean |
beforePut(K key,
V oldValue,
V newValue)
Callback method that is invoked immediately before an entry is put into the
enclosed
Map . |
protected boolean |
beforeRemove(Object key,
V value)
Callback method that is invoked immediately before an entry is removed from
the enclosed
Map . |
MirrorMap.Mirror<K,V,R> |
getMirror()
Returns the
MirrorMap.Mirror for this MirrorMap . |
MirrorMap<K,R,V> |
getMirrorMap()
Returns the
MirrorMap instance of type <K,R> that
represents the one-to-one mapping with its mirror of type <K,V>
(i.e. |
protected MirrorMap.Mirror<K,R,V> |
getReverseMirror()
Returns the reverse
MirrorMap.Mirror for this MirrorMap , and caches
it for subsequent retrieval, avoiding reinstantiation. |
protected void |
lock(boolean unlocked)
Locks the
underlying map to detect concurrent modification. |
protected ObservableMap.ObservableEntrySet |
newEntrySet()
Factory method that returns a new instance of an
ObservableMap.ObservableEntrySet . |
protected MirrorMap<K,V,R> |
newInstance(Map<K,V> values,
Map<K,R> reflections)
Factory method that returns a new instance of a
MirrorMap with the
specified target maps. |
protected MirrorMap<K,R,V> |
newMirrorInstance(Map<K,R> values)
Factory method that returns a new mirror instance of a
MirrorMap with the specified target map. |
int |
size() |
protected boolean |
unlock()
Unlocks the
underlying map and checks for concurrent
modification. |
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, keySet, merge, newKeySet, put, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, toString, values
isEmpty, superCompute, superComputeIfAbsent, superComputeIfPresent, superForEach, superGetOrDefault, superMerge, superPutIfAbsent, superRemove, superReplace, superReplace, superReplaceAll
clone
protected static final Object PENDING
mirror
upon the invocation
of beforeGet(Object)
.protected Iterator<?> targetLock
protected boolean inited
public MirrorMap(Map<K,V> values, Map<K,R> reflections, MirrorMap.Mirror<K,V,R> mirror)
MirrorMap
with the specified target maps and
MirrorMap.Mirror
. The specified target maps are meant to be empty, as they
become the underlying maps of the new MirrorMap
instance. The
specified MirrorMap.Mirror
provides the
V -> R
and
R -> V
methods, which are
used to reflect object values from one MirrorMap
to the other.values
- The underlying map of type <K,V>
.reflections
- The underlying map of type <K,R>
.mirror
- The MirrorMap.Mirror
specifying the
V -> R
and
R -> V
methods.NullPointerException
- If any of the specified parameters is null.protected MirrorMap(MirrorMap<K,R,V> mirrorMap, Map<K,V> values, MirrorMap.Mirror<K,V,R> mirror)
MirrorMap
with the specified maps and mirror. This
method is specific for the construction of a reflected MirrorMap
instance.mirrorMap
- The MirrorMap
for which this
map will be a
reflection. Likewise, this
map will be a reflection for
mirrorMap
.values
- The underlying map of type <K,V>
.mirror
- The MirrorMap.Mirror
specifying the
V -> R
and
R -> V
methods.protected MirrorMap<K,V,R> newInstance(Map<K,V> values, Map<K,R> reflections)
MirrorMap
with the
specified target maps. This method is intended to be overridden by
subclasses in order to provide instances of the subclass.values
- The underlying map of type <K,V>
.reflections
- The underlying map of type <K,R>
.MirrorMap
with the specified target
maps.protected MirrorMap<K,R,V> newMirrorInstance(Map<K,R> values)
MirrorMap
with the specified target map. This method is intended to
be overridden by subclasses in order to provide instances of the subclass.values
- The underlying map of type <K,V>
.MirrorMap
with the specified target
maps.protected ObservableMap.ObservableEntrySet newEntrySet()
ObservableMap
ObservableMap.ObservableEntrySet
. This method is intended to be overridden by
subclasses in order to provide instances of the subclass.newEntrySet
in class ObservableMap<K,V>
ObservableMap.ObservableEntrySet
.public MirrorMap<K,R,V> getMirrorMap()
MirrorMap
instance of type <K,R>
that
represents the one-to-one mapping with its mirror of type <K,V>
(i.e. this
instance). Changes to the MirrorMap
will be
reflected in this
instance, and vice-versa.MirrorMap
instance of type <K,R>
that retains
the one-to-one mapping with its mirror of type <K,V>
(i.e.
this
instance).public MirrorMap.Mirror<K,V,R> getMirror()
MirrorMap.Mirror
for this MirrorMap
.MirrorMap.Mirror
for this MirrorMap
.protected MirrorMap.Mirror<K,R,V> getReverseMirror()
MirrorMap.Mirror
for this MirrorMap
, and caches
it for subsequent retrieval, avoiding reinstantiation.MirrorMap.Mirror
for this MirrorMap
.protected void lock(boolean unlocked)
underlying map
to detect concurrent modification.
Specifically, this method calls target.entrySet().iterator()
and saves its reference for examination
during the next method call that is made to the MirrorMap
. If the
call to Iterator.next()
on the saved iterator
results in a ConcurrentModificationException
, it means that the
underlying map
was modified outside of the
MirrorMap
. Such modifications are not allowed, because they risk
compromising the data integrity of the this map and its mirrorMap
.unlocked
- If true
, this map is unlocked and will be locked;
if false
, this map is already locked and will not be
relocked.protected boolean unlock() throws ConcurrentModificationException
underlying map
and checks for concurrent
modification. Specifically, this method calls Iterator.next()
on
targetLock
, which may result in a
ConcurrentModificationException
if the underlying
map
was modified outside of this MirrorMap
.ConcurrentModificationException
- If the underlying
map
was modified outside of the MirrorMap
public int size()
protected boolean beforePut(K key, V oldValue, V newValue)
ObservableMap
Map
.beforePut
in class ObservableMap<K,V>
key
- The key of the entry to be added to the enclosed Map
.oldValue
- The old value to be replaced for the key in the enclosed
Map
, or null
if there was no existing value for
the key.newValue
- The new value to be put for the key in the enclosed
Map
.false
, the subsequent put
operation will not be performed; otherwise, the operation will be
performed.protected void afterPut(K key, V oldValue, V newValue, RuntimeException e)
ObservableMap
Map
.afterPut
in class ObservableMap<K,V>
key
- The key of the entry to be added to the enclosed Map
.oldValue
- The old value to be replaced for the key in the enclosed
Map
, or null
if there was no existing value for
the key.newValue
- The new value to be put for the key in the enclosed
Map
.e
- A RuntimeException
that occurred during the put operation,
or null
if no exception occurred.protected boolean beforeRemove(Object key, V value)
ObservableMap
Map
.beforeRemove
in class ObservableMap<K,V>
key
- The key of the entry to be removed from the enclosed
Map
.value
- The value for the key to be removed in the enclosed
Map
, or null
if there was no existing value for
the key.false
, the subsequent remove
operation will not be performed; otherwise, the operation will be
performed.protected void afterRemove(Object key, V value, RuntimeException e)
ObservableMap
Map
.afterRemove
in class ObservableMap<K,V>
key
- The key of the entry to be removed from the enclosed
Map
.value
- The value for the key to be removed in the enclosed
Map
, or null
if there was no existing value for
the key.e
- A RuntimeException
that occurred during the remove
operation, or null
if no exception occurred.protected boolean beforeContainsKey(Object key)
ObservableMap
ObservableMap.containsKey(Object)
.beforeContainsKey
in class ObservableMap<K,V>
key
- The key to be dereferenced for inclusion.false
, the subsequent
ObservableMap.containsKey(Object)
operation immediately return
false
; otherwise, the operation will be performed in
regular fashion.protected void afterContainsKey(Object key, boolean result, RuntimeException e)
ObservableMap
ObservableMap.containsKey(Object)
.afterContainsKey
in class ObservableMap<K,V>
key
- The key dereferenced for inclusion.result
- The result returned by ObservableMap.containsKey(Object)
.e
- A RuntimeException
that occurred during the get operation,
or null
if no exception occurred.protected void beforeContainsValue(Object value)
ObservableMap
ObservableMap.containsValue(Object)
.beforeContainsValue
in class ObservableMap<K,V>
value
- The key to be dereferenced for inclusion.protected void afterContainsValue(Object value, boolean result, RuntimeException e)
ObservableMap
ObservableMap.containsValue(Object)
.afterContainsValue
in class ObservableMap<K,V>
value
- The value dereferenced for inclusion.result
- The result returned by ObservableMap.containsValue(Object)
.e
- A RuntimeException
that occurred during the get operation,
or null
if no exception occurred.protected void beforeGet(Object key)
ObservableMap
ObservableMap.get(Object)
.beforeGet
in class ObservableMap<K,V>
key
- The key whose associated value is to be retrieved.protected void afterGet(Object key, V value, RuntimeException e)
ObservableMap
Map
.afterGet
in class ObservableMap<K,V>
key
- The key whose associated value is to be retrieved.value
- The value retrieved via ObservableMap.get(Object)
.e
- A RuntimeException
that occurred during the get operation,
or null
if no exception occurred.Copyright © 2020 LibJ. All rights reserved.