Path: blob/master/src/java.base/share/classes/sun/util/locale/LocaleObjectCache.java
41159 views
/*1* Copyright (c) 2010, 2018, 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 final ConcurrentMap<K, CacheEntry<K, V>> map;40private final 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) {59key = normalizeKey(key);60V newVal = createObject(key);61if (key == null || newVal == null) {62// subclass must return non-null key/value object63return null;64}6566CacheEntry<K, V> newEntry = new CacheEntry<>(key, newVal, queue);67entry = map.putIfAbsent(key, newEntry);68if (entry == null) {69value = newVal;70} else {71value = entry.get();72if (value == null) {73map.put(key, newEntry);74value = newVal;75}76}77}78return value;79}8081protected V put(K key, V value) {82CacheEntry<K, V> entry = new CacheEntry<>(key, value, queue);83CacheEntry<K, V> oldEntry = map.put(key, entry);84return (oldEntry == null) ? null : oldEntry.get();85}8687@SuppressWarnings("unchecked")88private void cleanStaleEntries() {89CacheEntry<K, V> entry;90while ((entry = (CacheEntry<K, V>)queue.poll()) != null) {91map.remove(entry.getKey(), entry);92}93}9495protected abstract V createObject(K key);9697protected K normalizeKey(K key) {98return key;99}100101private static class CacheEntry<K, V> extends SoftReference<V> {102private K key;103104CacheEntry(K key, V value, ReferenceQueue<V> queue) {105super(value, queue);106this.key = key;107}108109K getKey() {110return key;111}112}113}114115116