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

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
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 * Simple cache. Uses map internally.
37 * @author Pavel Vlasov
38 * @version $Revision: 1.3 $
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 // Nothing
158 }
159
160 public void setOwner(Object owner) {
161 // Nothing
162 }
163}
164