001: package org.jacorb.notification.util;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1999-2004 Gerald Brose
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: /**
025: * Add Caching to WildcardMap. If the Keys inside the Map contain the Wildcard Operator '*' the
026: * Operation getWithExpansion is rather timeconsuming. For each Key that contains a '*' a pattern
027: * match must be done. This Decorator adds simple Caching. When a key is looked up the retrieved
028: * value is stored in an internal cache with fixed size. Subsequent calls to getWithExpansion query
029: * the cache first. As soon as a put or remove Operation occurs the Cache is invalidated.
030: *
031: * @author Alphonse Bendt
032: * @version $Id: CachingWildcardMap.java,v 1.5 2005/02/14 00:13:05 alphonse.bendt Exp $
033: */
034:
035: public class CachingWildcardMap implements WildcardMap {
036: private final Object[] cachedKeys_;
037:
038: private final Object[][] cachedValues_;
039:
040: private Object victimKey_;
041:
042: private Object[] victimValue_;
043:
044: private final int cacheSize_;
045:
046: private final WildcardMap delegate_;
047:
048: public CachingWildcardMap() {
049: this (4, new DefaultWildcardMap());
050: }
051:
052: public CachingWildcardMap(int cacheSize, WildcardMap delegate) {
053: super ();
054:
055: cachedValues_ = new Object[cacheSize][];
056: cachedKeys_ = new Object[cacheSize];
057: cacheSize_ = cacheSize;
058: delegate_ = delegate;
059: }
060:
061: private int calcPosition(String key) {
062: return key.charAt(0) % cacheSize_;
063: }
064:
065: private void invalidateCache() {
066: for (int x = 0; x < cacheSize_; ++x) {
067: cachedKeys_[x] = null;
068: }
069: victimKey_ = null;
070: }
071:
072: public void clear() {
073: invalidateCache();
074:
075: delegate_.clear();
076: }
077:
078: public Object remove(Object key) {
079: invalidateCache();
080:
081: return delegate_.remove(key);
082: }
083:
084: public Object put(Object key, Object value) {
085: invalidateCache();
086:
087: return delegate_.put(key, value);
088: }
089:
090: public Object[] getWithExpansion(Object key) {
091: String _key = key.toString();
092: int _pos = calcPosition(_key);
093: Object[] _ret;
094:
095: if (_key.equals(cachedKeys_[_pos])) {
096: _ret = cachedValues_[_pos];
097: } else if (_key.equals(victimKey_)) {
098: _ret = victimValue_;
099: } else {
100: _ret = delegate_.getWithExpansion(key);
101:
102: // store old cache entry in victim cache
103: victimKey_ = cachedKeys_[_pos];
104: victimValue_ = cachedValues_[_pos];
105:
106: // update cache entry
107: cachedKeys_[_pos] = _key;
108: cachedValues_[_pos] = _ret;
109: }
110:
111: return _ret;
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * @see org.jacorb.notification.util.WildcardMap#getNoExpansion(java.lang.Object)
118: */
119: public Object getNoExpansion(Object key) {
120: return delegate_.getNoExpansion(key);
121: }
122: }
|