?? weakfasthashmap.java
字號:
return (false);
}
} else {
if (!value.equals(mo.get(key))) {
return (false);
}
}
}
return (true);
}
}
}
/**
* Return the hash code value for this map. This implementation uses
* exactly the code that is used to define the list hash function in the
* documentation for the <code>Map.hashCode</code> method.
*
* @return suitable integer hash code
*/
public int hashCode() {
if (fast) {
int h = 0;
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
h += i.next().hashCode();
}
return (h);
} else {
synchronized (map) {
int h = 0;
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
h += i.next().hashCode();
}
return (h);
}
}
}
/**
* Return a shallow copy of this <code>FastHashMap</code> instance.
* The keys and values themselves are not copied.
*
* @return a clone of this map
*/
public Object clone() {
WeakFastHashMap results = null;
if (fast) {
results = new WeakFastHashMap(map);
} else {
synchronized (map) {
results = new WeakFastHashMap(map);
}
}
results.setFast(getFast());
return (results);
}
// Map views
// ----------------------------------------------------------------------
/**
* Return a collection view of the mappings contained in this map. Each
* element in the returned collection is a <code>Map.Entry</code>.
* @return the set of map Map entries
*/
public Set entrySet() {
return new EntrySet();
}
/**
* Return a set view of the keys contained in this map.
* @return the set of the Map's keys
*/
public Set keySet() {
return new KeySet();
}
/**
* Return a collection view of the values contained in this map.
* @return the set of the Map's values
*/
public Collection values() {
return new Values();
}
// Abstractions on Map creations (for subclasses such as WeakFastHashMap)
// ----------------------------------------------------------------------
protected Map createMap() {
return new WeakHashMap();
}
protected Map createMap(int capacity) {
return new WeakHashMap(capacity);
}
protected Map createMap(int capacity, float factor) {
return new WeakHashMap(capacity, factor);
}
protected Map createMap(Map map) {
return new WeakHashMap(map);
}
protected Map cloneMap(Map map) {
return createMap(map);
}
// Map view inner classes
// ----------------------------------------------------------------------
/**
* Abstract collection implementation shared by keySet(), values() and entrySet().
*/
private abstract class CollectionView implements Collection {
public CollectionView() {
}
protected abstract Collection get(Map map);
protected abstract Object iteratorNext(Map.Entry entry);
public void clear() {
if (fast) {
synchronized (WeakFastHashMap.this) {
map = createMap();
}
} else {
synchronized (map) {
get(map).clear();
}
}
}
public boolean remove(Object o) {
if (fast) {
synchronized (WeakFastHashMap.this) {
Map temp = cloneMap(map);
boolean r = get(temp).remove(o);
map = temp;
return r;
}
} else {
synchronized (map) {
return get(map).remove(o);
}
}
}
public boolean removeAll(Collection o) {
if (fast) {
synchronized (WeakFastHashMap.this) {
Map temp = cloneMap(map);
boolean r = get(temp).removeAll(o);
map = temp;
return r;
}
} else {
synchronized (map) {
return get(map).removeAll(o);
}
}
}
public boolean retainAll(Collection o) {
if (fast) {
synchronized (WeakFastHashMap.this) {
Map temp = cloneMap(map);
boolean r = get(temp).retainAll(o);
map = temp;
return r;
}
} else {
synchronized (map) {
return get(map).retainAll(o);
}
}
}
public int size() {
if (fast) {
return get(map).size();
} else {
synchronized (map) {
return get(map).size();
}
}
}
public boolean isEmpty() {
if (fast) {
return get(map).isEmpty();
} else {
synchronized (map) {
return get(map).isEmpty();
}
}
}
public boolean contains(Object o) {
if (fast) {
return get(map).contains(o);
} else {
synchronized (map) {
return get(map).contains(o);
}
}
}
public boolean containsAll(Collection o) {
if (fast) {
return get(map).containsAll(o);
} else {
synchronized (map) {
return get(map).containsAll(o);
}
}
}
public Object[] toArray(Object[] o) {
if (fast) {
return get(map).toArray(o);
} else {
synchronized (map) {
return get(map).toArray(o);
}
}
}
public Object[] toArray() {
if (fast) {
return get(map).toArray();
} else {
synchronized (map) {
return get(map).toArray();
}
}
}
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (fast) {
return get(map).equals(o);
} else {
synchronized (map) {
return get(map).equals(o);
}
}
}
public int hashCode() {
if (fast) {
return get(map).hashCode();
} else {
synchronized (map) {
return get(map).hashCode();
}
}
}
public boolean add(Object o) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection c) {
throw new UnsupportedOperationException();
}
public Iterator iterator() {
return new CollectionViewIterator();
}
private class CollectionViewIterator implements Iterator {
private Map expected;
private Map.Entry lastReturned = null;
private Iterator iterator;
public CollectionViewIterator() {
this.expected = map;
this.iterator = expected.entrySet().iterator();
}
public boolean hasNext() {
if (expected != map) {
throw new ConcurrentModificationException();
}
return iterator.hasNext();
}
public Object next() {
if (expected != map) {
throw new ConcurrentModificationException();
}
lastReturned = (Map.Entry)iterator.next();
return iteratorNext(lastReturned);
}
public void remove() {
if (lastReturned == null) {
throw new IllegalStateException();
}
if (fast) {
synchronized (WeakFastHashMap.this) {
if (expected != map) {
throw new ConcurrentModificationException();
}
WeakFastHashMap.this.remove(lastReturned.getKey());
lastReturned = null;
expected = map;
}
} else {
iterator.remove();
lastReturned = null;
}
}
}
}
/**
* Set implementation over the keys of the FastHashMap
*/
private class KeySet extends CollectionView implements Set {
protected Collection get(Map map) {
return map.keySet();
}
protected Object iteratorNext(Map.Entry entry) {
return entry.getKey();
}
}
/**
* Collection implementation over the values of the FastHashMap
*/
private class Values extends CollectionView {
protected Collection get(Map map) {
return map.values();
}
protected Object iteratorNext(Map.Entry entry) {
return entry.getValue();
}
}
/**
* Set implementation over the entries of the FastHashMap
*/
private class EntrySet extends CollectionView implements Set {
protected Collection get(Map map) {
return map.entrySet();
}
protected Object iteratorNext(Map.Entry entry) {
return entry;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -