Path: blob/master/src/java.desktop/share/classes/sun/awt/WeakIdentityHashMap.java
41152 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt;2627import java.lang.ref.Reference;28import java.lang.ref.ReferenceQueue;29import java.lang.ref.WeakReference;30import java.util.*;3132// A weak key reference hash map that uses System.identityHashCode() and "=="33// instead of hashCode() and equals(Object)34class WeakIdentityHashMap<K, V> implements Map<K, V> {35private final Map<WeakKey<K>, V> map;36private final transient ReferenceQueue<K> queue = new ReferenceQueue<K>();3738/**39* Constructs a new, empty identity hash map with a default initial40* size (16).41*/42public WeakIdentityHashMap() {43map = new HashMap<>(16);44}4546/**47* Constructs a new, empty identity map with the specified initial size.48*/49public WeakIdentityHashMap(int initialSize) {50map = new HashMap<>(initialSize);51}5253private Map<WeakKey<K>, V> getMap() {54for(Reference<? extends K> ref; (ref = this.queue.poll()) != null;) {55map.remove(ref);56}57return map;58}5960@Override61public int size() {62return getMap().size();63}6465@Override66public boolean isEmpty() {67return getMap().isEmpty();68}6970@Override71public boolean containsKey(Object key) {72return getMap().containsKey(new WeakKey<>(key, null));73}7475@Override76public boolean containsValue(Object value) {77return getMap().containsValue(value);78}7980@Override81public V get(Object key) {82return getMap().get(new WeakKey<>(key, null));83}8485@Override86public V put(K key, V value) {87return getMap().put(new WeakKey<K>(key, queue), value);88}8990@Override91public V remove(Object key) {92return getMap().remove(new WeakKey<>(key, null));93}9495@Override96public void putAll(Map<? extends K, ? extends V> m) {97for (Entry<? extends K, ? extends V> entry : m.entrySet()) {98put(entry.getKey(), entry.getValue());99}100}101102@Override103public void clear() {104getMap().clear();105}106107@Override108public Set<K> keySet() {109return new AbstractSet<K>() {110@Override111public Iterator<K> iterator() {112return new Iterator<K>() {113private K next;114Iterator<WeakKey<K>> iterator = getMap().keySet().iterator();115116@Override117public boolean hasNext() {118while (iterator.hasNext()) {119if ((next = iterator.next().get()) != null) {120return true;121}122}123return false;124}125126@Override127public K next() {128if(next == null && !hasNext()) {129throw new NoSuchElementException();130}131K ret = next;132next = null;133return ret;134}135};136}137138@Override139public int size() {140return getMap().keySet().size();141}142};143}144145@Override146public Collection<V> values() {147return getMap().values();148}149150@Override151public Set<Entry<K, V>> entrySet() {152return new AbstractSet<Entry<K, V>>() {153@Override154public Iterator<Entry<K, V>> iterator() {155final Iterator<Entry<WeakKey<K>, V>> iterator = getMap().entrySet().iterator();156return new Iterator<Entry<K, V>>() {157@Override158public boolean hasNext() {159return iterator.hasNext();160}161162@Override163public Entry<K, V> next() {164return new Entry<K, V>() {165Entry<WeakKey<K>, V> entry = iterator.next();166167@Override168public K getKey() {169return entry.getKey().get();170}171172@Override173public V getValue() {174return entry.getValue();175}176177@Override178public V setValue(V value) {179return null;180}181};182}183};184}185186@Override187public int size() {188return getMap().entrySet().size();189}190};191}192193private static class WeakKey<K> extends WeakReference<K> {194private final int hash;195196WeakKey(K key, ReferenceQueue <K> q) {197super(key, q);198hash = System.identityHashCode(key);199}200201@Override202public boolean equals(Object o) {203if(this == o) {204return true;205} else if( o instanceof WeakKey ) {206return get() == ((WeakKey)o).get();207} else {208return false;209}210}211212@Override213public int hashCode() {214return hash;215}216}217218219}220221222