@@ -0,0 +1,2852 @@
+/* **********************************************************************%0A    Copyright 2006 Rensselaer Polytechnic Institute. All worldwide rights reserved.%0A%0A    Redistribution and use of this distribution in source and binary forms,%0A    with or without modification, are permitted provided that:%0A       The above copyright notice and this permission notice appear in all%0A        copies and supporting documentation;%0A%0A        The name, identifiers, and trademarks of Rensselaer Polytechnic%0A        Institute are not used in advertising or publicity without the%0A        express prior written permission of Rensselaer Polytechnic Institute;%0A%0A    DISCLAIMER: The software is distributed%22 AS IS%22 without any express or%0A    implied warranty, including but not limited to, any implied warranties%0A    of merchantability or fitness for a particular purpose or any warrant)'%0A    of non-infringement of any current or pending patent rights. The authors%0A    of the software make no representations about the suitability of this%0A    software for any particular purpose. The entire risk as to the quality%0A    and performance of the software is with the user. Should the software%0A    prove defective, the user assumes the cost of all necessary servicing,%0A    repair or correction. In particular, neither Rensselaer Polytechnic%0A    Institute, nor the authors of the software are liable for any indirect,%0A    special, consequential, or incidental damages related to the software,%0A    to the maximum extent the law permits.%0A*/%0A%0Apackage edu.rpi.sss.util;%0A%0Aimport org.apache.log4j.Logger;%0A%0Aimport java.io.Serializable;%0Aimport java.lang.ref.SoftReference;%0Aimport java.util.WeakHashMap;%0A%0A/** Implement a pool of objects whose values appear frequently enough that pooling%0A * will result in a substantial reduction in space and or cpu.%0A *%0A * %3Cp%3EObjects pooled must be either immutable or sharable because they will be%0A * shared.%0A *%0A * @author Mike Douglass%0A *%0A * @param %3CT%3E%0A */%0Apublic class ObjectPool%3CT%3E implements Serializable %7B%0A  private WeakHashMap%3CT, SoftReference%3CT%3E%3E pool =%0A    new WeakHashMap%3CT, SoftReference%3CT%3E%3E();%0A%0A  private static long refs;%0A  private static long hits;%0A%0A  private transient Logger log;%0A  private boolean debug = false;%0A%0A  /**%0A   * @param val%0A   * @return value in pool%0A   */%0A  public T get(T val) %7B%0A    if (debug &&%0A        ((refs %25 500) == 0)) %7B%0A      getLogger().debug(%22pool refs %22 + refs + %22: hits %22 + hits);%0A    %7D%0A    refs++;%0A    SoftReference%3CT%3E poolVal = pool.get(val);%0A%0A    if (poolVal != null) %7B%0A      T tval = poolVal.get();%0A      if (tval != null) %7B%0A        hits++;%0A        return tval;%0A      %7D%0A    %7D%0A%0A    synchronized (pool) %7B%0A      pool.put(val, new SoftReference%3CT%3E(val));%0A      return val;%0A    %7D%0A  %7D%0A%0A  private Logger getLogger() %7B%0A    if (log == null) %7B%0A      log = Logger.getLogger(this.getClass());%0A    %7D%0A%0A    return log;%0A  %7D%0A%7D%0A

Add an object pool to cache immutable or shared objects.