org.entityfs.lock
Class CompoundEntityLock

java.lang.Object
  extended by org.entityfs.lock.CompoundEntityLock
All Implemented Interfaces:
Lock, EntityLock

public class CompoundEntityLock
extends Object
implements EntityLock

This is an EntityLock that contains a collection of EntityLock:s. When this lock is locked, all locks in the internal list are locked, in the list iteration order.

The internal state of this object is protected by its intrinsic lock (synchronized). This may lead to deadlocks between threads that try to lock this lock if either of them holds any of the locks that this compound lock contains.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
CompoundEntityLock(Collection<? extends EntityLock> c, boolean writeLock)
          Create a new compound lock instance.
 
Method Summary
 int getHoldCount()
          Get the hold count of the lock.
 EntityView getLocked()
          Get the locked object.
 boolean isDummy()
          Is this a dummy lock? This lock is a dummy lock if all its locks are.
 boolean isHeldByCurrentThread()
          Is this lock held by the current thread? (This is implemented by ReentrantLock but not specified in the Lock interface.)
 boolean isReadLock()
          Returns true if this lock is a read lock.
 boolean isWriteLock()
          Returns true if this lock is a write lock.
 void lock()
           
 void lockInterruptibly()
           
 Condition newCondition()
           
 boolean tryLock()
          Try to lock this lock.
 boolean tryLock(long timeout, TimeUnit tu)
          Try to lock this lock.
 void unlock()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompoundEntityLock

public CompoundEntityLock(Collection<? extends EntityLock> c,
                          boolean writeLock)
Create a new compound lock instance.

Parameters:
c - The collection of locks. The locks are added to this object's internal list of locks in the order that they are returned when iterating over the collection.
writeLock - Is this a write lock (true) or a read lock ( false)?
Method Detail

isWriteLock

public boolean isWriteLock()
Description copied from interface: EntityLock
Returns true if this lock is a write lock.

Specified by:
isWriteLock in interface EntityLock
Returns:
true if this is a write lock, false if this is a read lock.

isReadLock

public boolean isReadLock()
Description copied from interface: EntityLock
Returns true if this lock is a read lock.

Specified by:
isReadLock in interface EntityLock
Returns:
true if this is a read lock, false if it is a write lock.

getHoldCount

public int getHoldCount()
Description copied from interface: EntityLock
Get the hold count of the lock. (How many Lock.lock() has been called minus the number of times Lock.unlock() has been called by the current thread.) If the hold count is 0, the lock is unlocked. The hold count can never be less than 0.

Specified by:
getHoldCount in interface EntityLock
Returns:
The hold count of the lock

isHeldByCurrentThread

public boolean isHeldByCurrentThread()
Description copied from interface: EntityLock
Is this lock held by the current thread? (This is implemented by ReentrantLock but not specified in the Lock interface.)

Specified by:
isHeldByCurrentThread in interface EntityLock
Returns:
true if this lock is locked by the current thread.

unlock

public void unlock()
Specified by:
unlock in interface Lock

lock

public void lock()
Specified by:
lock in interface Lock

lockInterruptibly

public void lockInterruptibly()
                       throws InterruptedException
Specified by:
lockInterruptibly in interface Lock
Throws:
InterruptedException

tryLock

public boolean tryLock(long timeout,
                       TimeUnit tu)
                throws InterruptedException
Try to lock this lock. The timeout time is the timeout time given for each lock in the lock list. The maximum timeout time is the supplied timeout * the number of locks in the list.

Specified by:
tryLock in interface Lock
Throws:
InterruptedException - If interrupted, all locks that have been locked by this method before the interrupted are unlocked before throwing the exception.

tryLock

public boolean tryLock()
Try to lock this lock. If the locking of any of the locks in the list fails, all locks locked by this method before that are unlocked before it returns.

Specified by:
tryLock in interface Lock

newCondition

public Condition newCondition()
Specified by:
newCondition in interface Lock
Returns:
A Condition created from the first lock in the lock list. This may or may not be what clients want.

getLocked

public EntityView getLocked()
                     throws UnsupportedOperationException
Description copied from interface: EntityLock
Get the locked object. For locks that are not connected to any particular object (dummy locks, mostly), this method returns null.

Specified by:
getLocked in interface EntityLock
Returns:
The locked object or null if this lock is not connected to any particular object.
Throws:
UnsupportedOperationException - Always!

isDummy

public boolean isDummy()
Is this a dummy lock? This lock is a dummy lock if all its locks are.

Specified by:
isDummy in interface EntityLock
Returns:
true if this lock is a dummy lock.