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 * @deprecated use a {@link Function} that returns a {@link ManagedLock}
29 * instead.
30 */
31 @Deprecated
32 public interface LockManager<T> {
33
34 /**
35 * Execute the supplied {@link Supplier} under a lock determined by the
36 * descriptor. Does not throw a checked exception.
37 *
38 * @param <R> the result type
39 * @param descriptor to look up the lock
40 * @param supplier the operation to perform under lock
41 * @return whatever the supplied {@link Supplier} returns
42 */
43 <R> R withLock(final T descriptor, final Supplier<R> supplier);
44
45 /**
46 * Execute the supplied {@link Callable} under a lock determined by the
47 * descriptor.
48 *
49 * @param <R> the result type
50 * @param descriptor to look up the lock
51 * @param callable the operation to perform under lock
52 * @return whatever the supplied {@link Callable} returns
53 * @throws Exception if the supplied {@link Callable} throws an exception
54 */
55 <R> R withLock(final T descriptor, final Callable<R> callable) throws Exception;
56
57 /**
58 * Execute the supplied {@link Runnable} under a lock determined by the
59 * descriptor.
60 *
61 * @param descriptor to look up the lock
62 * @param runnable the operation to perform under lock
63 */
64 void withLock(final T descriptor, final Runnable runnable);
65
66 /**
67 * Maintains two lock managers that internally use the same map of
68 * {@link ReadWriteLock read/write locks}
69 *
70 * @param <T>
71 */
72 interface ReadWrite<T> {
73 /**
74 * For performing operations that require read locks
75 *
76 * @return a lock manager that uses read locks
77 */
78 LockManager<T> read();
79
80 /**
81 * For performing operations that require write locks
82 *
83 * @return a lock manager that uses write locks
84 */
85 LockManager<T> write();
86 }
87 }