1 26 27 package net.sourceforge.groboutils.util.datastruct.v1; 28 29 30 50 public class ObjectCache 51 { 52 55 public static final int UNLIMITED_SIZE = -1; 56 57 58 62 public static interface ObjectCreator 63 { 64 69 public Object createObject(); 70 } 71 72 73 77 public static class DefaultObjectCreator implements ObjectCreator 78 { 79 private Class clazz; 80 81 86 public DefaultObjectCreator( Class clazz ) 87 { 88 if (clazz == null) 89 { 90 throw new IllegalArgumentException ( "No null args" ); 91 } 92 this.clazz = clazz; 93 } 94 95 public Object createObject() 96 { 97 try 98 { 99 return clazz.newInstance(); 100 } 101 catch (Exception e) 102 { 103 return null; 104 } 105 } 106 } 107 108 109 110 113 private SynchQueue cache = new SynchQueue(); 114 115 118 private ObjectCreator creator; 119 120 123 private int maxSize = UNLIMITED_SIZE; 124 125 126 129 private int overflowCount = 0; 130 131 132 135 private int underflowCount = 0; 136 137 138 141 142 145 public ObjectCache() 146 { 147 } 149 150 151 155 public ObjectCache( int maxSize ) 156 { 157 setMaxSize( maxSize ); 158 } 159 160 161 165 public ObjectCache( ObjectCreator creator ) 166 { 167 setObjectCreator( creator ); 168 } 169 170 171 175 public ObjectCache( Class creator ) 176 { 177 setClassCreator( creator ); 178 } 179 180 181 186 public ObjectCache( ObjectCreator creator, int maxSize ) 187 { 188 setMaxSize( maxSize ); 189 setObjectCreator( creator ); 190 } 191 192 193 199 public ObjectCache( Class creator, int maxSize ) 200 { 201 setMaxSize( maxSize ); 202 setClassCreator( creator ); 203 } 204 205 206 215 public ObjectCache( ObjectCreator creator, int maxSize, boolean fill ) 216 { 217 setMaxSize( maxSize ); 218 setObjectCreator( creator ); 219 if (fill) 220 { 221 fillCache(); 222 } 223 } 224 225 226 236 public ObjectCache( Class creator, int maxSize, boolean fill ) 237 { 238 setMaxSize( maxSize ); 239 setClassCreator( creator ); 240 if (fill) 241 { 242 fillCache(); 243 } 244 } 245 246 247 248 249 252 253 261 public void putBack( Object o ) 262 { 263 if (o == null) 264 { 265 throw new IllegalArgumentException ( 267 "Null objects cannot be added into the cache" ); 268 } 269 if (this.maxSize > 0) 270 { 271 if (this.cache.size() >= this.maxSize) 272 { 273 this.overflowCount++; 276 return; 277 } 278 } 279 this.cache.enqueue( o ); 280 } 281 282 293 public Object get() 294 { 295 synchronized( this ) 297 { 298 if (!this.cache.isEmpty()) 299 { 300 try 301 { 302 return this.cache.dequeue(); 304 } 305 catch (InterruptedException ie) 306 { 307 throw new IllegalStateException ("encountered an interrupt: "+ie); 309 } 310 } 311 } 312 this.underflowCount++; 317 return createObject(); 318 } 319 320 321 338 public Object get( long millisWaitTime ) 339 throws InterruptedException 340 { 341 synchronized( this ) 343 { 344 if (!this.cache.isEmpty()) 346 { 347 try 348 { 349 return this.cache.dequeue(); 351 } 352 catch (InterruptedException ie) 353 { 354 throw new IllegalStateException ("encountered an interrupt: "+ie); 356 } 357 } 358 } 359 this.underflowCount++; 364 365 return this.cache.dequeue( millisWaitTime ); 366 } 367 368 369 373 public int getOverflows() 374 { 375 return this.overflowCount; 376 } 377 378 379 383 public int getUnderflows() 384 { 385 return this.underflowCount; 386 } 387 388 389 395 public void setMaxSize( int size ) 396 { 397 this.maxSize = size; 398 } 399 400 401 404 public int getMaxSize() 405 { 406 return this.maxSize; 407 } 408 409 410 413 public void setObjectCreator( ObjectCreator creator ) 414 { 415 this.creator = creator; 416 } 417 418 419 422 public void setClassCreator( Class creator ) 423 { 424 ObjectCreator oc = null; 425 if (creator != null) 426 { 427 oc = new DefaultObjectCreator( creator ); 428 } 429 setObjectCreator( oc ); 430 } 431 432 433 437 public void addObject() 438 { 439 Object o = createObject(); 440 if (o != null) 441 { 442 putBack( o ); 443 } 444 } 445 446 447 451 public void fillCache() 452 { 453 if (this.creator != null) 454 { 455 for (int i = this.cache.size(); i < this.maxSize; i++) 458 { 459 addObject(); 460 } 461 } 462 } 463 464 467 468 471 protected Object createObject() 472 { 473 Object o = null; 474 if (this.creator != null) 475 { 476 o = this.creator.createObject(); 478 } 479 return o; 480 } 481 } 482 483 | Popular Tags |