diff --git a/rlib-common/src/main/java/com/ss/rlib/common/util/array/ConcurrentArray.java b/rlib-common/src/main/java/com/ss/rlib/common/util/array/ConcurrentArray.java index c292edbb..c84fa09a 100644 --- a/rlib-common/src/main/java/com/ss/rlib/common/util/array/ConcurrentArray.java +++ b/rlib-common/src/main/java/com/ss/rlib/common/util/array/ConcurrentArray.java @@ -353,6 +353,31 @@ default void writeUnlock(long stamp) { } } + /** + * Execute a function to get some result under {@link #readLock()} block. + * + * @param first the first argument for the function. + * @param second the second argument for the function. + * @param function the function. + * @param the first argument's type. + * @param the second argument's type. + * @param the result's type. + * @return the result from the function. + * @since 9.9.0 + */ + default @Nullable R getInReadLock( + @NotNull A first, + @NotNull T second, + @NotNull NotNullNullableTripleFunction, A, T, R> function + ) { + var stamp = readLock(); + try { + return function.apply(this, first, second); + } finally { + readUnlock(stamp); + } + } + /** * Execute a function to get some result under {@link #writeLock()} block. * @@ -391,6 +416,31 @@ default void writeUnlock(long stamp) { } } + /** + * Execute a function to get some result under {@link #writeLock()} block. + * + * @param first the first argument for the function. + * @param second the second argument for the function. + * @param function the function. + * @param the first argument's type. + * @param the second argument's type. + * @param the result's type. + * @return the result from the function. + * @since 9.9.0 + */ + default @Nullable R getInWriteLock( + @NotNull A first, + @NotNull T second, + @NotNull NotNullNullableTripleFunction, A, T, R> function + ) { + var stamp = writeLock(); + try { + return function.apply(this, first, second); + } finally { + writeUnlock(stamp); + } + } + /** * Execute a function under {@link #readLock()} block. * @@ -433,6 +483,33 @@ default void writeUnlock(long stamp) { return this; } + /** + * Execute a function under {@link #readLock()} block. + * + * @param the first argument's type. + * @param the second argument's type. + * @param first the first argument. + * @param second the second argument. + * @param function the function. + * @return this array. + * @since 9.9.0 + */ + default @NotNull ConcurrentArray runInReadLock( + @NotNull A first, + @NotNull T second, + @NotNull NotNullTripleConsumer, A, T> function + ) { + + var stamp = readLock(); + try { + function.accept(this, first, second); + } finally { + readUnlock(stamp); + } + + return this; + } + /** * Execute a function under {@link #writeLock()} block. * diff --git a/rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ConcurrentArrayTest.java b/rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ConcurrentArrayTest.java index c14e5ddd..41c1baca 100644 --- a/rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ConcurrentArrayTest.java +++ b/rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ConcurrentArrayTest.java @@ -270,6 +270,13 @@ void runInReadLockTest() { array.runInReadLock("Third", (arr, arg) -> assertEquals(2, arr.count(arg, String::equals))); + + array.runInReadLock(2, "Third", (arr, first, second) -> { + assertType(arr, Array.class); + assertIntType(first); + assertType(second, String.class); + assertEquals(first, arr.count(second, String::equals)); + }); } @Test @@ -279,6 +286,10 @@ void getInWriteLockTest() { assertEquals("Third", array.getInWriteLock(arr -> arr.get(2))); assertEquals("Second", array.getInWriteLock(1, Array::get)); + assertEquals( + "FirstSecond", + array.getInWriteLock(0, 1, (arr, first, second) -> arr.get(first) + arr.get(second)) + ); } @Test @@ -288,6 +299,10 @@ void getInReadLockTest() { assertEquals("Third", array.getInReadLock(arr -> arr.get(2))); assertEquals("Second", array.getInReadLock(1, Array::get)); + assertEquals( + "FirstSecond", + array.getInReadLock(0, 1, (arr, first, second) -> arr.get(first) + arr.get(second)) + ); } @Test