|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 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 | + * https://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 | +package org.springframework.data.util; |
| 17 | + |
| 18 | +import java.util.function.Supplier; |
| 19 | + |
| 20 | +import org.springframework.util.Assert; |
| 21 | + |
| 22 | +/** |
| 23 | + * {@code Lock} provides more extensive locking operations than can be obtained using {@code synchronized} methods and |
| 24 | + * {@link java.util.concurrent.locks.Lock}. It allows more flexible structuring and an improved usage model. |
| 25 | + * <p> |
| 26 | + * This Lock abstraction is an extension to the {@link java.util.concurrent.locks.Lock lock utilities} and intended for |
| 27 | + * easier functional and try-with-resources usage. |
| 28 | + * |
| 29 | + * <pre class="code"> |
| 30 | + * ReentrantLock backend = new ReentrantLock(); |
| 31 | + * |
| 32 | + * Lock lock = Lock.of(backend); |
| 33 | + * |
| 34 | + * lock.executeWithoutResult(() -> { |
| 35 | + * // callback without returning a result |
| 36 | + * }); |
| 37 | + * |
| 38 | + * lock.execute(() -> { |
| 39 | + * // callback returning a result |
| 40 | + * return …; |
| 41 | + * }); |
| 42 | + * </pre> |
| 43 | + * |
| 44 | + * @author Mark Paluch |
| 45 | + * @since 3.2 |
| 46 | + */ |
| 47 | +public interface Lock { |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a new {@link Lock} adapter for the given {@link java.util.concurrent.locks.Lock delegate}. |
| 51 | + * |
| 52 | + * @param delegate must not be {@literal null}. |
| 53 | + * @return a new {@link Lock} adapter. |
| 54 | + */ |
| 55 | + static Lock of(java.util.concurrent.locks.Lock delegate) { |
| 56 | + |
| 57 | + Assert.notNull(delegate, "Lock delegate must not be null"); |
| 58 | + |
| 59 | + return new DefaultLock(delegate); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Acquires the lock. |
| 64 | + * <p> |
| 65 | + * If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies |
| 66 | + * dormant until the lock has been acquired. |
| 67 | + * |
| 68 | + * @see java.util.concurrent.locks.Lock#lock() |
| 69 | + */ |
| 70 | + AcquiredLock lock(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Acquires the lock unless the current thread is {@linkplain Thread#interrupt interrupted}. |
| 74 | + * <p> |
| 75 | + * Acquires the lock if it is available and returns immediately. |
| 76 | + * <p> |
| 77 | + * If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies |
| 78 | + * dormant until one of two things happens: |
| 79 | + * <ul> |
| 80 | + * <li>The lock is acquired by the current thread; or |
| 81 | + * <li>Some other thread {@linkplain Thread#interrupt interrupts} the current thread, and interruption of lock |
| 82 | + * acquisition is supported. |
| 83 | + * </ul> |
| 84 | + * <p> |
| 85 | + * If the current thread: |
| 86 | + * <ul> |
| 87 | + * <li>has its interrupted status set on entry to this method; or |
| 88 | + * <li>is {@linkplain Thread#interrupt interrupted} while acquiring the lock, and interruption of lock acquisition is |
| 89 | + * supported, |
| 90 | + * </ul> |
| 91 | + * then {@link InterruptedException} is thrown and the current thread's interrupted status is cleared. |
| 92 | + */ |
| 93 | + AcquiredLock lockInterruptibly() throws InterruptedException; |
| 94 | + |
| 95 | + /** |
| 96 | + * Execute the action specified by the given callback object guarded by a lock and return its result. The |
| 97 | + * {@code action} is only executed once the lock has been acquired. |
| 98 | + * |
| 99 | + * @param action the action to run. |
| 100 | + * @return the result of the action. |
| 101 | + * @param <T> type of the result. |
| 102 | + * @throws RuntimeException if thrown by the action |
| 103 | + */ |
| 104 | + default <T> T execute(Supplier<T> action) { |
| 105 | + try (AcquiredLock l = lock()) { |
| 106 | + return action.get(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Execute the action specified by the given callback object guarded by a lock. The {@code action} is only executed |
| 112 | + * once the lock has been acquired. |
| 113 | + * |
| 114 | + * @param action the action to run. |
| 115 | + * @throws RuntimeException if thrown by the action. |
| 116 | + */ |
| 117 | + default void executeWithoutResult(Runnable action) { |
| 118 | + try (AcquiredLock l = lock()) { |
| 119 | + action.run(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * An acquired lock can be used with try-with-resources for easier releasing. |
| 125 | + */ |
| 126 | + interface AcquiredLock extends AutoCloseable { |
| 127 | + |
| 128 | + /** |
| 129 | + * Releases the lock. |
| 130 | + * |
| 131 | + * @see java.util.concurrent.locks.Lock#unlock() |
| 132 | + */ |
| 133 | + @Override |
| 134 | + void close(); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments