632N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
632N/A *
632N/A * This code is free software; you can redistribute it and/or modify it
632N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
632N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
632N/A *
632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
632N/A * version 2 for more details (a copy is included in the LICENSE file that
632N/A * accompanied this code).
632N/A *
632N/A * You should have received a copy of the GNU General Public License version
632N/A * 2 along with this work; if not, write to the Free Software Foundation,
632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
632N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
632N/A */
632N/Apackage com.sun.beans;
632N/A
632N/Aimport java.lang.ref.Reference;
632N/Aimport java.lang.ref.WeakReference;
632N/A
632N/Aimport java.util.Map;
632N/Aimport java.util.WeakHashMap;
632N/A
632N/A/**
632N/A * A hashtable-based cache with weak keys and weak values.
632N/A * An entry in the map will be automatically removed
632N/A * when its key is no longer in the ordinary use.
632N/A * A value will be automatically removed as well
632N/A * when it is no longer in the ordinary use.
632N/A *
632N/A * @since 1.7
632N/A *
632N/A * @author Sergey A. Malenkov
632N/A */
632N/Apublic final class WeakCache<K, V> {
632N/A private final Map<K, Reference<V>> map = new WeakHashMap<K, Reference<V>>();
632N/A
632N/A /**
632N/A * Returns a value to which the specified {@code key} is mapped,
632N/A * or {@code null} if this map contains no mapping for the {@code key}.
632N/A *
632N/A * @param key the key whose associated value is returned
632N/A * @return a value to which the specified {@code key} is mapped
632N/A */
632N/A public V get(K key) {
632N/A Reference<V> reference = this.map.get(key);
632N/A if (reference == null) {
632N/A return null;
632N/A }
632N/A V value = reference.get();
632N/A if (value == null) {
632N/A this.map.remove(key);
632N/A }
632N/A return value;
632N/A }
632N/A
632N/A /**
632N/A * Associates the specified {@code value} with the specified {@code key}.
632N/A * Removes the mapping for the specified {@code key} from this cache
632N/A * if it is present and the specified {@code value} is {@code null}.
632N/A * If the cache previously contained a mapping for the {@code key},
632N/A * the old value is replaced by the specified {@code value}.
632N/A *
632N/A * @param key the key with which the specified value is associated
632N/A * @param value the value to be associated with the specified key
632N/A */
632N/A public void put(K key, V value) {
632N/A if (value != null) {
632N/A this.map.put(key, new WeakReference<V>(value));
632N/A }
632N/A else {
632N/A this.map.remove(key);
632N/A }
632N/A }
1987N/A
1987N/A /**
1987N/A * Removes all of the mappings from this cache.
1987N/A */
1987N/A public void clear() {
1987N/A this.map.clear();
1987N/A }
632N/A}