jdk/src/share/classes/com/sun/beans/WeakCache.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4382
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     4
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4382
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4382
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    10
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    15
 * accompanied this code).
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    16
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4382
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4382
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4382
diff changeset
    23
 * questions.
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    24
 */
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    25
package com.sun.beans;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    26
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    27
import java.lang.ref.Reference;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    28
import java.lang.ref.WeakReference;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    29
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    30
import java.util.Map;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    31
import java.util.WeakHashMap;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    32
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    33
/**
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    34
 * A hashtable-based cache with weak keys and weak values.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    35
 * An entry in the map will be automatically removed
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    36
 * when its key is no longer in the ordinary use.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    37
 * A value will be automatically removed as well
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    38
 * when it is no longer in the ordinary use.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    39
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    40
 * @since 1.7
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    41
 *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    42
 * @author Sergey A. Malenkov
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    43
 */
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    44
public final class WeakCache<K, V> {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    45
    private final Map<K, Reference<V>> map = new WeakHashMap<K, Reference<V>>();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    46
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    47
    /**
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    48
     * Returns a value to which the specified {@code key} is mapped,
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    49
     * or {@code null} if this map contains no mapping for the {@code key}.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    50
     *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    51
     * @param key  the key whose associated value is returned
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    52
     * @return a value to which the specified {@code key} is mapped
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    53
     */
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    54
    public V get(K key) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    55
        Reference<V> reference = this.map.get(key);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    56
        if (reference == null) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    57
            return null;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    58
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    59
        V value = reference.get();
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    60
        if (value == null) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    61
            this.map.remove(key);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    62
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    63
        return value;
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    64
    }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    65
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    66
    /**
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    67
     * Associates the specified {@code value} with the specified {@code key}.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    68
     * Removes the mapping for the specified {@code key} from this cache
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    69
     * if it is present and the specified {@code value} is {@code null}.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    70
     * If the cache previously contained a mapping for the {@code key},
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    71
     * the old value is replaced by the specified {@code value}.
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    72
     *
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    73
     * @param key    the key with which the specified value is associated
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    74
     * @param value  the value to be associated with the specified key
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    75
     */
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    76
    public void put(K key, V value) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    77
        if (value != null) {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    78
            this.map.put(key, new WeakReference<V>(value));
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    79
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    80
        else {
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    81
            this.map.remove(key);
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    82
        }
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    83
    }
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 1308
diff changeset
    84
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 1308
diff changeset
    85
    /**
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 1308
diff changeset
    86
     * Removes all of the mappings from this cache.
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 1308
diff changeset
    87
     */
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 1308
diff changeset
    88
    public void clear() {
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 1308
diff changeset
    89
        this.map.clear();
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 1308
diff changeset
    90
    }
1308
db0929bb4cd4 6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
diff changeset
    91
}