All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.lang.ThreadLocalCreateOnDemandObjectContainer Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.lang;

/**
 * An implementation of {@link ObjectContainer} that creates an own instance per
 * thread when it is requested for the first time using an underlying
 * {@link Factory} which is commonly known as "lazy loading" pattern.
 * 
 * @param 
 *           the type of object the container can carries
 */
public class ThreadLocalCreateOnDemandObjectContainer extends AbstractCreateOnDemandObjectContainer
{
   private final ThreadLocal _instances = new ThreadLocal();

   /**
    * Creates a new instance for the given {@link Factory}
    * 
    * @param factory
    *           the {@link Factory} to use for creating the instances
    */
   public ThreadLocalCreateOnDemandObjectContainer(final Factory factory)
   {
      super(factory);
   }

   @Override
   protected E getInstance()
   {
      return _instances.get();
   }

   @Override
   protected void setInstance(final E instance)
   {
      _instances.set(instance);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy