001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.ocache.impl;
010:
011: import com.completex.objective.components.ocache.*;
012:
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: /**
017: * OdalCache decorator
018: *
019: * @author Gennady Krizhevsky
020: */
021: public class BasicCacheImpl implements OdalNotifyingCache,
022: OdalKeyedCache, OdalCacheWrapper {
023:
024: private OdalCache coreCache;
025: private OdalKeyFactory keyFactory = OdalKeyFactory.NULL_ODAL_KEY_FACTORY;
026: private ValueTransformer valueTransformer = ValueTransformer.NULL_VALUE_TRANSFORMER;
027: private boolean markCacheCollectionElements = true;
028:
029: private ArrayList listeners = new ArrayList();
030:
031: protected BasicCacheImpl() {
032: }
033:
034: public BasicCacheImpl(OdalCache coreCache,
035: OdalKeyFactory odalKeyFactory,
036: ValueTransformer valueTransformer,
037: boolean markCacheCollectionElements) {
038: this .coreCache = coreCache;
039: setKeyFactory(odalKeyFactory);
040: setValueTransformer(valueTransformer);
041: setMarkCacheCollectionElements(markCacheCollectionElements);
042: }
043:
044: public OdalKeyFactory getKeyFactory() {
045: return keyFactory;
046: }
047:
048: public void setKeyFactory(OdalKeyFactory keyFactory) {
049: if (keyFactory != null) {
050: this .keyFactory = keyFactory;
051: }
052: }
053:
054: public boolean isMarkCacheCollectionElements() {
055: return markCacheCollectionElements;
056: }
057:
058: public void setMarkCacheCollectionElements(
059: boolean markCacheCollectionElements) {
060: this .markCacheCollectionElements = markCacheCollectionElements;
061: }
062:
063: /**
064: * @see OdalCache#get
065: */
066: public Object get(Object key) {
067: listenersBeforeGet(key);
068: Object value = getNotNotify(key);
069: listenersAfterGet(key, value);
070: return transformAfterGet(this .valueTransformer, key, value);
071: }
072:
073: protected Object transformAfterGet(
074: ValueTransformer valueTransformer, Object key, Object value) {
075: return valueTransformer.afterGet(key, value);
076: }
077:
078: protected Object transformBeforePut(
079: ValueTransformer valueTransformer, Object key, Object value) {
080: return valueTransformer.beforePut(key, value);
081: }
082:
083: protected Object resolveKey(Object key) {
084: Object transformedKey = keyFactory.key(key);
085: return transformedKey == null ? key : transformedKey;
086: }
087:
088: public Object getNotNotify(Object key) {
089: return doGet(key);
090: }
091:
092: /**
093: * Core functionality on get object to cache - can be overriden in descendants
094: *
095: * @param key
096: * @return cached value
097: */
098: protected Object doGet(Object key) {
099: return coreCache.get(key);
100: }
101:
102: /**
103: * @see com.completex.objective.components.ocache.OdalLinkedCache#put(Object)
104: */
105: public void put(Object value) {
106: if (keyFactory == null) {
107: throw new OdalRuntimeCacheException(
108: "OdalKeyFactory is null for OdalKeyedCache");
109: }
110:
111: put(keyFactory.key(value), value);
112: }
113:
114: /**
115: * @see OdalCache#put(Object, Object)
116: */
117: public Object put(Object key, Object value) {
118: listenersBeforePut(key, value);
119: Object retValue = putNotNotify(key, value);
120: listenersAfterPut(key, value);
121: return retValue;
122: }
123:
124: public Object putNotNotify(Object key, Object value) {
125: value = transformBeforePut(this .valueTransformer, key, value);
126: return doPut(key, value);
127: }
128:
129: /**
130: * Core functionality on put object to cache - can be overriden in descendants
131: *
132: * @param key
133: * @param value
134: * @return old cache value
135: */
136: protected Object doPut(Object key, Object value) {
137: if (value != null) {
138: return coreCache.put(key, value);
139: } else {
140: return null;
141: }
142: }
143:
144: /**
145: * @see OdalCache#remove
146: */
147: public Object remove(Object key) {
148: if (key != null) {
149: listenersBeforeRemove(key);
150: Object retValue = removeNotNotify(key);
151: listenersAfterRemove(key, retValue);
152: return retValue;
153: } else {
154: return null;
155: }
156: }
157:
158: protected Object doRemove(Object key) {
159: if (key != null) {
160: return coreCache.remove(key);
161: } else {
162: return null;
163: }
164: }
165:
166: public Object removeNotNotify(Object key) {
167: return doRemove(key);
168: }
169:
170: /**
171: * Removes all the entries from the cache
172: */
173: public void clear() {
174: listenersBeforeClear();
175: clearNotNotify();
176: listenersAfterClear();
177: }
178:
179: public void doClear() {
180: coreCache.clear();
181: }
182:
183: public void clearNotNotify() {
184: doClear();
185: }
186:
187: /**
188: * @see OdalCache#getName
189: */
190: public String getName() {
191: return coreCache == null ? null : coreCache.getName();
192: }
193:
194: /**
195: * @return supporting cache
196: */
197: public OdalCache getCoreCache() {
198: return coreCache;
199: }
200:
201: public void addListener(OdalCacheListener listener) {
202: listeners.add(listener);
203: }
204:
205: public void removeListener(OdalCacheListener listener) {
206: listeners.remove(listener);
207: }
208:
209: public void removeAllListeners() {
210: listeners.clear();
211: }
212:
213: private void listenersBeforeGet(Object key) {
214: for (int i = 0; i < listeners.size(); i++) {
215: OdalCacheListener listener = (OdalCacheListener) listeners
216: .get(i);
217: oneListenerBeforeGet(listener, key);
218: }
219: }
220:
221: protected void oneListenerBeforeGet(OdalCacheListener listener,
222: Object key) {
223: listener.beforeGet(key);
224: }
225:
226: private void listenersAfterGet(Object key, Object value) {
227: for (int i = 0; i < listeners.size(); i++) {
228: OdalCacheListener listener = (OdalCacheListener) listeners
229: .get(i);
230: oneListenerAfterGet(listener, key, value);
231: }
232: }
233:
234: protected void oneListenerAfterGet(OdalCacheListener listener,
235: Object key, Object value) {
236: listener.afterGet(key, value);
237: }
238:
239: private void listenersBeforePut(Object key, Object value) {
240: for (int i = 0; i < listeners.size(); i++) {
241: OdalCacheListener listener = (OdalCacheListener) listeners
242: .get(i);
243: oneListenerBeforePut(listener, key, value);
244: }
245: }
246:
247: protected void oneListenerBeforePut(OdalCacheListener listener,
248: Object key, Object value) {
249: listener.beforePut(key, value);
250: }
251:
252: private void listenersAfterPut(Object key, Object value) {
253: for (int i = 0; i < listeners.size(); i++) {
254: OdalCacheListener listener = (OdalCacheListener) listeners
255: .get(i);
256: oneListenerAfterPut(listener, key, value);
257: }
258: }
259:
260: protected void oneListenerAfterPut(OdalCacheListener listener,
261: Object key, Object value) {
262: listener.afterPut(key, value);
263: }
264:
265: private void listenersBeforeRemove(Object key) {
266: for (int i = 0; i < listeners.size(); i++) {
267: OdalCacheListener listener = (OdalCacheListener) listeners
268: .get(i);
269: oneListenerBeforeRemove(listener, key);
270: }
271: }
272:
273: protected void oneListenerBeforeRemove(OdalCacheListener listener,
274: Object key) {
275: listener.beforeRemove(key);
276: }
277:
278: protected void listenersAfterRemove(Object key, Object value) {
279: for (int i = 0; i < listeners.size(); i++) {
280: OdalCacheListener listener = (OdalCacheListener) listeners
281: .get(i);
282: oneListenerAfterRemove(listener, key, value);
283: }
284: }
285:
286: protected void oneListenerAfterRemove(OdalCacheListener listener,
287: Object key, Object value) {
288: listener.afterRemove(key, value);
289: }
290:
291: // Clear:
292:
293: private void listenersBeforeClear() {
294: for (int i = 0; i < listeners.size(); i++) {
295: OdalCacheListener listener = (OdalCacheListener) listeners
296: .get(i);
297: oneListenerBeforeClear(listener);
298: }
299: }
300:
301: protected void oneListenerBeforeClear(OdalCacheListener listener) {
302: listener.beforeClear();
303: }
304:
305: private void listenersAfterClear() {
306: for (int i = 0; i < listeners.size(); i++) {
307: OdalCacheListener listener = (OdalCacheListener) listeners
308: .get(i);
309: oneListenerAfterClear(listener);
310: }
311: }
312:
313: protected void oneListenerAfterClear(OdalCacheListener listener) {
314: listener.afterClear();
315: }
316:
317: public ValueTransformer getValueTransformer() {
318: return valueTransformer;
319: }
320:
321: public void setValueTransformer(ValueTransformer valueTransformer) {
322: if (valueTransformer != null) {
323: this .valueTransformer = valueTransformer;
324: }
325: }
326:
327: public List getListeners() {
328: return listeners;
329: }
330:
331: }
|