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

net.sf.javagimmicks.concurrent.locks.MultiLockProviderFactory Maven / Gradle / Ivy

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

import java.util.HashMap;
import java.util.TreeMap;

import net.sf.javagimmicks.lang.Factory;

/**
 * Serves as central {@link Factory} for {@link MultiLockProvider} instances.
 * Instances can only be retrieved by the static getters
 * {@link #getHashBasedInstance()} and {@link #getTreeBasedInstance()}.
 * 
 * @param 
 *           The type of the internally used resource identifiers
 */
public class MultiLockProviderFactory implements Factory>
{
   private static final MultiLockProviderFactory HASH_INSTANCE = new MultiLockProviderFactory(
         new HashLockRegistryFactory());

   private static final MultiLockProviderFactory TREE_INSTANCE = new MultiLockProviderFactory(
         new TreeLockRegistryFactory());

   /**
    * Returns the hash-based {@link MultiLockProviderFactory} instance which
    * manages resource identifiers using {@link HashMap}s.
    * 
    * @return the hash-bases instance
    */
   @SuppressWarnings("unchecked")
   public static  MultiLockProviderFactory getHashBasedInstance()
   {
      return (MultiLockProviderFactory) HASH_INSTANCE;
   }

   /**
    * Returns the tree-based {@link MultiLockProviderFactory} instance which
    * manages resource identifiers using {@link TreeMap}s.
    * 
    * @return the hash-bases instance
    */
   @SuppressWarnings("unchecked")
   public static  MultiLockProviderFactory getTreeBasedInstance()
   {
      return (MultiLockProviderFactory) TREE_INSTANCE;
   }

   private final Factory> _registryFactory;

   private MultiLockProviderFactory(final Factory> registryFactory)
   {
      _registryFactory = registryFactory;
   }

   @Override
   public MultiLockProvider create()
   {
      return new RegistryLockProvider(_registryFactory.create());
   }

   private static class HashLockRegistryFactory implements Factory>
   {
      @Override
      public LockRegistry create()
      {
         return DefaultLockRegistry.createHashBasedInstance();
      }
   }

   private static class TreeLockRegistryFactory implements Factory>
   {
      @Override
      public LockRegistry create()
      {
         return DefaultLockRegistry.createTreeBasedInstance();
      }
   }
}