src/java.base/share/classes/java/lang/WeakPairMap.java
author darcy
Wed, 15 Aug 2018 10:44:56 -0700
changeset 51413 43e41800d579
parent 47471 304ef03403b1
permissions -rw-r--r--
5075463: (enum) Serialized Form javadoc for java.lang.Enum is misleading Reviewed-by: lancea, rriggs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     1
/*
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     4
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    10
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    15
 * accompanied this code).
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    16
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    20
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    23
 * questions.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    24
 */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    25
package java.lang;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    26
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    27
import java.lang.ref.Reference;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    28
import java.lang.ref.ReferenceQueue;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    30
import java.util.Collection;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    31
import java.util.Objects;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    32
import java.util.concurrent.ConcurrentHashMap;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    33
import java.util.function.BiFunction;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    34
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    35
/**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    36
 * A WeakHashMap-like data structure that uses a pair of weakly-referenced keys
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    37
 * with identity equality semantics to associate a strongly-referenced value.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    38
 * Unlike WeakHashMap, this data structure is thread-safe.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    39
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    40
 * @param <K1> the type of 1st key in key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    41
 * @param <K2> the type of 2nd key in key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    42
 * @param <V>  the type of value
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    43
 * @author Peter Levart
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    44
 */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    45
final class WeakPairMap<K1, K2, V> {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    46
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    47
    private final ConcurrentHashMap<Pair<K1, K2>, V> map = new ConcurrentHashMap<>();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    48
    private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    49
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    50
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    51
     * Tests if the specified pair of keys are associated with a value
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    52
     * in the WeakPairMap.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    53
     *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    54
     * @param k1 the 1st of the pair of keys
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    55
     * @param k2 the 2nd of the pair of keys
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    56
     * @return true if and only if the specified key pair is in this WeakPairMap,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    57
     * as determined by the identity comparison; false otherwise
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    58
     * @throws NullPointerException if any of the specified keys is null
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    59
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    60
    public boolean containsKeyPair(K1 k1, K2 k2) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    61
        expungeStaleAssociations();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    62
        return map.containsKey(Pair.lookup(k1, k2));
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    63
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    64
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    65
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    66
     * Returns the value to which the specified pair of keys is mapped, or null
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    67
     * if this WeakPairMap contains no mapping for the key pair.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    68
     * <p>More formally, if this WeakPairMap contains a mapping from a key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    69
     * {@code (_k1, _k2)} to a value {@code v} such that
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    70
     * {@code k1 == _k1 && k2 == _k2}, then this method returns {@code v};
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    71
     * otherwise it returns {@code null}.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    72
     * (There can be at most one such mapping.)
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    73
     *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    74
     * @param k1 the 1st of the pair of keys for which the mapped value is to
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    75
     *           be returned
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    76
     * @param k2 the 2nd of the pair of keys for which the mapped value is to
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    77
     *           be returned
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    78
     * @return the value to which the specified key pair is mapped, or null if
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    79
     * this map contains no mapping for the key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    80
     * @throws NullPointerException if any of the specified keys is null
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    81
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    82
    public V get(K1 k1, K2 k2) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    83
        expungeStaleAssociations();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    84
        return map.get(Pair.lookup(k1, k2));
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    85
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    86
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    87
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    88
     * Maps the specified key pair to the specified value in this WeakPairMap.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    89
     * Neither the keys nor the value can be null.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    90
     * <p>The value can be retrieved by calling the {@link #get} method
47471
304ef03403b1 8190323: "the the" typos
rriggs
parents: 47216
diff changeset
    91
     * with the same keys (compared by identity).
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    92
     *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    93
     * @param k1 the 1st of the pair of keys with which the specified value is to
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    94
     *           be associated
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    95
     * @param k2 the 2nd of the pair of keys with which the specified value is to
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    96
     *           be associated
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    97
     * @param v  value to be associated with the specified key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    98
     * @return the previous value associated with key pair, or {@code null} if
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    99
     * there was no mapping for key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   100
     * @throws NullPointerException if any of the specified keys or value is null
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   101
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   102
    public V put(K1 k1, K2 k2, V v) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   103
        expungeStaleAssociations();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   104
        return map.put(Pair.weak(k1, k2, queue), v);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   105
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   106
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   107
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   108
     * If the specified key pair is not already associated with a value,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   109
     * associates it with the given value and returns {@code null}, else does
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   110
     * nothing and returns the currently associated value.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   111
     *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   112
     * @param k1 the 1st of the pair of keys with which the specified value is to
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   113
     *           be associated
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   114
     * @param k2 the 2nd of the pair of keys with which the specified value is to
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   115
     *           be associated
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   116
     * @param v  value to be associated with the specified key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   117
     * @return the previous value associated with key pair, or {@code null} if
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   118
     * there was no mapping for key pair
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   119
     * @throws NullPointerException if any of the specified keys or value is null
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   120
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   121
    public V putIfAbsent(K1 k1, K2 k2, V v) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   122
        expungeStaleAssociations();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   123
        return map.putIfAbsent(Pair.weak(k1, k2, queue), v);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   124
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   125
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   126
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   127
     * If the specified key pair is not already associated with a value,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   128
     * attempts to compute its value using the given mapping function
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   129
     * and enters it into this WeakPairMap unless {@code null}. The entire
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   130
     * method invocation is performed atomically, so the function is
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   131
     * applied at most once per key pair. Some attempted update operations
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   132
     * on this WeakPairMap by other threads may be blocked while computation
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   133
     * is in progress, so the computation should be short and simple,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   134
     * and must not attempt to update any other mappings of this WeakPairMap.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   135
     *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   136
     * @param k1              the 1st of the pair of keys with which the
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   137
     *                        computed value is to be associated
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   138
     * @param k2              the 2nd of the pair of keys with which the
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   139
     *                        computed value is to be associated
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   140
     * @param mappingFunction the function to compute a value
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   141
     * @return the current (existing or computed) value associated with
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   142
     * the specified key pair, or null if the computed value is null
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   143
     * @throws NullPointerException  if any of the specified keys or
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   144
     *                               mappingFunction is null
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   145
     * @throws IllegalStateException if the computation detectably
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   146
     *                               attempts a recursive update to this map
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   147
     *                               that would otherwise never complete
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   148
     * @throws RuntimeException      or Error if the mappingFunction does so, in
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   149
     *                               which case the mapping is left unestablished
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   150
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   151
    public V computeIfAbsent(K1 k1, K2 k2,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   152
                             BiFunction<? super K1, ? super K2, ? extends V>
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   153
                                 mappingFunction) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   154
        expungeStaleAssociations();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   155
        try {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   156
            return map.computeIfAbsent(
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   157
                Pair.weak(k1, k2, queue),
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   158
                pair -> mappingFunction.apply(pair.first(), pair.second()));
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   159
        } finally {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   160
            Reference.reachabilityFence(k1);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   161
            Reference.reachabilityFence(k2);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   162
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   163
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   164
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   165
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   166
     * Returns a {@link Collection} view of the values contained in this
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   167
     * WeakPairMap. The collection is backed by the WeakPairMap, so changes to
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   168
     * the map are reflected in the collection, and vice-versa.  The collection
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   169
     * supports element removal, which removes the corresponding
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   170
     * mapping from this map, via the {@code Iterator.remove},
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   171
     * {@code Collection.remove}, {@code removeAll},
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   172
     * {@code retainAll}, and {@code clear} operations.  It does not
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   173
     * support the {@code add} or {@code addAll} operations.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   174
     *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   175
     * @return the collection view
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   176
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   177
    public Collection<V> values() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   178
        expungeStaleAssociations();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   179
        return map.values();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   180
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   181
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   182
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   183
     * Removes associations from this WeakPairMap for which at least one of the
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   184
     * keys in key pair has been found weakly-reachable and corresponding
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   185
     * WeakRefPeer(s) enqueued. Called as part of each public operation.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   186
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   187
    private void expungeStaleAssociations() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   188
        WeakRefPeer<?> peer;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   189
        while ((peer = (WeakRefPeer<?>) queue.poll()) != null) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   190
            map.remove(peer.weakPair());
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   191
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   192
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   193
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   194
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   195
     * Common interface of both {@link Weak} and {@link Lookup} key pairs.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   196
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   197
    private interface Pair<K1, K2> {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   198
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   199
        static <K1, K2> Pair<K1, K2> weak(K1 k1, K2 k2,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   200
                                          ReferenceQueue<Object> queue) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   201
            return new Weak<>(k1, k2, queue);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   202
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   203
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   204
        static <K1, K2> Pair<K1, K2> lookup(K1 k1, K2 k2) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   205
            return new Lookup<>(k1, k2);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   206
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   207
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   208
        /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   209
         * @return The 1st of the pair of keys (may be null for {@link Weak}
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   210
         * when it gets cleared)
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   211
         */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   212
        K1 first();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   213
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   214
        /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   215
         * @return The 2nd of the pair of keys (may be null for {@link Weak}
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   216
         * when it gets cleared)
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   217
         */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   218
        K2 second();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   219
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   220
        static int hashCode(Object first, Object second) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   221
            // assert first != null && second != null;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   222
            return System.identityHashCode(first) ^
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   223
                   System.identityHashCode(second);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   224
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   225
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   226
        static boolean equals(Object first, Object second, Pair<?, ?> p) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   227
            return first != null && second != null &&
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   228
                   first == p.first() && second == p.second();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   229
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   230
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   231
        /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   232
         * A Pair where both keys are weakly-referenced.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   233
         * It is composed of two instances of {@link WeakRefPeer}s:
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   234
         * <pre>{@code
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   235
         *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   236
         *     +-referent-> [K1]                +-referent-> [K2]
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   237
         *     |                                |
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   238
         *   +----------------+               +----------------+
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   239
         *   | Pair.Weak <:   |-----peer----->| (anonymous) <: |
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   240
         *   | WeakRefPeer,   |               | WeakRefPeer    |
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   241
         *   | Pair           |<--weakPair()--|                |
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   242
         *   +----------------+               +----------------+
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   243
         *     |            ^
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   244
         *     |            |
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   245
         *     +-weakPair()-+
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   246
         *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   247
         * }</pre>
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   248
         * <p>
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   249
         * Pair.Weak is used for CHM keys. Both peers are associated with the
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   250
         * same {@link ReferenceQueue} so when either of their referents
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   251
         * becomes weakly-reachable, the corresponding entries can be
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   252
         * {@link #expungeStaleAssociations() expunged} from the map.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   253
         */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   254
        final class Weak<K1, K2> extends WeakRefPeer<K1> implements Pair<K1, K2> {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   255
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   256
            // saved hash so it can be retrieved after the reference is cleared
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   257
            private final int hash;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   258
            // link to <K2> peer
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   259
            private final WeakRefPeer<K2> peer;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   260
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   261
            Weak(K1 k1, K2 k2, ReferenceQueue<Object> queue) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   262
                super(k1, queue);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   263
                hash = Pair.hashCode(k1, k2);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   264
                peer = new WeakRefPeer<>(k2, queue) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   265
                    // link back to <K1> peer
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   266
                    @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   267
                    Weak<?, ?> weakPair() { return Weak.this; }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   268
                };
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   269
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   270
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   271
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   272
            Weak<?, ?> weakPair() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   273
                return this;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   274
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   275
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   276
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   277
            public K1 first() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   278
                return get();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   279
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   280
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   281
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   282
            public K2 second() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   283
                return peer.get();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   284
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   285
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   286
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   287
            public int hashCode() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   288
                return hash;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   289
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   290
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   291
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   292
            public boolean equals(Object obj) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   293
                return this == obj ||
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   294
                       (obj instanceof Pair &&
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   295
                        Pair.equals(first(), second(), (Pair<?, ?>) obj));
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   296
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   297
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   298
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   299
        /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   300
         * Optimized lookup Pair, used as lookup key in methods like
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   301
         * {@link java.util.Map#get(Object)} or
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   302
         * {@link java.util.Map#containsKey(Object)}) where
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   303
         * there is a great chance its allocation is eliminated
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   304
         * by escape analysis when such lookups are inlined by JIT.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   305
         * All its methods are purposely designed so that 'this' is never
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   306
         * passed to any other method or used as identity.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   307
         */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   308
        final class Lookup<K1, K2> implements Pair<K1, K2> {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   309
            private final K1 k1;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   310
            private final K2 k2;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   311
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   312
            Lookup(K1 k1, K2 k2) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   313
                this.k1 = Objects.requireNonNull(k1);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   314
                this.k2 = Objects.requireNonNull(k2);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   315
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   316
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   317
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   318
            public K1 first() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   319
                return k1;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   320
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   321
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   322
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   323
            public K2 second() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   324
                return k2;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   325
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   326
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   327
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   328
            public int hashCode() {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   329
                return Pair.hashCode(k1, k2);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   330
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   331
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   332
            @Override
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   333
            public boolean equals(Object obj) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   334
                return obj instanceof Pair &&
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   335
                       Pair.equals(k1, k2, (Pair<?, ?>) obj);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   336
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   337
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   338
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   339
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   340
    /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   341
     * Common abstract supertype of a pair of WeakReference peers.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   342
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   343
    private static abstract class WeakRefPeer<K> extends WeakReference<K> {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   344
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   345
        WeakRefPeer(K k, ReferenceQueue<Object> queue) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   346
            super(Objects.requireNonNull(k), queue);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   347
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   348
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   349
        /**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   350
         * @return the {@link Pair.Weak} side of the pair of peers.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   351
         */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   352
        abstract Pair.Weak<?, ?> weakPair();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   353
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   354
}