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

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

The newest version!
package net.sf.javagimmicks.concurrent.locks;

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

import net.sf.javagimmicks.util.Supplier;

/**
 * Serves as central {@link Supplier} 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 Supplier>
{
   private static final MultiLockProviderFactory HASH_INSTANCE = new MultiLockProviderFactory(
         new HashLockRegistrySupplier());

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

   /**
    * Returns the hash-based {@link MultiLockProviderFactory} instance which
    * manages resource identifiers using {@link HashMap}s.
    * 
    * @param 
    *           The type of the internally used resource identifiers
    * @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.
    * 
    * @param 
    *           The type of the internally used resource identifiers
    * @return the hash-bases instance
    */
   @SuppressWarnings("unchecked")
   public static  MultiLockProviderFactory getTreeBasedInstance()
   {
      return (MultiLockProviderFactory) TREE_INSTANCE;
   }

   private final Supplier> _registrySupplier;

   private MultiLockProviderFactory(final Supplier> registrySupplier)
   {
      _registrySupplier = registrySupplier;
   }

   @Override
   public MultiLockProvider get()
   {
      return new RegistryLockProvider(_registrySupplier.get());
   }

   private static class HashLockRegistrySupplier implements Supplier>
   {
      @Override
      public LockRegistry get()
      {
         return DefaultLockRegistry.createHashBasedInstance();
      }
   }

   private static class TreeLockRegistrySupplier implements Supplier>
   {
      @Override
      public LockRegistry get()
      {
         return DefaultLockRegistry.createTreeBasedInstance();
      }
   }
}