|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractMap<K,V>
kodkod.util.collections.FixedMap<K,V>
public final class FixedMap<K,V>
A map with a fixed set of keys that acts as an indexer, assigning a unique integer in the range [0..#keys) to each key. This class implements the Map interface with an array, using reference-equality in place of object-equality when comparing keys (and values). In other words, in a FixedMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)
This class is not a general-purpose Map implementation! While this class implements the Map interface, it intentionally violates Map's general contract, which mandates the use of the equals method when comparing objects.
This class provides put(Object, Object)
operation for the keys
within its fixed key set and permits
null values and the null key. The remove(Object)
operation is not supported. This class guarantees
that the order of the map will remain constant over time.
This class provides log-time performance for the basic
operations (get and put), assuming the system
identity hash function (System.identityHashCode(Object)
)
disperses elements properly among the buckets.
This implementation is not synchronized, and its iterators are not fail-fast.
keys: set K |
map: keys -> one V |
indices: keys lone->one int |
Constructor Summary | |
---|---|
FixedMap(K[] keys)
Constructs a new fixed map, backed by the given array of keys. |
|
FixedMap(java.util.Map<K,V> map)
Constructs a new fixed map from the given map. |
|
FixedMap(java.util.Set<K> keys)
Constructs a new fixed map for the given set of keys. |
Method Summary | |
---|---|
boolean |
containsKey(java.lang.Object key)
Tests whether the specified object reference is a key in this fixed map. |
boolean |
containsValue(java.lang.Object value)
Tests whether the specified object reference is a value in this fixed map. |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Returns a set view of the mappings contained in this map. |
boolean |
equals(java.lang.Object o)
Compares the specified object with this map for equality. |
V |
get(int index)
Returns the value to which the key at the specified index is mapped in this fixed map. |
V |
get(java.lang.Object key)
Returns the value to which the specified key is mapped in this fixed map, or null if the map contains no mapping for this key. |
int |
hashCode()
Returns the hash code value for this map. |
int |
indexOf(K key)
Returns the index of the given key, if it is in this.keys. |
boolean |
isEmpty()
|
K |
keyAt(int index)
Returns the key at the given index. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this fixed map. |
V |
remove(java.lang.Object key)
Throws an UnsupportedOperationException exception. |
int |
size()
Returns the number of keys in this.indexer. |
Methods inherited from class java.util.AbstractMap |
---|
clear, keySet, putAll, toString, values |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public FixedMap(java.util.Map<K,V> map)
this.keys' = keys && this.map = map.map |
public FixedMap(java.util.Set<K> keys)
this.keys' = keys && no this.map' |
public FixedMap(K[] keys)
this.keys' = keys && no this.map' |
no disj i, j: [0..keys.length) | keys[i] == keys[j] |
all i, j: [0..keys.length) | i < j => System.identityHashCode(keys[i]) <= System.identityHashCode(keys[j]) |
Method Detail |
---|
public final int indexOf(K key)
indexOf
in interface Indexer<K>
public final K keyAt(int index)
keyAt
in interface Indexer<K>
java.lang.IndexOutOfBoundsException
- - index !in this.indices[this.keys]public final boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<K,V>
containsKey
in class java.util.AbstractMap<K,V>
public final boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<K,V>
containsValue
in class java.util.AbstractMap<K,V>
public final java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Like the backing map, the Map.Entry objects in the set returned by this method define key and value equality as reference-equality rather than object-equality. This affects the behavior of the equals and hashCode methods of these Map.Entry objects. A reference-equality based Map.Entry e is equal to an object o if and only if o is a Map.Entry and e.getKey()==o.getKey() && e.getValue()==o.getValue(). To accommodate these equals semantics, the hashCode method returns System.identityHashCode(e.getKey()) ^ System.identityHashCode(e.getValue()).
Owing to the reference-equality-based semantics of the
Map.Entry instances in the set returned by this method,
it is possible that the symmetry and transitivity requirements of
the Object.equals(Object)
contract may be violated if any of
the entries in the set is compared to a normal map entry, or if
the set returned by this method is compared to a set of normal map
entries (such as would be returned by a call to this method on a normal
map). However, the Object.equals contract is guaranteed to
hold among identity-based map entries, and among sets of such entries.
entrySet
in interface java.util.Map<K,V>
entrySet
in class java.util.AbstractMap<K,V>
public final V get(java.lang.Object key)
get
in interface java.util.Map<K,V>
get
in class java.util.AbstractMap<K,V>
public final V get(int index)
java.lang.IndexOutOfBoundsException
- - index !in this.indices[this.keys]index in this.indices[this.keys] |
public final boolean isEmpty()
isEmpty
in interface java.util.Map<K,V>
isEmpty
in class java.util.AbstractMap<K,V>
Map.isEmpty()
public final V put(K key, V value)
put
in interface java.util.Map<K,V>
put
in class java.util.AbstractMap<K,V>
java.lang.IllegalArgumentException
- - key !in this.keysthis.map' = this.map ++ key->value |
key in this.keys |
public final V remove(java.lang.Object key)
UnsupportedOperationException
exception.
remove
in interface java.util.Map<K,V>
remove
in class java.util.AbstractMap<K,V>
Map.remove(java.lang.Object)
public final int size()
Indexer
size
in interface java.util.Map<K,V>
size
in interface Indexer<K>
size
in class java.util.AbstractMap<K,V>
Map.size()
public int hashCode()
Object.hashCode()
.
Owing to the reference-equality-based semantics of the Map.Entry instances in the set returned by this map's entrySet method, it is possible that the contractual requirement of Object.hashCode mentioned in the previous paragraph will be violated if one of the two objects being compared is an FixedMap instance and the other is a normal map.
hashCode
in interface java.util.Map<K,V>
hashCode
in class java.util.AbstractMap<K,V>
Object.hashCode()
,
Object.equals(Object)
,
equals(Object)
public boolean equals(java.lang.Object o)
Owing to the reference-equality-based semantics of this map it is possible that the symmetry and transitivity requirements of the Object.equals contract may be violated if this map is compared to a normal map. However, the Object.equals contract is guaranteed to hold among FixedMap instances.
equals
in interface java.util.Map<K,V>
equals
in class java.util.AbstractMap<K,V>
o
- object to be compared for equality with this map.
Object.equals(Object)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |