]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/cojen/util/SoftValuedHashMap.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / cojen / util / SoftValuedHashMap.java
1 /*
2  *  Copyright 2004-2010 Brian S O'Neill
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.cojen.util;
18
19 import java.lang.ref.Reference;
20 import java.lang.ref.SoftReference;
21 import java.util.Map;
22
23 /**
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.
27  * <p>
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().
34  * 
35  * @author Brian S O'Neill
36  */
37 public class SoftValuedHashMap<K, V> extends ReferencedValueHashMap<K, V> {
38
39     /**
40      * Constructs a new, empty map with the specified initial 
41      * capacity and the specified load factor. 
42      *
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.
47      */
48     public SoftValuedHashMap(int initialCapacity, float loadFactor) {
49         super(initialCapacity, loadFactor);
50     }
51
52     /**
53      * Constructs a new, empty map with the specified initial capacity
54      * and default load factor, which is <tt>0.75</tt>.
55      *
56      * @param   initialCapacity   the initial capacity of the HashMap.
57      * @throws    IllegalArgumentException if the initial capacity is less
58      *              than zero.
59      */
60     public SoftValuedHashMap(int initialCapacity) {
61         super(initialCapacity);
62     }
63
64     /**
65      * Constructs a new, empty map with a default capacity and load
66      * factor, which is <tt>0.75</tt>.
67      */
68     public SoftValuedHashMap() {
69         super();
70     }
71
72     /**
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>.
77      */
78     public SoftValuedHashMap(Map<? extends K, ? extends V> t) {
79         super(t);
80     }
81
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);
84     }
85
86     static class SoftEntry<K, V> extends ReferencedValueHashMap.Entry<K, V> {
87
88         SoftEntry(int hash, K key, V value, Entry<K, V> next) {
89             super(hash, key, value, next);
90         }
91
92         SoftEntry(int hash, K key, Reference<V> value, Entry<K, V> next) {
93             super(hash, key, value, next);
94         }
95
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);
99         }
100
101         Reference<V> newReference(V value) {
102             return new SoftReference<V>(value);
103         }
104     }
105 }