📄 multikeymap.java
字号:
int index = map.hashIndex(hashCode, map.data.length);
AbstractHashedMap.HashEntry entry = map.data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3)) {
Object oldValue = entry.getValue();
map.updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
map.addMapping(index, hashCode, new MultiKey(key1, key2, key3), value);
return null;
}
/**
* Removes the specified multi-key from this map.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @return the value mapped to the removed key, null if key not in map
*/
public Object remove(Object key1, Object key2, Object key3) {
int hashCode = hash(key1, key2, key3);
int index = map.hashIndex(hashCode, map.data.length);
AbstractHashedMap.HashEntry entry = map.data[index];
AbstractHashedMap.HashEntry previous = null;
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3)) {
Object oldValue = entry.getValue();
map.removeMapping(entry, index, previous);
return oldValue;
}
previous = entry;
entry = entry.next;
}
return null;
}
/**
* Gets the hash code for the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @return the hash code
*/
protected int hash(Object key1, Object key2, Object key3) {
int h = 0;
if (key1 != null) {
h ^= key1.hashCode();
}
if (key2 != null) {
h ^= key2.hashCode();
}
if (key3 != null) {
h ^= key3.hashCode();
}
h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;
}
/**
* Is the key equal to the combined key.
*
* @param entry the entry to compare to
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @return true if the key matches
*/
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3) {
MultiKey multi = (MultiKey) entry.getKey();
return
multi.size() == 3 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
(key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2)));
}
//-----------------------------------------------------------------------
/**
* Gets the value mapped to the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @return the mapped value, null if no match
*/
public Object get(Object key1, Object key2, Object key3, Object key4) {
int hashCode = hash(key1, key2, key3, key4);
AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
return entry.getValue();
}
entry = entry.next;
}
return null;
}
/**
* Checks whether the map contains the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @return true if the map contains the key
*/
public boolean containsKey(Object key1, Object key2, Object key3, Object key4) {
int hashCode = hash(key1, key2, key3, key4);
AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
return true;
}
entry = entry.next;
}
return false;
}
/**
* Stores the value against the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @param value the value to store
* @return the value previously mapped to this combined key, null if none
*/
public Object put(Object key1, Object key2, Object key3, Object key4, Object value) {
int hashCode = hash(key1, key2, key3, key4);
int index = map.hashIndex(hashCode, map.data.length);
AbstractHashedMap.HashEntry entry = map.data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
Object oldValue = entry.getValue();
map.updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
map.addMapping(index, hashCode, new MultiKey(key1, key2, key3, key4), value);
return null;
}
/**
* Removes the specified multi-key from this map.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @return the value mapped to the removed key, null if key not in map
*/
public Object remove(Object key1, Object key2, Object key3, Object key4) {
int hashCode = hash(key1, key2, key3, key4);
int index = map.hashIndex(hashCode, map.data.length);
AbstractHashedMap.HashEntry entry = map.data[index];
AbstractHashedMap.HashEntry previous = null;
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4)) {
Object oldValue = entry.getValue();
map.removeMapping(entry, index, previous);
return oldValue;
}
previous = entry;
entry = entry.next;
}
return null;
}
/**
* Gets the hash code for the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @return the hash code
*/
protected int hash(Object key1, Object key2, Object key3, Object key4) {
int h = 0;
if (key1 != null) {
h ^= key1.hashCode();
}
if (key2 != null) {
h ^= key2.hashCode();
}
if (key3 != null) {
h ^= key3.hashCode();
}
if (key4 != null) {
h ^= key4.hashCode();
}
h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;
}
/**
* Is the key equal to the combined key.
*
* @param entry the entry to compare to
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @return true if the key matches
*/
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3, Object key4) {
MultiKey multi = (MultiKey) entry.getKey();
return
multi.size() == 4 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
(key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2))) &&
(key4 == null ? multi.getKey(3) == null : key4.equals(multi.getKey(3)));
}
//-----------------------------------------------------------------------
/**
* Gets the value mapped to the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @param key5 the fifth key
* @return the mapped value, null if no match
*/
public Object get(Object key1, Object key2, Object key3, Object key4, Object key5) {
int hashCode = hash(key1, key2, key3, key4, key5);
AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
return entry.getValue();
}
entry = entry.next;
}
return null;
}
/**
* Checks whether the map contains the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @param key5 the fifth key
* @return true if the map contains the key
*/
public boolean containsKey(Object key1, Object key2, Object key3, Object key4, Object key5) {
int hashCode = hash(key1, key2, key3, key4, key5);
AbstractHashedMap.HashEntry entry = map.data[map.hashIndex(hashCode, map.data.length)];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
return true;
}
entry = entry.next;
}
return false;
}
/**
* Stores the value against the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @param key5 the fifth key
* @param value the value to store
* @return the value previously mapped to this combined key, null if none
*/
public Object put(Object key1, Object key2, Object key3, Object key4, Object key5, Object value) {
int hashCode = hash(key1, key2, key3, key4, key5);
int index = map.hashIndex(hashCode, map.data.length);
AbstractHashedMap.HashEntry entry = map.data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
Object oldValue = entry.getValue();
map.updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
map.addMapping(index, hashCode, new MultiKey(key1, key2, key3, key4, key5), value);
return null;
}
/**
* Removes the specified multi-key from this map.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -