Path: blob/master/src/java.desktop/share/classes/com/sun/beans/util/Cache.java
41171 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.beans.util;2526import java.lang.ref.ReferenceQueue;27import java.lang.ref.SoftReference;28import java.lang.ref.WeakReference;29import java.util.Objects;3031/**32* Hash table based implementation of the cache,33* which allows to use weak or soft references for keys and values.34* An entry in a {@code Cache} will automatically be removed35* when its key or value is no longer in ordinary use.36*37* @author Sergey Malenkov38* @since 1.839*/40public abstract class Cache<K,V> {41private static final int MAXIMUM_CAPACITY = 1 << 30; // maximum capacity MUST be a power of two <= 1<<304243private final boolean identity; // defines whether the identity comparison is used44private final Kind keyKind; // a reference kind for the cache keys45private final Kind valueKind; // a reference kind for the cache values4647private final ReferenceQueue<Object> queue = new ReferenceQueue<>(); // queue for references to remove4849private volatile CacheEntry<K,V>[] table = newTable(1 << 3); // table's length MUST be a power of two50private int threshold = 6; // the next size value at which to resize51private int size; // the number of key-value mappings contained in this map5253/**54* Creates a corresponding value for the specified key.55*56* @param key a key that can be used to create a value57* @return a corresponding value for the specified key58*/59public abstract V create(K key);6061/**62* Constructs an empty {@code Cache}.63* The default initial capacity is 8.64* The default load factor is 0.75.65*66* @param keyKind a reference kind for keys67* @param valueKind a reference kind for values68*69* @throws NullPointerException if {@code keyKind} or {@code valueKind} are {@code null}70*/71public Cache(Kind keyKind, Kind valueKind) {72this(keyKind, valueKind, false);73}7475/**76* Constructs an empty {@code Cache}77* with the specified comparison method.78* The default initial capacity is 8.79* The default load factor is 0.75.80*81* @param keyKind a reference kind for keys82* @param valueKind a reference kind for values83* @param identity defines whether reference-equality84* is used in place of object-equality85*86* @throws NullPointerException if {@code keyKind} or {@code valueKind} are {@code null}87*/88public Cache(Kind keyKind, Kind valueKind, boolean identity) {89Objects.requireNonNull(keyKind, "keyKind");90Objects.requireNonNull(valueKind, "valueKind");91this.keyKind = keyKind;92this.valueKind = valueKind;93this.identity = identity;94}9596/**97* Returns the value to which the specified key is mapped,98* or {@code null} if there is no mapping for the key.99*100* @param key the key whose cached value is to be returned101* @return a value to which the specified key is mapped,102* or {@code null} if there is no mapping for {@code key}103*104* @throws NullPointerException if {@code key} is {@code null}105* or corresponding value is {@code null}106*/107public final V get(K key) {108Objects.requireNonNull(key, "key");109removeStaleEntries();110int hash = hash(key);111// unsynchronized search improves performance112// the null value does not mean that there are no needed entry113CacheEntry<K,V>[] table = this.table; // unsynchronized access114V current = getEntryValue(key, hash, table[index(hash, table)]);115if (current != null) {116return current;117}118synchronized (this.queue) {119// synchronized search improves stability120// we must create and add new value if there are no needed entry121current = getEntryValue(key, hash, this.table[index(hash, this.table)]);122if (current != null) {123return current;124}125V value = create(key);126Objects.requireNonNull(value, "value");127int index = index(hash, this.table);128this.table[index] = new CacheEntry<>(hash, key, value, this.table[index]);129if (++this.size >= this.threshold) {130if (this.table.length == MAXIMUM_CAPACITY) {131this.threshold = Integer.MAX_VALUE;132} else {133removeStaleEntries();134table = newTable(this.table.length << 1);135transfer(this.table, table);136// If ignoring null elements and processing ref queue caused massive137// shrinkage, then restore old table. This should be rare, but avoids138// unbounded expansion of garbage-filled tables.139if (this.size >= this.threshold / 2) {140this.table = table;141this.threshold <<= 1;142} else {143transfer(table, this.table);144}145removeStaleEntries();146}147}148return value;149}150}151152/**153* Removes the cached value that corresponds to the specified key.154*155* @param key the key whose mapping is to be removed from this cache156*/157public final void remove(K key) {158if (key != null) {159synchronized (this.queue) {160removeStaleEntries();161int hash = hash(key);162int index = index(hash, this.table);163CacheEntry<K,V> prev = this.table[index];164CacheEntry<K,V> entry = prev;165while (entry != null) {166CacheEntry<K,V> next = entry.next;167if (entry.matches(hash, key)) {168if (entry == prev) {169this.table[index] = next;170} else {171prev.next = next;172}173entry.unlink();174break;175}176prev = entry;177entry = next;178}179}180}181}182183/**184* Removes all of the mappings from this cache.185* It will be empty after this call returns.186*/187public final void clear() {188synchronized (this.queue) {189int index = this.table.length;190while (0 < index--) {191CacheEntry<K,V> entry = this.table[index];192while (entry != null) {193CacheEntry<K,V> next = entry.next;194entry.unlink();195entry = next;196}197this.table[index] = null;198}199while (null != this.queue.poll()) {200// Clear out the reference queue.201}202}203}204205/**206* Retrieves object hash code and applies a supplemental hash function207* to the result hash, which defends against poor quality hash functions.208* This is critical because {@code Cache} uses power-of-two length hash tables,209* that otherwise encounter collisions for hashCodes that do not differ210* in lower bits.211*212* @param key the object which hash code is to be calculated213* @return a hash code value for the specified object214*/215private int hash(Object key) {216if (this.identity) {217int hash = System.identityHashCode(key);218return (hash << 1) - (hash << 8);219}220int hash = key.hashCode();221// This function ensures that hashCodes that differ only by222// constant multiples at each bit position have a bounded223// number of collisions (approximately 8 at default load factor).224hash ^= (hash >>> 20) ^ (hash >>> 12);225return hash ^ (hash >>> 7) ^ (hash >>> 4);226}227228/**229* Returns index of the specified hash code in the given table.230* Note that the table size must be a power of two.231*232* @param hash the hash code233* @param table the table234* @return an index of the specified hash code in the given table235*/236private static int index(int hash, Object[] table) {237return hash & (table.length - 1);238}239240/**241* Creates a new array for the cache entries.242*243* @param size requested capacity MUST be a power of two244* @return a new array for the cache entries245*/246@SuppressWarnings({"unchecked", "rawtypes"})247private CacheEntry<K,V>[] newTable(int size) {248return (CacheEntry<K,V>[]) new CacheEntry[size];249}250251private V getEntryValue(K key, int hash, CacheEntry<K,V> entry) {252while (entry != null) {253if (entry.matches(hash, key)) {254return entry.value.getReferent();255}256entry = entry.next;257}258return null;259}260261private void removeStaleEntries() {262Object reference = this.queue.poll();263if (reference != null) {264synchronized (this.queue) {265do {266if (reference instanceof Ref) {267@SuppressWarnings("rawtypes")268Ref ref = (Ref) reference;269@SuppressWarnings("unchecked")270CacheEntry<K,V> owner = (CacheEntry<K,V>) ref.getOwner();271if (owner != null) {272int index = index(owner.hash, this.table);273CacheEntry<K,V> prev = this.table[index];274CacheEntry<K,V> entry = prev;275while (entry != null) {276CacheEntry<K,V> next = entry.next;277if (entry == owner) {278if (entry == prev) {279this.table[index] = next;280} else {281prev.next = next;282}283entry.unlink();284break;285}286prev = entry;287entry = next;288}289}290}291reference = this.queue.poll();292}293while (reference != null);294}295}296}297298private void transfer(CacheEntry<K,V>[] oldTable, CacheEntry<K,V>[] newTable) {299int oldIndex = oldTable.length;300while (0 < oldIndex--) {301CacheEntry<K,V> entry = oldTable[oldIndex];302oldTable[oldIndex] = null;303while (entry != null) {304CacheEntry<K,V> next = entry.next;305if (entry.key.isStale() || entry.value.isStale()) {306entry.unlink();307} else {308int newIndex = index(entry.hash, newTable);309entry.next = newTable[newIndex];310newTable[newIndex] = entry;311}312entry = next;313}314}315}316317/**318* Represents a cache entry (key-value pair).319*/320private final class CacheEntry<K,V> {321private final int hash;322private final Ref<K> key;323private final Ref<V> value;324private volatile CacheEntry<K,V> next;325326/**327* Constructs an entry for the cache.328*329* @param hash the hash code calculated for the entry key330* @param key the entry key331* @param value the initial value of the entry332* @param next the next entry in a chain333*/334private CacheEntry(int hash, K key, V value, CacheEntry<K,V> next) {335this.hash = hash;336this.key = Cache.this.keyKind.create(this, key, Cache.this.queue);337this.value = Cache.this.valueKind.create(this, value, Cache.this.queue);338this.next = next;339}340341/**342* Determines whether the entry has the given key with the given hash code.343*344* @param hash an expected hash code345* @param object an object to be compared with the entry key346* @return {@code true} if the entry has the given key with the given hash code;347* {@code false} otherwise348*/349private boolean matches(int hash, Object object) {350if (this.hash != hash) {351return false;352}353Object key = this.key.getReferent();354return (key == object) || !Cache.this.identity && (key != null) && key.equals(object);355}356357/**358* Marks the entry as actually removed from the cache.359*/360private void unlink() {361this.next = null;362this.key.removeOwner();363this.value.removeOwner();364Cache.this.size--;365}366}367368/**369* Basic interface for references.370* It defines the operations common for the all kind of references.371*372* @param <T> the type of object to refer373*/374private static interface Ref<T> {375/**376* Returns the object that possesses information about the reference.377*378* @return the owner of the reference or {@code null} if the owner is unknown379*/380Object getOwner();381382/**383* Returns the object to refer.384*385* @return the referred object or {@code null} if it was collected386*/387T getReferent();388389/**390* Determines whether the referred object was taken by the garbage collector or not.391*392* @return {@code true} if the referred object was collected393*/394boolean isStale();395396/**397* Marks this reference as removed from the cache.398*/399void removeOwner();400}401402/**403* Represents a reference kind.404*/405public static enum Kind {406STRONG {407<T> Ref<T> create(Object owner, T value, ReferenceQueue<? super T> queue) {408return new Strong<>(owner, value);409}410},411SOFT {412<T> Ref<T> create(Object owner, T referent, ReferenceQueue<? super T> queue) {413return (referent == null)414? new Strong<>(owner, referent)415: new Soft<>(owner, referent, queue);416}417},418WEAK {419<T> Ref<T> create(Object owner, T referent, ReferenceQueue<? super T> queue) {420return (referent == null)421? new Strong<>(owner, referent)422: new Weak<>(owner, referent, queue);423}424};425426/**427* Creates a reference to the specified object.428*429* @param <T> the type of object to refer430* @param owner the owner of the reference, if needed431* @param referent the object to refer432* @param queue the queue to register the reference with,433* or {@code null} if registration is not required434* @return the reference to the specified object435*/436abstract <T> Ref<T> create(Object owner, T referent, ReferenceQueue<? super T> queue);437438/**439* This is an implementation of the {@link Cache.Ref} interface440* that uses the strong references that prevent their referents441* from being made finalizable, finalized, and then reclaimed.442*443* @param <T> the type of object to refer444*/445private static final class Strong<T> implements Ref<T> {446private Object owner;447private final T referent;448449/**450* Creates a strong reference to the specified object.451*452* @param owner the owner of the reference, if needed453* @param referent the non-null object to refer454*/455private Strong(Object owner, T referent) {456this.owner = owner;457this.referent = referent;458}459460/**461* Returns the object that possesses information about the reference.462*463* @return the owner of the reference or {@code null} if the owner is unknown464*/465public Object getOwner() {466return this.owner;467}468469/**470* Returns the object to refer.471*472* @return the referred object473*/474public T getReferent() {475return this.referent;476}477478/**479* Determines whether the referred object was taken by the garbage collector or not.480*481* @return {@code true} if the referred object was collected482*/483public boolean isStale() {484return false;485}486487/**488* Marks this reference as removed from the cache.489*/490public void removeOwner() {491this.owner = null;492}493}494495/**496* This is an implementation of the {@link Cache.Ref} interface497* that uses the soft references that are cleared at the discretion498* of the garbage collector in response to a memory request.499*500* @param <T> the type of object to refer501* @see java.lang.ref.SoftReference502*/503private static final class Soft<T> extends SoftReference<T> implements Ref<T> {504private Object owner;505506/**507* Creates a soft reference to the specified object.508*509* @param owner the owner of the reference, if needed510* @param referent the non-null object to refer511* @param queue the queue to register the reference with,512* or {@code null} if registration is not required513*/514private Soft(Object owner, T referent, ReferenceQueue<? super T> queue) {515super(referent, queue);516this.owner = owner;517}518519/**520* Returns the object that possesses information about the reference.521*522* @return the owner of the reference or {@code null} if the owner is unknown523*/524public Object getOwner() {525return this.owner;526}527528/**529* Returns the object to refer.530*531* @return the referred object or {@code null} if it was collected532*/533public T getReferent() {534return get();535}536537/**538* Determines whether the referred object was taken by the garbage collector or not.539*540* @return {@code true} if the referred object was collected541*/542public boolean isStale() {543return null == get();544}545546/**547* Marks this reference as removed from the cache.548*/549public void removeOwner() {550this.owner = null;551}552}553554/**555* This is an implementation of the {@link Cache.Ref} interface556* that uses the weak references that do not prevent their referents557* from being made finalizable, finalized, and then reclaimed.558*559* @param <T> the type of object to refer560* @see java.lang.ref.WeakReference561*/562private static final class Weak<T> extends WeakReference<T> implements Ref<T> {563private Object owner;564565/**566* Creates a weak reference to the specified object.567*568* @param owner the owner of the reference, if needed569* @param referent the non-null object to refer570* @param queue the queue to register the reference with,571* or {@code null} if registration is not required572*/573private Weak(Object owner, T referent, ReferenceQueue<? super T> queue) {574super(referent, queue);575this.owner = owner;576}577578/**579* Returns the object that possesses information about the reference.580*581* @return the owner of the reference or {@code null} if the owner is unknown582*/583public Object getOwner() {584return this.owner;585}586587/**588* Returns the object to refer.589*590* @return the referred object or {@code null} if it was collected591*/592public T getReferent() {593return get();594}595596/**597* Determines whether the referred object was taken by the garbage collector or not.598*599* @return {@code true} if the referred object was collected600*/601public boolean isStale() {602return null == get();603}604605/**606* Marks this reference as removed from the cache.607*/608public void removeOwner() {609this.owner = null;610}611}612}613}614615616