View Javadoc

1   /**
2    * Copyright 2008 Atlassian Pty Ltd 
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0 
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package com.atlassian.util.concurrent;
18  
19  import java.util.concurrent.Callable;
20  import java.util.concurrent.locks.ReadWriteLock;
21  
22  /**
23   * {@link LockManager} allows {@link Callable callables}, {@link Supplier
24   * suppliers} and {@link Runnable runnables} to be run under a lock that is
25   * resolved against an input object.
26   * 
27   * @param <T> The input type that we lock on.
28   * @param <D> The stripe type that we stripe locks on.
29   * @deprecated use a {@link Function} that returns a {@link ManagedLock}
30   * instead.
31   */
32  @Deprecated
33  public interface LockManager<T> {
34  
35      /**
36       * Execute the supplied {@link Supplier} under a lock determined by the
37       * descriptor. Does not throw a checked exception.
38       * 
39       * @param <R> the result type
40       * @param descriptor to look up the lock
41       * @param supplier the operation to perform under lock
42       * @return whatever the supplied {@link Supplier} returns
43       */
44      <R> R withLock(final T descriptor, final Supplier<R> supplier);
45  
46      /**
47       * Execute the supplied {@link Callable} under a lock determined by the
48       * descriptor.
49       * 
50       * @param <R> the result type
51       * @param descriptor to look up the lock
52       * @param callable the operation to perform under lock
53       * @return whatever the supplied {@link Callable} returns
54       * @throws Exception if the supplied {@link Callable} throws an exception
55       */
56      <R> R withLock(final T descriptor, final Callable<R> callable) throws Exception;
57  
58      /**
59       * Execute the supplied {@link Runnable} under a lock determined by the
60       * descriptor.
61       * 
62       * @param descriptor to look up the lock
63       * @param runnable the operation to perform under lock
64       */
65      void withLock(final T descriptor, final Runnable runnable);
66  
67      /**
68       * Maintains two lock managers that internally use the same map of
69       * {@link ReadWriteLock read/write locks}
70       * 
71       * @param <T>
72       */
73      interface ReadWrite<T> {
74          /**
75           * For performing operations that require read locks
76           * 
77           * @return a lock manager that uses read locks
78           */
79          LockManager<T> read();
80  
81          /**
82           * For performing operations that require write locks
83           * 
84           * @return a lock manager that uses write locks
85           */
86          LockManager<T> write();
87      }
88  }