jdk/src/share/classes/sun/util/locale/LocaleObjectCache.java
author okutsu
Thu, 14 Apr 2011 15:59:47 +0900
changeset 9224 75c0420badef
parent 6501 684810d882b3
child 10118 c22b087661be
permissions -rw-r--r--
7028818: (lc) Lazily initialize locale extension 7029740: (lc) New Locale class implementation doesn't follow the Java coding conventions 7032820: (lc) sun.util.locale.InternalLocaleBuilder.CaseInsensitiveChar.equals problems 7033503: (lc) Restore optimization code for Locale class initialization 7033504: (lc) incompatible behavior change for ja_JP_JP and th_TH_TH locales Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     1
/*
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     4
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    10
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    15
 * accompanied this code).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    16
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    20
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    23
 * questions.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    24
 */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    25
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    26
/*
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    27
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    28
 * Copyright (C) 2009-2010, International Business Machines Corporation and    *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    29
 * others. All Rights Reserved.                                                *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    30
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    31
 */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    32
package sun.util.locale;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    33
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    34
import java.lang.ref.ReferenceQueue;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    35
import java.lang.ref.SoftReference;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    36
import java.util.concurrent.ConcurrentHashMap;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    37
import java.util.concurrent.ConcurrentMap;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    38
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    39
public abstract class LocaleObjectCache<K, V> {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    40
    private ConcurrentMap<K, CacheEntry<K, V>> map;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    41
    private ReferenceQueue<V> queue = new ReferenceQueue<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    42
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    43
    public LocaleObjectCache() {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    44
        this(16, 0.75f, 16);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    45
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    46
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    47
    public LocaleObjectCache(int initialCapacity, float loadFactor, int concurrencyLevel) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    48
        map = new ConcurrentHashMap<>(initialCapacity, loadFactor, concurrencyLevel);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    49
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    50
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    51
    public V get(K key) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    52
        V value = null;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    53
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    54
        cleanStaleEntries();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    55
        CacheEntry<K, V> entry = map.get(key);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    56
        if (entry != null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    57
            value = entry.get();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    58
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    59
        if (value == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    60
            key = normalizeKey(key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    61
            V newVal = createObject(key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    62
            if (key == null || newVal == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    63
                // subclass must return non-null key/value object
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    64
                return null;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    65
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    66
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    67
            CacheEntry<K, V> newEntry = new CacheEntry<>(key, newVal, queue);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    68
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    69
            while (value == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    70
                cleanStaleEntries();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    71
                entry = map.putIfAbsent(key, newEntry);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    72
                if (entry == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    73
                    value = newVal;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    74
                    break;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    75
                } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    76
                    value = entry.get();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    77
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    78
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    79
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    80
        return value;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    81
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    82
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    83
    protected V put(K key, V value) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    84
        CacheEntry<K, V> entry = new CacheEntry<>(key, value, queue);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    85
        CacheEntry<K, V> oldEntry = map.put(key, entry);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    86
        return (oldEntry == null) ? null : oldEntry.get();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    87
    }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    88
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    89
    @SuppressWarnings("unchecked")
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    90
    private void cleanStaleEntries() {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    91
        CacheEntry<K, V> entry;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    92
        while ((entry = (CacheEntry<K, V>)queue.poll()) != null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    93
            map.remove(entry.getKey());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    94
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    95
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    96
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    97
    protected abstract V createObject(K key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    98
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    99
    protected K normalizeKey(K key) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   100
        return key;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   101
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   102
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   103
    private static class CacheEntry<K, V> extends SoftReference<V> {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   104
        private K key;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   105
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   106
        CacheEntry(K key, V value, ReferenceQueue<V> queue) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   107
            super(value, queue);
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   108
            this.key = key;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   109
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   110
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   111
        K getKey() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   112
            return key;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   113
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   114
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   115
}