org.entityfs.lock.cmd
Class LockCollection

java.lang.Object
  extended by org.entityfs.lock.cmd.LockCollection

public class LockCollection
extends Object

The LockCollection contains a collection of locked EntityLock :s. A lock is saved together with the LockCommand that was used to lock it, and the LockCommand can later be used to retrieve the lock.

Objects of this class are not thread safe.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
LockCollection()
           
 
Method Summary
 void addLock(LockCommand cmd, EntityLock l)
          Add one (locked) lock for the supplied command.
 void addLock(TwoObjects<? extends LockCommand,? extends EntityLock> l)
          Add one locked lock and the command that generated it.
 void addLocks(Collection<? extends TwoObjects<? extends LockCommand,? extends EntityLock>> locks)
          Add all the locks and commands in the supplied collection.
 boolean isEmpty()
          Is the lock collection empty.
 Set<Map.Entry<LockCommand,Queue<EntityLock>>> lockEntrySet()
          Get a set containing all entries in the lock collection.
 void merge(LockCollection lc)
          Merge the contents of the supplied lock collection into this collection.
 EntityLock removeLock(LockCommand cmd)
          Remove a lock that was locked by the supplied command and return it.
 Collection<TwoObjects<LockCommand,EntityLock>> removeLocks(Collection<? extends LockCommand> c)
          Remove the locks created by the supplied commands from the collection and return them.
 void unlockAll()
          Unlock all remaining locks in the collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LockCollection

public LockCollection()
Method Detail

addLock

public void addLock(LockCommand cmd,
                    EntityLock l)
Add one (locked) lock for the supplied command.

Parameters:
cmd - The command.
l - The lock.
See Also:
addLock(LockCommand, EntityLock), addLocks(Collection)

addLock

public void addLock(TwoObjects<? extends LockCommand,? extends EntityLock> l)
Add one locked lock and the command that generated it.

Parameters:
l - The command and the lock.
See Also:
addLock(LockCommand, EntityLock), addLocks(Collection)

addLocks

public void addLocks(Collection<? extends TwoObjects<? extends LockCommand,? extends EntityLock>> locks)
Add all the locks and commands in the supplied collection.

Parameters:
locks - A collection of locks together with the command that locked them.
See Also:
addLock(LockCommand, EntityLock), addLock(TwoObjects)

removeLock

public EntityLock removeLock(LockCommand cmd)
                      throws MissingLockException
Remove a lock that was locked by the supplied command and return it. If several locks produced by the same (or equal) commands, return only the first lock found.

The lock commands are checked for equality, so the supplied lock command does not have to be the same lock command instance that produced the lock; it just has to be equal to that lock command. (Or, rather, the lock commands have to have equal hash codes, but that should be the same as to say that the lock commands have to be equal, provided that the lock command implementations follow the equals <-> hashCode contract.)

Parameters:
cmd - The lock command.
Returns:
The lock, or null if there were no locks for the lock command in the collection.
Throws:
MissingLockException - If a lock locked by the supplied command is not found.
See Also:
removeLocks(Collection)

removeLocks

public Collection<TwoObjects<LockCommand,EntityLock>> removeLocks(Collection<? extends LockCommand> c)
                                                           throws MissingLockException
Remove the locks created by the supplied commands from the collection and return them. If several locks were locked by the same command, only the first found lock for that command is returned.

If no lock is found for any of the commands, the method throws an MissingLockException and no locks are removed.

See removeLock(LockCommand) for a discussion on lock command equality.

Parameters:
c - The collection of lock commands.
Returns:
A collection of locks locked by the supplied commands. This collection may not contain locks for all supplied commands if executing the lock commands did not generate any locks (for instance on a non-locking file system).
Throws:
MissingLockException - If no lock was found for any of the supplied commands.
See Also:
removeLock(LockCommand)

lockEntrySet

public Set<Map.Entry<LockCommand,Queue<EntityLock>>> lockEntrySet()
Get a set containing all entries in the lock collection.

The returned set is read only.

Returns:
The set of entries in the lock collection.

isEmpty

public boolean isEmpty()
Is the lock collection empty.

Returns:
true if the lock collection is empty.

merge

public void merge(LockCollection lc)
Merge the contents of the supplied lock collection into this collection.

Parameters:
lc - The lock collection with contents to be merged into this lock collection. May be null.

unlockAll

public void unlockAll()
Unlock all remaining locks in the collection. This method can be used in a finally block at the end of the code block where the lock collection is used to ensure that all locks are released.