WeakValueCache.java revision 1508:a661018d34b8
1/* 2 * Copyright (c) 2015, 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 */ 25 26package jdk.nashorn.internal; 27 28import java.lang.ref.ReferenceQueue; 29import java.lang.ref.WeakReference; 30import java.util.HashMap; 31import java.util.function.Function; 32 33/** 34 * This class provides a map based cache with weakly referenced values. Cleared references are 35 * purged from the underlying map when values are retrieved or created. 36 * It uses a {@link java.util.HashMap} to store values and needs to be externally synchronized. 37 * 38 * @param <K> the key type 39 * @param <V> the value type 40 */ 41public final class WeakValueCache<K, V> { 42 43 private final HashMap<K, KeyValueReference<K, V>> map = new HashMap<>(); 44 private final ReferenceQueue<V> refQueue = new ReferenceQueue<>(); 45 46 /** 47 * Returns the value associated with {@code key}, or {@code null} if no such value exists. 48 * 49 * @param key the key 50 * @return the value or null if none exists 51 */ 52 public V get(final K key) { 53 removeClearedEntries(); 54 return findValue(key); 55 } 56 57 /** 58 * Returns the value associated with {@code key}, or creates and returns a new value if 59 * no value exists using the {@code creator} function. 60 * 61 * @param key the key 62 * @param creator function to create a new value 63 * @return the existing value, or a new one if none existed 64 */ 65 public V getOrCreate(final K key, final Function<? super K, ? extends V> creator) { 66 removeClearedEntries(); 67 68 V value = findValue(key); 69 70 if (value == null) { 71 // Define a new value if it does not exist 72 value = creator.apply(key); 73 map.put(key, new KeyValueReference<>(key, value)); 74 } 75 76 return value; 77 } 78 79 private V findValue(final K key) { 80 final KeyValueReference<K, V> ref = map.get(key); 81 if (ref != null) { 82 return ref.get(); 83 } 84 return null; 85 } 86 87 private void removeClearedEntries() { 88 // Remove cleared entries 89 for (;;) { 90 final KeyValueReference ref = (KeyValueReference) refQueue.poll(); 91 if (ref == null) { 92 break; 93 } 94 map.remove(ref.key, ref); 95 } 96 } 97 98 private static class KeyValueReference<K, V> extends WeakReference<V> { 99 final K key; 100 101 KeyValueReference(final K key, final V value) { 102 super(value); 103 this.key = key; 104 } 105 } 106 107} 108