1 package org.apache.oro.util; 2 3 59 60 import java.util.*; 61 62 74 public final class CacheRandom extends GenericCache { 75 private Random __random; 76 77 82 public CacheRandom(int capacity) { 83 super(capacity); 84 __random = new Random(System.currentTimeMillis()); 85 } 86 87 93 public CacheRandom(){ 94 this(GenericCache.DEFAULT_CAPACITY); 95 } 96 97 105 public final synchronized void addElement(Object key, Object value) { 106 107 int index; 108 Object obj; 109 110 obj = _table.get(key); 111 112 if(obj != null) { 113 GenericCacheEntry entry; 114 115 entry = (GenericCacheEntry)obj; 117 entry._value = value; 118 entry._key = key; 119 120 return; 121 } 122 123 125 if(!isFull()) { 127 index = _numEntries; 128 ++_numEntries; 129 } else { 130 index = (int)(_cache.length*__random.nextFloat()); 132 _table.remove(_cache[index]._key); 133 } 134 135 _cache[index]._value = value; 136 _cache[index]._key = key; 137 _table.put(key, _cache[index]); 138 } 139 } 140 | Popular Tags |