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