org.eclipse.jdt.internal.compiler.apt.util
Class ManyToMany<T1,T2>
java.lang.Object
org.eclipse.jdt.internal.compiler.apt.util.ManyToMany<T1,T2>
public class ManyToMany<T1,T2>
- extends Object
Manage a Map>, with reverse links so that it is possible to
efficiently find all T1s that have a particular T2 associated with them.
Access to the map is synchronized, so that it is possible to read and
write simultaneously from multiple threads.
The map permits the null value for keys nor for value elements.
Design invariants preserved by all operations on this map are as follows:
- If a key exists, it has at least one value associated with it; that is,
for all k such that null != containsKey(k), getValues(k) returns a non-empty
set.
- If a value exists, it has at least one key associated with it; that is,
for all v such that null != containsValue(v), getKeys(v) returns a non-empty
set.
Method Summary |
boolean |
clear()
Empty all maps. |
void |
clearDirtyBit()
Sets the dirty bit to false. |
boolean |
containsKey(T1 key)
Equivalent to keySet().contains(key). |
boolean |
containsKeyValuePair(T1 key,
T2 value)
Is there a key that is mapped to the specified value?
Search within the forward map. |
boolean |
containsValue(T2 value)
Equivalent to values().contains(value). |
Set<T1> |
getKeys(T2 value)
Search the reverse map for all keys that have been associated with
a particular value. |
Set<T1> |
getKeySet()
|
Set<T2> |
getValues(T1 key)
Search the forward map for all values associated with a particular key. |
Set<T2> |
getValueSet()
|
boolean |
isDirty()
Return the state of the dirty bit. |
boolean |
keyHasOtherValues(T1 key,
T2 value)
Check whether key has an association to any values other
than value - that is, whether the same key has been added
with multiple values. |
boolean |
put(T1 key,
T2 value)
Associate the specified value with the key. |
boolean |
remove(T1 key,
T2 value)
Remove a particular key-value association. |
boolean |
removeKey(T1 key)
Remove the key and its associated key/value entries. |
boolean |
removeValue(T2 value)
Remove the value and its associated key/value entries. |
boolean |
valueHasOtherKeys(T2 value,
T1 key)
Check whether value has an association from any keys other
than key - that is, whether the same value has been added
with multiple keys. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ManyToMany
public ManyToMany()
clear
public boolean clear()
- Empty all maps. If the maps previously contained entries,
this will set the dirty bit.
- Returns:
- true if the maps contained any entries prior to being cleared
clearDirtyBit
public void clearDirtyBit()
- Sets the dirty bit to false. Internal operations do not use the dirty
bit; clearing it will not affect behavior of the map. It's just there
for the convenience of callers who don't want to keep track of every
put() and remove().
containsKey
public boolean containsKey(T1 key)
- Equivalent to keySet().contains(key).
- Returns:
- true if the map contains the specified key.
containsKeyValuePair
public boolean containsKeyValuePair(T1 key,
T2 value)
- Is there a key that is mapped to the specified value?
Search within the forward map.
- Returns:
- true if such a key exists
containsValue
public boolean containsValue(T2 value)
- Equivalent to values().contains(value).
- Returns:
- true if the map contains the specified value (regardless
of what key it might be associated with).
getKeys
public Set<T1> getKeys(T2 value)
- Search the reverse map for all keys that have been associated with
a particular value.
- Returns:
- the set of keys that are associated with the specified value,
or an empty set if the value does not exist in the map.
getValues
public Set<T2> getValues(T1 key)
- Search the forward map for all values associated with a particular key.
Returns a copy of the set of values.
- Returns:
- a copy of the set of values that are associated with the
specified key, or an empty set if the key does not exist in the map.
getKeySet
public Set<T1> getKeySet()
- Returns:
- a copy of the set of all keys (that is, all items of type T1).
If the maps are empty, the returned set will be empty, not null. The
returned set can be modified by the caller without affecting the map.
- See Also:
getValueSet()
getValueSet
public Set<T2> getValueSet()
- Returns:
- a copy of the set of all values (that is, all items of type T2).
If the maps are empty, the returned set will be empty, not null. The
returned set can be modified by the caller without affecting the map.
- See Also:
getKeySet()
isDirty
public boolean isDirty()
- Return the state of the dirty bit. All operations that change the state
of the maps, including @see #clear(), set the dirty bit if any content actually
changed. The only way to clear the dirty bit is to call @see #clearDirtyBit().
- Returns:
- true if the map content has changed since it was created or since
the last call to clearDirtyBit().
- See Also:
clearDirtyBit()
keyHasOtherValues
public boolean keyHasOtherValues(T1 key,
T2 value)
- Check whether
key
has an association to any values other
than value
- that is, whether the same key has been added
with multiple values. Equivalent to asking whether the intersection of
getValues(key)
and the set containing value
is
non-empty.
- Returns:
- true iff
key
is in the map and is associated
with values other than value
. - See Also:
valueHasOtherKeys(Object, Object)
put
public boolean put(T1 key,
T2 value)
- Associate the specified value with the key. Adds the entry
to both the forward and reverse maps. Adding the same value
twice to a particular key has no effect. Because this is a
many-to-many map, adding a new value for an existing key does
not change the existing association, it adds a new one.
- Parameters:
key
- can be nullvalue
- can be null
- Returns:
- true if the key/value pair did not exist prior to being added
remove
public boolean remove(T1 key,
T2 value)
- Remove a particular key-value association. This is the inverse
of put(key, value). If the key does not exist, or the value
does not exist, or the association does not exist, this call
has no effect.
- Returns:
- true if the key/value pair existed in the map prior to removal
removeKey
public boolean removeKey(T1 key)
- Remove the key and its associated key/value entries.
Calling removeKey(k) is equivalent to calling remove(k,v)
for every v in getValues(k).
- Returns:
- true if the key existed in the map prior to removal
removeValue
public boolean removeValue(T2 value)
- Remove the value and its associated key/value entries.
Calling removeValue(v) is equivalent to calling remove(k,v)
for every k in getKeys(v).
- Returns:
- true if the value existed in the map prior to removal.
valueHasOtherKeys
public boolean valueHasOtherKeys(T2 value,
T1 key)
- Check whether
value
has an association from any keys other
than key
- that is, whether the same value has been added
with multiple keys. Equivalent to asking whether the intersection of
getKeys(value)
and the set containing key
is
non-empty.
- Returns:
- true iff
value
is in the map and is associated
with keys other than key
. - See Also:
keyHasOtherValues(Object, Object)
Copyright © 2012. All Rights Reserved.