History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/beans/WeakCache.java (1 of 18352 files). Show all

Unified · Split

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