SimpleMemoryCache.java
biz/hammurapi/cache/SimpleMemoryCache.java
Violations
Inspector |
Message |
Severity |
Location |
Java Inspector 048 |
Copyrights information should be present in each file. |
1 |
|
Java Inspector 089 |
Constructor is not properly documented |
2 |
47:9
|
Java Inspector 089 |
Undocumented parameter producer |
2 |
47:9
|
Java Inspector 089 |
Undocumented method |
2 |
57:9
|
Java Inspector 089 |
Undocumented method |
2 |
61:33
|
Java Inspector 089 |
Undocumented method |
2 |
65:33
|
Java Inspector 089 |
Undocumented method |
2 |
69:33
|
Java Inspector 089 |
Undocumented method |
2 |
77:9
|
Java Inspector 089 |
Undocumented method |
2 |
106:9
|
Java Inspector 089 |
Undocumented method |
2 |
112:9
|
Java Inspector 089 |
Undocumented method |
2 |
119:9
|
Java Inspector 089 |
Undocumented method |
2 |
132:9
|
Java Inspector 089 |
Undocumented method |
2 |
136:9
|
Java Inspector 089 |
Undocumented method |
2 |
152:9
|
Java Inspector 089 |
Undocumented method |
2 |
156:9
|
Java Inspector 089 |
Undocumented method |
2 |
160:9
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package biz.hammurapi.cache;
24
25import java.util.HashMap;
26import java.util.HashSet;
27import java.util.Iterator;
28import java.util.Map;
29import java.util.Set;
30
31import biz.hammurapi.config.ConfigurationException;
32import biz.hammurapi.util.Acceptor;
33
34
35
36
37
38
39
40public class SimpleMemoryCache extends AbstractProducer implements Cache {
41 private Map cache=new HashMap();
42 private Producer producer;
43
44
45
46
47 public SimpleMemoryCache(Producer producer) {
48 super();
49
50 this.producer=producer;
51 if (producer!=null) {
52 producer.addCache(this);
53 }
54
55 }
56
57 public void put(Object key, final Object value, final long time, final long expirationTime) {
58 synchronized (cache) {
59 cache.put(key, new Entry() {
60
61 public long getExpirationTime() {
62 return expirationTime;
63 }
64
65 public long getTime() {
66 return time;
67 }
68
69 public Object get() {
70 return value;
71 }
72
73 });
74 }
75 }
76
77 public Entry get(Object key) {
78
79 synchronized (cache) {
80 Entry entry=(Entry) cache.get(key);
81 long now = System.currentTimeMillis();
82 if (entry!=null) {
83 if (entry.getExpirationTime()>0 && entry.getExpirationTime()<now) {
84 cache.remove(key);
85 } else {
86 return entry;
87 }
88 }
89
90 if (key instanceof ProducingKey) {
91 entry = ((ProducingKey) key).get();
92 cache.put(key, entry);
93 return entry;
94 }
95
96 if (producer!=null) {
97 entry = producer.get(key);
98 cache.put(key, entry);
99 return entry;
100 }
101
102 return null;
103 }
104 }
105
106 public void clear() {
107 synchronized (cache) {
108 cache.clear();
109 }
110 }
111
112 public void remove(Object key) {
113 synchronized (cache) {
114 cache.remove(key);
115 }
116 onRemove(key);
117 }
118
119 public void remove(Acceptor acceptor) {
120 synchronized (cache) {
121 Iterator it=cache.keySet().iterator();
122 while (it.hasNext()) {
123 Object key=it.next();
124 if (acceptor.accept(key)) {
125 it.remove();
126 onRemove(key);
127 }
128 }
129 }
130 }
131
132 public void stop() {
133 cache.clear();
134 }
135
136 public Set keySet() {
137 HashSet ret = new HashSet();
138 synchronized (cache) {
139 ret.addAll(cache.keySet());
140 }
141
142 if (producer!=null) {
143 Set pkeys=producer.keySet();
144 if (pkeys!=null) {
145 ret.addAll(pkeys);
146 }
147 }
148
149 return ret;
150 }
151
152 public boolean isActive() {
153 return true;
154 }
155
156 public void start() throws ConfigurationException {
157
158 }
159
160 public void setOwner(Object owner) {
161
162 }
163}
164