001: package com.completex.objective.components.ocache.impl;
002:
003: import com.completex.objective.components.ocache.*;
004:
005: /**
006: * @author Gennady Krizhevsky
007: */
008: public class MultiIndexCacheImpl extends BasicCacheImpl implements
009: OdalMultiIndexCache {
010:
011: private OdalMultiIndexCache masterCache;
012:
013: public MultiIndexCacheImpl(OdalCache coreCache,
014: OdalKeyFactory odalKeyFactory,
015: ValueTransformer valueTransformer,
016: boolean markCacheCollectionElements) {
017: super (coreCache, odalKeyFactory, valueTransformer,
018: markCacheCollectionElements);
019: }
020:
021: /**
022: * @see com.completex.objective.components.ocache.OdalCacheListener#beforeGet(Object)
023: */
024: public void beforeGet(Object key) {
025: }
026:
027: /**
028: * @see com.completex.objective.components.ocache.OdalCacheListener#afterGet(Object, Object)
029: */
030: public void afterGet(Object key, Object value) {
031: }
032:
033: /**
034: * @see com.completex.objective.components.ocache.OdalCacheListener#beforePut(Object, Object)
035: */
036: public void beforePut(Object key, Object value) {
037: }
038:
039: /**
040: * @see com.completex.objective.components.ocache.OdalCacheListener#afterPut(Object, Object)
041: */
042: public void afterPut(Object key, Object value) {
043: putNotNotify(getKeyFactory().key(value), value);
044: }
045:
046: /**
047: * @see com.completex.objective.components.ocache.OdalCacheListener#beforeRemove(Object)
048: */
049: public void beforeRemove(Object key) {
050: }
051:
052: /**
053: * @see com.completex.objective.components.ocache.OdalCacheListener#afterRemove(Object, Object)
054: */
055: public void afterRemove(Object key, Object value) {
056: removeNotNotify(getKeyFactory().key(value));
057: }
058:
059: protected void listenersAfterRemove(Object key, Object value) {
060: Object masterValue;
061: if (isSlave()) {
062: masterValue = masterCache.getNotNotify(value);
063: } else {
064: masterValue = value;
065: }
066:
067: for (int i = 0; i < getListeners().size(); i++) {
068: OdalCacheListener listener = (OdalCacheListener) getListeners()
069: .get(i);
070: oneListenerAfterRemove(listener, key, masterValue);
071: }
072: }
073:
074: /**
075: * @see com.completex.objective.components.ocache.OdalCacheListener#beforeClear()
076: */
077: public void beforeClear() {
078: }
079:
080: /**
081: * @see com.completex.objective.components.ocache.OdalCacheListener#afterClear()
082: */
083: public void afterClear() {
084: clearNotNotify();
085: }
086:
087: protected Object transformAfterGet(
088: ValueTransformer valueTransformer, Object key, Object value) {
089: if (value == null) {
090: return null;
091: }
092: Object realKey = key;
093: if (isSlave()) {
094: realKey = value;
095: value = masterCache.get(realKey);
096: if (value == null) {
097: removeNotNotify(key);
098: }
099: }
100: return super
101: .transformAfterGet(valueTransformer, realKey, value);
102: }
103:
104: public boolean isSlave() {
105: return !isMaster();
106: }
107:
108: protected Object transformBeforePut(
109: ValueTransformer valueTransformer, Object key, Object value) {
110: Object realValue = value;
111: if (isSlave()) {
112: OdalKeyFactory masterOdalKeyFactory = masterCache
113: .getKeyFactory();
114: realValue = masterOdalKeyFactory.key(value);
115: }
116:
117: return super .transformBeforePut(valueTransformer, key,
118: realValue);
119: }
120:
121: /**
122: * @see com.completex.objective.components.ocache.OdalMultiIndexCache#getMasterCache()
123: */
124: public OdalKeyedCache getMasterCache() {
125: return masterCache;
126: }
127:
128: /**
129: * @see OdalMultiIndexCache#setMasterCache(com.completex.objective.components.ocache.OdalMultiIndexCache)
130: */
131: public void setMasterCache(OdalMultiIndexCache masterCache) {
132: this .masterCache = masterCache;
133: }
134:
135: /**
136: * @see com.completex.objective.components.ocache.OdalMultiIndexCache#isMaster()
137: */
138: public boolean isMaster() {
139: return this .masterCache == null;
140: }
141:
142: public void setKeyFactory(OdalKeyFactory keyFactory) {
143: if (keyFactory == null) {
144: throw new OdalRuntimeCacheException(
145: "Key factory is null. It has to be explicitely set for Multi Index Cache");
146: }
147: super.setKeyFactory(keyFactory);
148: }
149:
150: }
|