1 package org.jacorb.notification.util; 2 3 23 24 34 35 public class CachingWildcardMap implements WildcardMap 36 { 37 private final Object [] cachedKeys_; 38 39 private final Object [][] cachedValues_; 40 41 private Object victimKey_; 42 43 private Object [] victimValue_; 44 45 private final int cacheSize_; 46 47 private final WildcardMap delegate_; 48 49 public CachingWildcardMap() 50 { 51 this(4, new DefaultWildcardMap()); 52 } 53 54 public CachingWildcardMap(int cacheSize, WildcardMap delegate) 55 { 56 super(); 57 58 cachedValues_ = new Object [cacheSize][]; 59 cachedKeys_ = new Object [cacheSize]; 60 cacheSize_ = cacheSize; 61 delegate_ = delegate; 62 } 63 64 private int calcPosition(String key) 65 { 66 return key.charAt(0) % cacheSize_; 67 } 68 69 private void invalidateCache() 70 { 71 for (int x = 0; x < cacheSize_; ++x) 72 { 73 cachedKeys_[x] = null; 74 } 75 victimKey_ = null; 76 } 77 78 public void clear() 79 { 80 invalidateCache(); 81 82 delegate_.clear(); 83 } 84 85 public Object remove(Object key) 86 { 87 invalidateCache(); 88 89 return delegate_.remove(key); 90 } 91 92 public Object put(Object key, Object value) 93 { 94 invalidateCache(); 95 96 return delegate_.put(key, value); 97 } 98 99 public Object [] getWithExpansion(Object key) 100 { 101 String _key = key.toString(); 102 int _pos = calcPosition(_key); 103 Object [] _ret; 104 105 if (_key.equals(cachedKeys_[_pos])) 106 { 107 _ret = cachedValues_[_pos]; 108 } 109 else if (_key.equals(victimKey_)) 110 { 111 _ret = victimValue_; 112 } 113 else 114 { 115 _ret = delegate_.getWithExpansion(key); 116 117 victimKey_ = cachedKeys_[_pos]; 119 victimValue_ = cachedValues_[_pos]; 120 121 cachedKeys_[_pos] = _key; 123 cachedValues_[_pos] = _ret; 124 } 125 126 return _ret; 127 } 128 129 134 public Object getNoExpansion(Object key) 135 { 136 return delegate_.getNoExpansion(key); 137 } 138 } | Popular Tags |