Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/locale/LocaleObjectCache.java
38918 views
/*1* Copyright (c) 2010, 2011, 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*/2425/*26*******************************************************************************27* Copyright (C) 2009-2010, International Business Machines Corporation and *28* others. All Rights Reserved. *29*******************************************************************************30*/31package sun.util.locale;3233import java.lang.ref.ReferenceQueue;34import java.lang.ref.SoftReference;35import java.util.concurrent.ConcurrentHashMap;36import java.util.concurrent.ConcurrentMap;3738public abstract class LocaleObjectCache<K, V> {39private ConcurrentMap<K, CacheEntry<K, V>> map;40private ReferenceQueue<V> queue = new ReferenceQueue<>();4142public LocaleObjectCache() {43this(16, 0.75f, 16);44}4546public LocaleObjectCache(int initialCapacity, float loadFactor, int concurrencyLevel) {47map = new ConcurrentHashMap<>(initialCapacity, loadFactor, concurrencyLevel);48}4950public V get(K key) {51V value = null;5253cleanStaleEntries();54CacheEntry<K, V> entry = map.get(key);55if (entry != null) {56value = entry.get();57}58if (value == null) {59V newVal = createObject(key);60// make sure key is normalized *after* the object creation61// so that newVal is assured to be created from a valid key.62key = normalizeKey(key);63if (key == null || newVal == null) {64// subclass must return non-null key/value object65return null;66}6768CacheEntry<K, V> newEntry = new CacheEntry<>(key, newVal, queue);6970entry = map.putIfAbsent(key, newEntry);71if (entry == null) {72value = newVal;73} else {74value = entry.get();75if (value == null) {76map.put(key, newEntry);77value = newVal;78}79}80}81return value;82}8384protected V put(K key, V value) {85CacheEntry<K, V> entry = new CacheEntry<>(key, value, queue);86CacheEntry<K, V> oldEntry = map.put(key, entry);87return (oldEntry == null) ? null : oldEntry.get();88}8990@SuppressWarnings("unchecked")91private void cleanStaleEntries() {92CacheEntry<K, V> entry;93while ((entry = (CacheEntry<K, V>)queue.poll()) != null) {94map.remove(entry.getKey());95}96}9798protected abstract V createObject(K key);99100protected K normalizeKey(K key) {101return key;102}103104private static class CacheEntry<K, V> extends SoftReference<V> {105private K key;106107CacheEntry(K key, V value, ReferenceQueue<V> queue) {108super(value, queue);109this.key = key;110}111112K getKey() {113return key;114}115}116}117118119