2 *******************************************************************************
\r
3 * Copyright (C) 2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.impl.locale;
\r
9 import java.lang.ref.Reference;
\r
10 import java.lang.ref.ReferenceQueue;
\r
11 import java.lang.ref.WeakReference;
\r
12 import java.util.Collections;
\r
13 import java.util.HashMap;
\r
14 import java.util.Map;
\r
16 //public class LocaleObjectCache<K, V> {
\r
17 public class LocaleObjectCache {
\r
19 // private ConcurrentHashMap<K, WeakValueRef<V>> _map = new ConcurrentHashMap<K, WeakValueRef<V>>();
\r
20 private Map _map = Collections.synchronizedMap(new HashMap());
\r
21 // private ReferenceQueue<V> _rq = new ReferenceQueue<V>();
\r
22 private ReferenceQueue _rq = new ReferenceQueue();
\r
24 public LocaleObjectCache() {
\r
27 // public V get(Object key) {
\r
28 public Object get(Object key) {
\r
29 expungeStaleEntries();
\r
30 // WeakValueRef<V> ref = _map.get(key);
\r
31 WeakValueRef ref = (WeakValueRef)_map.get(key);
\r
39 * Unlike Map#put, this method returns non-null value actually
\r
40 * in the cache, even no values for the key was not available
\r
43 // public V put(K key, V value) {
\r
44 public Object put(Object key, Object value) {
\r
45 expungeStaleEntries();
\r
46 // WeakValueRef<V> ref = _map.get(key);
\r
47 WeakValueRef ref = (WeakValueRef)_map.get(key);
\r
49 // Make sure if another thread put the new value
\r
50 // V valInCache = ref.get();
\r
51 Object valInCache = (String)ref.get();
\r
52 if (valInCache != null) {
\r
56 // We do not synchronize the internal map here.
\r
57 // In the worst case, another thread may put the new
\r
58 // value with the same contents, but it should not cause
\r
59 // any serious problem.
\r
60 // _map.put(key, new WeakValueRef<V>(key, value, _rq));
\r
61 _map.put(key, new WeakValueRef(key, value, _rq));
\r
65 private void expungeStaleEntries() {
\r
66 // Reference<? extends V> val;
\r
68 while ((val = _rq.poll()) != null) {
\r
69 // Object key = ((WeakValueRef<?>)val).getKey();
\r
70 Object key = ((WeakValueRef)val).getKey();
\r
75 // private static class WeakValueRef<V> extends WeakReference<V> {
\r
76 private static class WeakValueRef extends WeakReference {
\r
77 private Object _key;
\r
79 // public WeakValueRef(Object key, V value, ReferenceQueue<V> rq) {
\r
80 public WeakValueRef(Object key, Object value, ReferenceQueue rq) {
\r
86 public Object get() {
\r
90 public Object getKey() {
\r