1 23 package com.sun.enterprise.admin.wsmgmt.pool.impl; 24 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import com.sun.enterprise.admin.wsmgmt.pool.spi.Pool; 32 33 39 public class BoundedPool implements Pool{ 40 41 46 public BoundedPool(String name) { 47 _name = name; 48 _keyList = Collections.synchronizedList(new ArrayList ()); 49 _map = new HashMap (); 50 } 51 52 58 public BoundedPool(String name, int size) { 59 60 this(name); 61 _maxSizeAllowed = size; 62 } 63 64 71 public Object get(Object key) { 72 return _map.get(key); 73 } 74 75 85 public Object put(Object key, Object val) { 86 87 if ((key == null) && (val == null)) { 88 throw new IllegalArgumentException (); 89 } 90 91 Object oldVal = null; 92 93 synchronized (this) { 94 oldVal = _map.put(key, val); 95 _keyList.add(key); 96 97 if (_keyList.size() > _maxSizeAllowed) { 99 Object rkey = _keyList.remove(0); 100 assert (rkey != null); 101 102 Object rval = _map.remove(rkey); 103 assert (rval != null); 104 } 105 } 106 107 return oldVal; 108 } 109 110 117 public Object remove(Object key) { 118 Object val = null; 119 120 synchronized (this) { 121 _keyList.remove(key); 122 val = _map.remove(key); 123 } 124 125 return val; 126 } 127 128 131 public void clear() { 132 synchronized (this) { 133 _map.clear(); 134 _keyList.clear(); 135 } 136 } 137 138 143 public int size() { 144 return _map.size(); 145 } 146 147 152 public int getMaxSize() { 153 return _maxSizeAllowed; 154 } 155 156 163 public boolean containsKey(Object key) { 164 return _map.containsKey(key); 165 } 166 167 174 public boolean containsValue(Object val) { 175 return _map.containsValue(val); 176 } 177 178 183 public Collection values() { 184 return _map.values(); 185 } 186 187 193 public void resize(int size) { 194 195 if (size < 1) { 196 return; 197 } 198 199 if (size == _maxSizeAllowed) { 200 } else if (size > _maxSizeAllowed) { 202 synchronized (this) { 203 _maxSizeAllowed = size; 204 } 205 } else { 206 synchronized (this) { 208 int currentSize = _map.size(); 209 int diff = currentSize - size; 210 211 for (int i=0; i<diff; i++) { 213 Object key = _keyList.get(i); 214 assert (key != null); 215 216 if (key != null) { 217 Object val = _map.remove(key); 218 assert (val != null); 219 } 220 } 221 for(int i=0; i <diff; i++) { 222 _keyList.remove(0); 223 } 224 _maxSizeAllowed = size; 225 } 226 } 227 } 228 229 234 public String getName() { 235 return _name; 236 } 237 238 private List _keyList = null; 240 private Map _map = null; 241 private String _name = null; 242 private int _maxSizeAllowed = 25; 243 } 244 | Popular Tags |