org.entityfs.lock
Class EntityLocks

java.lang.Object
  extended by org.entityfs.lock.EntityLocks

public final class EntityLocks
extends Object

This class contains static utility methods for working with EntityLock:s.

Since:
1.0
Author:
Karl Gustafsson

Method Summary
static Collection<EntityLock> lockAll(boolean writeLock, EntityView... entities)
          Lock all provided entities.
static EntityLock lockParentForReading(EntityView ev)
          Lock the entity's parent for reading and return the lock object.
static EntityLock lockParentForWriting(EntityView ev)
          Lock the entity's parent for writing and return the lock object.
static EntityLock switchReadLocks(EntityView entity, EntityLock oldLock)
          Switch the locked lock for a new read lock on the entity.
static EntityLock switchWriteLocks(EntityView entity, EntityLock oldLock)
          Switch the locked lock for a new write lock on the entity.
static void unlockAll(Collection<? extends EntityLock> locks)
          Unlock all locks in the collection that are locked by the current thread.
static
<T extends EntityLock>
void
unlockAll(T[] locks)
          Unlock all locks in the array that are locked by the current thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

switchReadLocks

public static EntityLock switchReadLocks(EntityView entity,
                                         EntityLock oldLock)
Switch the locked lock for a new read lock on the entity. The old lock is unlocked after the new lock has been acquired.

Parameters:
entity - The entity to lock for reading.
oldLock - The old lock to unlock after acquiring the read lock.
Returns:
A locked read lock for the entity.

switchWriteLocks

public static EntityLock switchWriteLocks(EntityView entity,
                                          EntityLock oldLock)
Switch the locked lock for a new write lock on the entity. The old lock is unlocked after the new lock has been acquired.

Parameters:
entity - The entity to lock for writing.
oldLock - The old lock to unlock after acquiring the write lock.
Returns:
A locked write lock for the entity.

unlockAll

public static void unlockAll(Collection<? extends EntityLock> locks)
Unlock all locks in the collection that are locked by the current thread.

Parameters:
locks - The locks to unlock. May be null.

unlockAll

public static <T extends EntityLock> void unlockAll(T[] locks)
Unlock all locks in the array that are locked by the current thread.

Parameters:
locks - The locks to unlock. May be null.

lockAll

public static Collection<EntityLock> lockAll(boolean writeLock,
                                             EntityView... entities)
Lock all provided entities. The entities are locked in their natural order (hashCode order).

Parameters:
writeLock - If true, entities are locked for writing. If false, locked for reading.
entities - The entities to lock.
Returns:
A collection with all locks.

lockParentForReading

public static EntityLock lockParentForReading(EntityView ev)
                                       throws IllegalArgumentException,
                                              EntityNotFoundException
Lock the entity's parent for reading and return the lock object. There is a, however minuscule, risk that the entity is moved to another parent directory between when its EntityView.getParent() method is called and when the parent is locked. Once the parent is locked, the entity is prevented from moving.

This method keeps trying until it manages to lock the entity's parent directory. (By checking that the entity is still in the parent directory after it is locked.)

When this method returns, it is guaranteed that

  1. The entity's parent is locked for reading.
  2. Calling EntityView.getParent() on the entity will return the locked parent.

Parameters:
ev - The entity whose parent should be locked for reading.
Returns:
The locked read lock on the parent entity.
Throws:
IllegalArgumentException - If the entity is the root directory of a file system. A root directory does not have a parent directory.
EntityNotFoundException - If the entity or the entity's parent are deleted.
See Also:
lockParentForWriting(EntityView)

lockParentForWriting

public static EntityLock lockParentForWriting(EntityView ev)
                                       throws IllegalArgumentException,
                                              EntityNotFoundException
Lock the entity's parent for writing and return the lock object. There is a, however minuscule, risk that the entity is moved to another parent directory between when its EntityView.getParent() method is called and when the parent is locked. Once the parent is locked, the entity is prevented from moving.

This method keeps trying until it manages to lock the entity's parent directory. (By checking that the entity is still in the parent directory after it is locked.)

When this method returns, it is guaranteed that

  1. The entity's parent is locked for writing.
  2. Calling EntityView.getParent() on the entity will return the locked parent.

Parameters:
ev - The entity whose parent should be locked for writing.
Returns:
The locked write lock on the parent entity.
Throws:
IllegalArgumentException - If the entity is the root directory of a file system. A root directory does not have a parent directory.
EntityNotFoundException - If the entity or the entity's parent are deleted.
See Also:
lockParentForWriting(EntityView)