Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SoftCache |
|
| 0.0;0 |
1 | /* | |
2 | * RCache - A collection of simple reference-based cache implementations. | |
3 | * Copyright (C) 2007 Rodrigo Ruiz | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | * Alternatively, the contents of this file may be used under the terms | |
20 | * of the Apache 2.0 license (the "Apache License"), in which case its | |
21 | * provisions are applicable instead of those above. If you wish to allow use | |
22 | * of your version of this file only* under the terms of the Apache License | |
23 | * and not to allow others to use your version of this file under the LGPL, | |
24 | * indicate your decision by* deleting the provisions above and replace them | |
25 | * with the notice and other provisions required by the Apache License. If | |
26 | * you do not delete the provisions above, a recipient may use your version of | |
27 | * this file under either the LGPL or the Apache License. | |
28 | */ | |
29 | package net.sourceforge.rcache; | |
30 | ||
31 | import java.lang.ref.ReferenceQueue; | |
32 | ||
33 | /** | |
34 | * <p>Memory-Sensitive Cache Based on SoftReferences</p> | |
35 | * | |
36 | * <p>Entries in a SoftCache can be garbage collected if, and only if, the JVM | |
37 | * considers that more memory is needed. This means that all SoftCache instances | |
38 | * will be effectively cleared before an OutOfMemory error is thrown.</p> | |
39 | * | |
40 | * <p>For certain applications, this class may be too much memory-consuming. If | |
41 | * the programmer only needs to enforce that a single instance is used for a | |
42 | * given key, and it is not important to keep this instance afterwards, | |
43 | * {@link WeakCache} may be a better option.</p> | |
44 | * | |
45 | * @see java.lang.ref.SoftReference | |
46 | * | |
47 | * @author Rodrigo Ruiz | |
48 | * @param <K> the type of keys maintained by this cache | |
49 | * @param <V> the type of cached values | |
50 | */ | |
51 | public class SoftCache<K, V> extends BaseCache<K, V> { | |
52 | ||
53 | /** | |
54 | * <p>Default constructor.</p> | |
55 | * | |
56 | * <p>Instances created with this constructor will have a small initial | |
57 | * capacity and a highly restricted concurrency level.</p> | |
58 | * | |
59 | * <p>For concurrent maps, resizing is a costly operation. Whenever possible, | |
60 | * it is advised to specify the expected maximum size, in order to | |
61 | * minimise the number of resizes.</p> | |
62 | */ | |
63 | public SoftCache() { | |
64 | 5 | super(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
65 | 5 | } |
66 | ||
67 | /** | |
68 | * <p>Creates an instance with a custom initial capacity.</p> | |
69 | * | |
70 | * @param initialCapacity The map initial capacity | |
71 | */ | |
72 | public SoftCache(int initialCapacity) { | |
73 | 1 | super(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
74 | 1 | } |
75 | ||
76 | /** | |
77 | * <p>Creates an instance with a custom initial capacity and load factor.</p> | |
78 | * | |
79 | * @param initialCapacity The map initial capacity | |
80 | * @param loadFactor The map load factor | |
81 | */ | |
82 | public SoftCache(int initialCapacity, float loadFactor) { | |
83 | 1 | super(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL); |
84 | 1 | } |
85 | ||
86 | /** | |
87 | * <p>Creates an instance with custom capacity and concurrency level.</p> | |
88 | * | |
89 | * <p>The concurrency level is used as a hint to select the internal Map | |
90 | * implementation:</p> | |
91 | * | |
92 | * <ul> | |
93 | * <li>< 0 : Do not care about concurrency. | |
94 | * A {@link java.util.HashMap} will be used.</li> | |
95 | * <li>= 0 : No concurrent access allowed. | |
96 | * A {@link java.util.Hashtable} will be selected.</li> | |
97 | * <li>> 0 : A {@link java.util.concurrent.ConcurrentHashMap} will be | |
98 | * created with the specified concurrency level.</li> | |
99 | * </ul> | |
100 | * | |
101 | * @param initialCapacity The map initial capacity | |
102 | * @param loadFactor The map load factor | |
103 | * @param concurrencyLevel The concurrency level. | |
104 | */ | |
105 | public SoftCache(int initialCapacity, float loadFactor, int concurrencyLevel) { | |
106 | 3 | super(initialCapacity, loadFactor, concurrencyLevel); |
107 | 3 | } |
108 | ||
109 | /** | |
110 | * {@inheritDoc} | |
111 | */ | |
112 | @Override protected final KeyedReference<K, V> createRef(K key, V value, | |
113 | ReferenceQueue<V> queue) { | |
114 | 14 | return new SoftKeyedReference<K, V>(key, value, queue); |
115 | } | |
116 | } |