src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/WeakValueCache.java
author smarks
Tue, 28 Mar 2017 12:10:20 -0700
changeset 47412 194f4c32678b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8174966: Unreferenced references Reviewed-by: rriggs, skoivu, rhalade, robm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     1
/*
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     4
 *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    10
 *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    15
 * accompanied this code).
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    16
 *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    20
 *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    23
 * questions.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    24
 */
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    25
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    26
package jdk.nashorn.internal;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    27
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    28
import java.lang.ref.ReferenceQueue;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    30
import java.util.HashMap;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    31
import java.util.function.Function;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    32
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    33
/**
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    34
 * This class provides a map based cache with weakly referenced values. Cleared references are
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    35
 * purged from the underlying map when values are retrieved or created.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    36
 * It uses a {@link java.util.HashMap} to store values and needs to be externally synchronized.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    37
 *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    38
 * @param <K> the key type
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    39
 * @param <V> the value type
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    40
 */
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    41
public final class WeakValueCache<K, V> {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    42
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    43
    private final HashMap<K, KeyValueReference<K, V>> map = new HashMap<>();
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    44
    private final ReferenceQueue<V> refQueue = new ReferenceQueue<>();
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    45
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    46
    /**
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    47
     * Returns the value associated with {@code key}, or {@code null} if no such value exists.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    48
     *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    49
     * @param key the key
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    50
     * @return the value or null if none exists
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    51
     */
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    52
    public V get(final K key) {
33695
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    53
        // Remove cleared entries
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    54
        for (;;) {
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    55
            final KeyValueReference<?, ?> ref = (KeyValueReference) refQueue.poll();
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    56
            if (ref == null) {
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    57
                break;
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    58
            }
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    59
            map.remove(ref.key, ref);
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    60
        }
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    61
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    62
        final KeyValueReference<K, V> ref = map.get(key);
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    63
        if (ref != null) {
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    64
            return ref.get();
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    65
        }
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    66
        return null;
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    67
    }
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    68
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    69
    /**
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    70
     * Returns the value associated with {@code key}, or creates and returns a new value if
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    71
     * no value exists using the {@code creator} function.
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    72
     *
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    73
     * @param key the key
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    74
     * @param creator function to create a new value
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    75
     * @return the existing value, or a new one if none existed
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    76
     */
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    77
    public V getOrCreate(final K key, final Function<? super K, ? extends V> creator) {
33695
f3f1bd0f638e 8142864: Raw types warning in WeakValueCache
hannesw
parents: 33690
diff changeset
    78
        V value = get(key);
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    79
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    80
        if (value == null) {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    81
            // Define a new value if it does not exist
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    82
            value = creator.apply(key);
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    83
            map.put(key, new KeyValueReference<>(key, value));
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    84
        }
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    85
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    86
        return value;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    87
    }
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    88
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    89
    private static class KeyValueReference<K, V> extends WeakReference<V> {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    90
        final K key;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    91
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    92
        KeyValueReference(final K key, final V value) {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    93
            super(value);
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    94
            this.key = key;
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    95
        }
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    96
    }
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    97
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents:
diff changeset
    98
}