%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
ca.spaz.util.CacheMap |
|
|
1 | /* |
|
2 | ******************************************************************************* |
|
3 | * Copyright (c) 2005 Chris Rose and AIMedia |
|
4 | * All rights reserved. CacheMap and the accompanying materials |
|
5 | * are made available under the terms of the Common Public License v1.0 |
|
6 | * which accompanies this distribution, and is available at |
|
7 | * http://www.eclipse.org/legal/cpl-v10.html |
|
8 | * |
|
9 | * Contributors: |
|
10 | * Chris Rose |
|
11 | *******************************************************************************/ |
|
12 | package ca.spaz.util; |
|
13 | ||
14 | import java.util.*; |
|
15 | ||
16 | /** |
|
17 | * A caching map implementation that provides a maximum number of stored objects. |
|
18 | * @author Chris Rose |
|
19 | */ |
|
20 | 13 | public class CacheMap implements Map { |
21 | ||
22 | private Map backingMap; |
|
23 | private Queue orderStack; |
|
24 | private int maxCacheSize; |
|
25 | ||
26 | 10 | public CacheMap(int size) { |
27 | 10 | this.backingMap = new HashMap(); |
28 | 10 | this.orderStack = new LinkedList(); |
29 | 10 | this.maxCacheSize = size; |
30 | 10 | } |
31 | ||
32 | /* (non-Javadoc) |
|
33 | * @see java.util.Map#size() |
|
34 | */ |
|
35 | public int size() { |
|
36 | 0 | return backingMap.size(); |
37 | } |
|
38 | ||
39 | /* (non-Javadoc) |
|
40 | * @see java.util.Map#isEmpty() |
|
41 | */ |
|
42 | public boolean isEmpty() { |
|
43 | 0 | return backingMap.isEmpty(); |
44 | } |
|
45 | ||
46 | /* (non-Javadoc) |
|
47 | * @see java.util.Map#containsKey(java.lang.Object) |
|
48 | */ |
|
49 | public boolean containsKey(Object key) { |
|
50 | 0 | return backingMap.containsKey(key); |
51 | } |
|
52 | ||
53 | /* (non-Javadoc) |
|
54 | * @see java.util.Map#containsValue(java.lang.Object) |
|
55 | */ |
|
56 | public boolean containsValue(Object value) { |
|
57 | 0 | return backingMap.containsValue(value); |
58 | } |
|
59 | ||
60 | /* (non-Javadoc) |
|
61 | * @see java.util.Map#get(java.lang.Object) |
|
62 | */ |
|
63 | public Object get(Object key) { |
|
64 | 100 | assert(orderStack.size() == backingMap.size()); |
65 | //TODO caching behaviour, if necessary |
|
66 | 100 | return backingMap.get(key); |
67 | } |
|
68 | ||
69 | /* (non-Javadoc) |
|
70 | * @see java.util.Map#put(java.lang.Object,java.lang.Object) |
|
71 | */ |
|
72 | public Object put(Object arg0, Object arg1) { |
|
73 | 100 | assert(orderStack.size() == backingMap.size()); |
74 | 100 | if (!orderStack.contains(arg0)) { |
75 | 100 | if (orderStack.size() >= maxCacheSize) { |
76 | 80 | prepQueue(); |
77 | } |
|
78 | 100 | orderStack.offer(arg0); |
79 | 100 | backingMap.put(arg0, arg1); |
80 | 30 | } else { |
81 | 0 | assert(backingMap.containsKey(arg0)); |
82 | } |
|
83 | 100 | assert(orderStack.size() == backingMap.size()); |
84 | 100 | assert(backingMap.size() <= maxCacheSize); |
85 | 100 | return null; |
86 | } |
|
87 | ||
88 | private void prepQueue() { |
|
89 | 216 | while (orderStack.size() >= maxCacheSize) { |
90 | 80 | Object key = orderStack.remove(); |
91 | 80 | Object res = backingMap.remove(key); |
92 | 80 | assert (res != null); |
93 | 24 | } |
94 | 80 | } |
95 | ||
96 | /* (non-Javadoc) |
|
97 | * @see java.util.Map#remove(java.lang.Object) |
|
98 | */ |
|
99 | public Object remove(Object key) { |
|
100 | 0 | assert(orderStack.size() == backingMap.size()); |
101 | 0 | orderStack.remove(key); |
102 | 0 | Object ret = backingMap.remove(key); |
103 | 0 | assert(orderStack.size() == backingMap.size()); |
104 | 0 | return ret; |
105 | } |
|
106 | ||
107 | /* (non-Javadoc) |
|
108 | * @see java.util.Map#putAll(java.util.Map) |
|
109 | */ |
|
110 | public void putAll(Map arg0) { |
|
111 | 0 | assert(orderStack.size() == backingMap.size()); |
112 | 0 | for (Iterator iter = arg0.entrySet().iterator(); iter.hasNext();) { |
113 | 0 | Map.Entry entry = (Map.Entry) iter.next(); |
114 | 0 | put(entry.getKey(), entry.getValue()); |
115 | 0 | } |
116 | 0 | assert(orderStack.size() == backingMap.size()); |
117 | 0 | } |
118 | ||
119 | /* (non-Javadoc) |
|
120 | * @see java.util.Map#clear() |
|
121 | */ |
|
122 | public void clear() { |
|
123 | 0 | orderStack.clear(); |
124 | 0 | backingMap.clear(); |
125 | 0 | } |
126 | ||
127 | /* (non-Javadoc) |
|
128 | * @see java.util.Map#keySet() |
|
129 | */ |
|
130 | public Set keySet() { |
|
131 | 0 | return backingMap.keySet(); |
132 | } |
|
133 | ||
134 | /* (non-Javadoc) |
|
135 | * @see java.util.Map#values() |
|
136 | */ |
|
137 | public Collection values() { |
|
138 | 0 | return backingMap.values(); |
139 | } |
|
140 | ||
141 | /* (non-Javadoc) |
|
142 | * @see java.util.Map#entrySet() |
|
143 | */ |
|
144 | public Set entrySet() { |
|
145 | 0 | return backingMap.entrySet(); |
146 | } |
|
147 | ||
148 | public boolean equals(Object obj) { |
|
149 | 0 | return backingMap.equals(obj); |
150 | } |
|
151 | ||
152 | public int hashCode() { |
|
153 | 0 | return backingMap.hashCode(); |
154 | } |
|
155 | ||
156 | public String toString() { |
|
157 | 0 | return backingMap.toString(); |
158 | } |
|
159 | ||
160 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |