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

net.sf.javagimmicks.concurrent.impl.DefaultLockRegistry Maven / Gradle / Ivy

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

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeMap;

import net.sf.javagimmicks.lang.Factory;

public class DefaultLockRegistry implements LockRegistry, Serializable
{
   private static final long serialVersionUID = -785304313135882910L;

   private static final Factory> HASHSET_SHARED_COLLECTION_FACTORY = new HashSetSharedCollectionFactory();

   public static  DefaultLockRegistry createHashBasedInstance()
   {
      return new DefaultLockRegistry(new HashMap(), new HashMap>(),
            HASHSET_SHARED_COLLECTION_FACTORY);
   }
   
   public static  DefaultLockRegistry createTreeBasedInstance()
   {
      return new DefaultLockRegistry(new TreeMap(), new TreeMap>(),
            HASHSET_SHARED_COLLECTION_FACTORY);
   }
   
   protected final Map _exRegistry;
   protected final Map> _shRegistry;
   protected final Factory> _shCollectionFactory;

   public DefaultLockRegistry(Map exRegistry, Map> shRegistry,
         Factory> sharedCollectionFactory)
   {
      _exRegistry = exRegistry;
      _shRegistry = shRegistry;
      _shCollectionFactory = sharedCollectionFactory;
   }
   
   public boolean isSharedFree(Collection resources)
   {
      for (K resource : resources)
      {
         if (_shRegistry.containsKey(resource))
         {
            return false;
         }
      }

      return true;
   }

   public void registerShared(Collection resources)
   {
      Thread currentThread = Thread.currentThread();

      for (K resource : resources)
      {
         Collection threadsForResource = _shRegistry.get(resource);
         if (threadsForResource == null)
         {
            threadsForResource = _shCollectionFactory.create();
            _shRegistry.put(resource, threadsForResource);
         }

         threadsForResource.add(currentThread);
      }
   }

   public void unregisterShared(Collection resources)
   {
      Thread currentThread = Thread.currentThread();

      for (K resource : resources)
      {
         Collection threadsForResource = _shRegistry.get(resource);
         if (threadsForResource == null)
         {
             continue;
         }
         
         threadsForResource.remove(currentThread);

         if (threadsForResource.isEmpty())
         {
            _shRegistry.remove(resource);
         }
      }
   }

   public boolean isExclusiveFree(Collection resources)
   {
      for (K resource : resources)
      {
         if (_exRegistry.containsKey(resource))
         {
            return false;
         }
      }

      return true;
   }

   public void registerExclusive(Collection resources)
   {
      Thread currentThread = Thread.currentThread();

      for (K resource : resources)
      {
         _exRegistry.put(resource, currentThread);
      }
   }

   public void unregisterExclusive(Collection resources)
   {
      for (K resource : resources)
      {
         _exRegistry.remove(resource);
      }
   }
   
   private static final class HashSetSharedCollectionFactory implements Factory>, Serializable
   {
      private static final long serialVersionUID = 3613642441786733902L;

      public Collection create()
      {
         return new HashSet();
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy