com.atlassian.util.concurrent
Class ManagedLocks

java.lang.Object
  extended by com.atlassian.util.concurrent.ManagedLocks

public class ManagedLocks
extends java.lang.Object

Static factory for producing ManagedLock and ManagedLock.ReadWrite instances. Also contains factory methods for getting striping functions for these as well.

All Parameters and returns values do no allow nulls. All supplied functions should not allow null values are permit null returns.

Several methods take stripe functions. These functions should return the same value for the life of their input, i.e. they should be based on immutable properties of the input value - otherwise the stripe function might point to a different lock depending on the state of the input. Whatever they return will be used as a map key, so it should be a good candidate for a map key, i.e. correctly implement Object.hashCode() and Object.equals(Object) and be immutable.

Since:
0.0.7

Method Summary
static ManagedLock manage(java.util.concurrent.locks.Lock lock)
          Get a ManagedLock that manages the supplied Lock.
static ManagedLock.ReadWrite manageReadWrite(java.util.concurrent.locks.ReadWriteLock lock)
          Get a ManagedLock.ReadWrite that manages the supplied ReadWriteLock.
static ManagedLock newManagedLock()
          Get a ManagedLock that manages a ReentrantLock.
static
<T> Function<T,ManagedLock>
weakManagedLockFactory()
          Convenience method that calls weakManagedLockFactory(Function) using the identity function for striping, essentially meaning that unique input will have its own lock.
static
<T,D> Function<T,ManagedLock>
weakManagedLockFactory(Function<T,D> stripeFunction)
          Convenience method that simply calls weakManagedLockFactory(Function, Supplier) with the default lock factory.
static
<T,D> Function<T,ManagedLock>
weakManagedLockFactory(Function<T,D> stripeFunction, Supplier<java.util.concurrent.locks.Lock> lockFactory)
          Create a Function for resolving managed locks .
static
<T> Function<T,ManagedLock.ReadWrite>
weakReadWriteManagedLockFactory()
          A convenience method for calling weakReadWriteManagedLockFactory(Function) that uses the identity function for striping, essentially meaning that unique input will have its own lock.
static
<T,D> Function<T,ManagedLock.ReadWrite>
weakReadWriteManagedLockFactory(Function<T,D> stripeFunction)
          A convenience method for calling weakReadWriteManagedLockFactory(Function, Supplier) that uses default locks
static
<T,D> Function<T,ManagedLock.ReadWrite>
weakReadWriteManagedLockFactory(Function<T,D> stripeFunction, Supplier<java.util.concurrent.locks.ReadWriteLock> lockFactory)
          Create a Function for resolving managed read-write locks.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

manage

@NotNull
public static ManagedLock manage(@NotNull
                                         java.util.concurrent.locks.Lock lock)
Get a ManagedLock that manages the supplied Lock.

Parameters:
lock - the lock to use.
Returns:
a managed lock

newManagedLock

@NotNull
public static ManagedLock newManagedLock()
Get a ManagedLock that manages a ReentrantLock.

Returns:
a managed lock

manageReadWrite

@NotNull
public static ManagedLock.ReadWrite manageReadWrite(@NotNull
                                                            java.util.concurrent.locks.ReadWriteLock lock)
Get a ManagedLock.ReadWrite that manages the supplied ReadWriteLock.

Parameters:
lock - the lock to use.
Returns:
a managed read write lock

weakManagedLockFactory

@NotNull
public static <T,D> Function<T,ManagedLock> weakManagedLockFactory(@NotNull
                                                                           Function<T,D> stripeFunction,
                                                                           @NotNull
                                                                           Supplier<java.util.concurrent.locks.Lock> lockFactory)
Create a Function for resolving managed locks . The particular lock is resolved using a striping Function that is used look up a lock instance. This allows for a finite set of locks to be used even if the set of T is essentially unbounded. The locks are stored using weak references so infrequently accessed locks should not use excess memory.

Type Parameters:
T - the type of the thing used to look up locks
D - the type used to map lock instances, should be a good map key
Parameters:
stripeFunction - to convert the input to the thing used to look up the individual locks
lockFactory - the factory for creating the individual locks
Returns:
a new Function that provides ManagedLock instances that stores created instances with weak references.

weakManagedLockFactory

@NotNull
public static <T,D> Function<T,ManagedLock> weakManagedLockFactory(@NotNull
                                                                           Function<T,D> stripeFunction)
Convenience method that simply calls weakManagedLockFactory(Function, Supplier) with the default lock factory.

Type Parameters:
T - the type of the thing used to look up locks
D - the type used to map lock instances, should be a good map key
Parameters:
stripeFunction - to convert Ts to Ds.
See Also:
weakManagedLockFactory(Function, Supplier)

weakManagedLockFactory

@NotNull
public static <T> Function<T,ManagedLock> weakManagedLockFactory()
Convenience method that calls weakManagedLockFactory(Function) using the identity function for striping, essentially meaning that unique input will have its own lock.

Type Parameters:
T - the type of the thing used to look up locks
See Also:
weakManagedLockFactory(Function, Supplier)

weakReadWriteManagedLockFactory

@NotNull
public static <T,D> Function<T,ManagedLock.ReadWrite> weakReadWriteManagedLockFactory(@NotNull
                                                                                              Function<T,D> stripeFunction,
                                                                                              @NotNull
                                                                                              Supplier<java.util.concurrent.locks.ReadWriteLock> lockFactory)
Create a Function for resolving managed read-write locks. The particular lock is resolved using a striping Function that is used look up a lock instance. This allows for a finite set of locks to be used even if the set of T is essentially unbounded. The locks are stored using weak references so infrequently accessed locks should not use excess memory.

Type Parameters:
T - the type of the thing used to look up locks
D - the type used to map lock instances, should be a good map key
Parameters:
stripeFunction - to convert the input to the thing used to look up the individual locks
lockFactory - the factory for creating the individual locks
Returns:
a new Function that provides ManagedLock.ReadWrite instances that stores created instances with weak references.

weakReadWriteManagedLockFactory

@NotNull
public static <T,D> Function<T,ManagedLock.ReadWrite> weakReadWriteManagedLockFactory(Function<T,D> stripeFunction)
A convenience method for calling weakReadWriteManagedLockFactory(Function, Supplier) that uses default locks

Type Parameters:
T - the type of the thing used to look up locks
D - the type used to map lock instances, should be a good map key
Parameters:
stripeFunction -
Returns:
a new Function that provides ManagedLock.ReadWrite instances that stores created instances with weak references.

weakReadWriteManagedLockFactory

@NotNull
public static <T> Function<T,ManagedLock.ReadWrite> weakReadWriteManagedLockFactory()
A convenience method for calling weakReadWriteManagedLockFactory(Function) that uses the identity function for striping, essentially meaning that unique input will have its own lock.

Type Parameters:
T - the type of the thing used to look up locks
Returns:
a new Function that provides the appropriate ManagedLock.ReadWrite for the argument ManagedLock.ReadWrite instances that stores created instances with weak references.


Copyright © 2011 Atlassian. All Rights Reserved.