Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <A> the first argument's type.
* @param <T> the second argument's type.
* @param <R> the result's type.
* @return the result from the function.
* @since 9.9.0
*/
default <A, T, R> @Nullable R getInReadLock(
@NotNull A first,
@NotNull T second,
@NotNull NotNullNullableTripleFunction<ConcurrentArray<E>, 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.
*
Expand Down Expand Up @@ -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 <A> the first argument's type.
* @param <T> the second argument's type.
* @param <R> the result's type.
* @return the result from the function.
* @since 9.9.0
*/
default <A, T, R> @Nullable R getInWriteLock(
@NotNull A first,
@NotNull T second,
@NotNull NotNullNullableTripleFunction<ConcurrentArray<E>, A, T, R> function
) {
var stamp = writeLock();
try {
return function.apply(this, first, second);
} finally {
writeUnlock(stamp);
}
}

/**
* Execute a function under {@link #readLock()} block.
*
Expand Down Expand Up @@ -433,6 +483,33 @@ default void writeUnlock(long stamp) {
return this;
}

/**
* Execute a function under {@link #readLock()} block.
*
* @param <A> the first argument's type.
* @param <T> 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 <A, T> @NotNull ConcurrentArray<E> runInReadLock(
@NotNull A first,
@NotNull T second,
@NotNull NotNullTripleConsumer<ConcurrentArray<E>, A, T> function
) {

var stamp = readLock();
try {
function.accept(this, first, second);
} finally {
readUnlock(stamp);
}

return this;
}

/**
* Execute a function under {@link #writeLock()} block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down