Path: blob/master/src/java.desktop/share/classes/com/sun/beans/WeakCache.java
41155 views
/*1* Copyright (c) 2008, 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*/24package com.sun.beans;2526import java.lang.ref.Reference;27import java.lang.ref.WeakReference;2829import java.util.Map;30import java.util.WeakHashMap;3132/**33* A hashtable-based cache with weak keys and weak values.34* An entry in the map will be automatically removed35* when its key is no longer in the ordinary use.36* A value will be automatically removed as well37* when it is no longer in the ordinary use.38*39* @since 1.740*41* @author Sergey A. Malenkov42*/43public final class WeakCache<K, V> {44private final Map<K, Reference<V>> map = new WeakHashMap<K, Reference<V>>();4546/**47* Returns a value to which the specified {@code key} is mapped,48* or {@code null} if this map contains no mapping for the {@code key}.49*50* @param key the key whose associated value is returned51* @return a value to which the specified {@code key} is mapped52*/53public V get(K key) {54Reference<V> reference = this.map.get(key);55if (reference == null) {56return null;57}58V value = reference.get();59if (value == null) {60this.map.remove(key);61}62return value;63}6465/**66* Associates the specified {@code value} with the specified {@code key}.67* Removes the mapping for the specified {@code key} from this cache68* if it is present and the specified {@code value} is {@code null}.69* If the cache previously contained a mapping for the {@code key},70* the old value is replaced by the specified {@code value}.71*72* @param key the key with which the specified value is associated73* @param value the value to be associated with the specified key74*/75public void put(K key, V value) {76if (value != null) {77this.map.put(key, new WeakReference<V>(value));78}79else {80this.map.remove(key);81}82}8384/**85* Removes all of the mappings from this cache.86*/87public void clear() {88this.map.clear();89}90}919293