2 * Copyright 2004-2010 Brian S O'Neill
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.cojen.util;
19 import java.lang.ref.Reference;
20 import java.lang.ref.SoftReference;
24 * A Map that softly references its values and can be used as a simple cache.
25 * SoftValuedHashMap is not thread-safe and must be wrapped with
26 * Collections.synchronizedMap to be made thread-safe.
28 * Note: Softly referenced entries may be automatically removed during
29 * either accessor or mutator operations, possibly causing a concurrent
30 * modification to be detected. Therefore, even if multiple threads are only
31 * accessing this map, be sure to synchronize this map first. Also, do not
32 * rely on the value returned by size() when using an iterator from this map.
33 * The iterators may return less entries than the amount reported by size().
35 * @author Brian S O'Neill
37 public class SoftValuedHashMap<K, V> extends ReferencedValueHashMap<K, V> {
40 * Constructs a new, empty map with the specified initial
41 * capacity and the specified load factor.
43 * @param initialCapacity the initial capacity of the HashMap.
44 * @param loadFactor the load factor of the HashMap
45 * @throws IllegalArgumentException if the initial capacity is less
46 * than zero, or if the load factor is nonpositive.
48 public SoftValuedHashMap(int initialCapacity, float loadFactor) {
49 super(initialCapacity, loadFactor);
53 * Constructs a new, empty map with the specified initial capacity
54 * and default load factor, which is <tt>0.75</tt>.
56 * @param initialCapacity the initial capacity of the HashMap.
57 * @throws IllegalArgumentException if the initial capacity is less
60 public SoftValuedHashMap(int initialCapacity) {
61 super(initialCapacity);
65 * Constructs a new, empty map with a default capacity and load
66 * factor, which is <tt>0.75</tt>.
68 public SoftValuedHashMap() {
73 * Constructs a new map with the same mappings as the given map. The
74 * map is created with a capacity of twice the number of mappings in
75 * the given map or 11 (whichever is greater), and a default load factor,
76 * which is <tt>0.75</tt>.
78 public SoftValuedHashMap(Map<? extends K, ? extends V> t) {
82 Entry<K, V> newEntry(int hash, K key, V value, Entry<K, V> next) {
83 return new SoftEntry<K, V>(hash, key, value, next);
86 static class SoftEntry<K, V> extends ReferencedValueHashMap.Entry<K, V> {
88 SoftEntry(int hash, K key, V value, Entry<K, V> next) {
89 super(hash, key, value, next);
92 SoftEntry(int hash, K key, Reference<V> value, Entry<K, V> next) {
93 super(hash, key, value, next);
96 @SuppressWarnings("rawtypes")
97 Entry newEntry(int hash, K key, Reference<V> value, Entry<K, V> next) {
98 return new SoftEntry<K, V>(hash, key, value, next);
101 Reference<V> newReference(V value) {
102 return new SoftReference<V>(value);