2 ****************************************************************************
\r
3 * Copyright (c) 2007-2009 International Business Machines Corporation and *
\r
4 * others. All rights reserved. *
\r
5 ****************************************************************************
\r
8 package com.ibm.icu.impl;
\r
10 import java.lang.ref.Reference;
\r
11 import java.lang.ref.SoftReference;
\r
12 import java.lang.ref.WeakReference;
\r
13 import java.util.Collections;
\r
14 import java.util.HashMap;
\r
15 import java.util.Map;
\r
17 public class SimpleCache<K, V> implements ICUCache<K, V> {
\r
18 private static final int DEFAULT_CAPACITY = 16;
\r
20 private Reference<Map<K, V>> cacheRef = null;
\r
21 private int type = ICUCache.SOFT;
\r
22 private int capacity = DEFAULT_CAPACITY;
\r
24 public SimpleCache() {
\r
27 public SimpleCache(int cacheType) {
\r
28 this(cacheType, DEFAULT_CAPACITY);
\r
31 public SimpleCache(int cacheType, int initialCapacity) {
\r
32 if (cacheType == ICUCache.WEAK) {
\r
35 if (initialCapacity > 0) {
\r
36 capacity = initialCapacity;
\r
40 public V get(Object key) {
\r
41 Reference<Map<K, V>> ref = cacheRef;
\r
43 Map<K, V> map = ref.get();
\r
45 return map.get(key);
\r
51 public void put(K key, V value) {
\r
52 Reference<Map<K, V>> ref = cacheRef;
\r
53 Map<K, V> map = null;
\r
58 map = Collections.synchronizedMap(new HashMap<K, V>(capacity));
\r
59 if (type == ICUCache.WEAK) {
\r
60 ref = new WeakReference<Map<K, V>>(map);
\r
62 ref = new SoftReference<Map<K, V>>(map);
\r
66 map.put(key, value);
\r
69 public void clear() {
\r