Skip to content

Commit 57d785c

Browse files
committed
Merge remote-tracking branch 'github/develop' into feature-implement_ssl_network
2 parents f890f4b + 11d399b commit 57d785c

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

rlib-common/src/main/java/com/ss/rlib/common/util/array/ConcurrentArray.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,31 @@ default void writeUnlock(long stamp) {
353353
}
354354
}
355355

356+
/**
357+
* Execute a function to get some result under {@link #readLock()} block.
358+
*
359+
* @param first the first argument for the function.
360+
* @param second the second argument for the function.
361+
* @param function the function.
362+
* @param <A> the first argument's type.
363+
* @param <T> the second argument's type.
364+
* @param <R> the result's type.
365+
* @return the result from the function.
366+
* @since 9.9.0
367+
*/
368+
default <A, T, R> @Nullable R getInReadLock(
369+
@NotNull A first,
370+
@NotNull T second,
371+
@NotNull NotNullNullableTripleFunction<ConcurrentArray<E>, A, T, R> function
372+
) {
373+
var stamp = readLock();
374+
try {
375+
return function.apply(this, first, second);
376+
} finally {
377+
readUnlock(stamp);
378+
}
379+
}
380+
356381
/**
357382
* Execute a function to get some result under {@link #writeLock()} block.
358383
*
@@ -391,6 +416,31 @@ default void writeUnlock(long stamp) {
391416
}
392417
}
393418

419+
/**
420+
* Execute a function to get some result under {@link #writeLock()} block.
421+
*
422+
* @param first the first argument for the function.
423+
* @param second the second argument for the function.
424+
* @param function the function.
425+
* @param <A> the first argument's type.
426+
* @param <T> the second argument's type.
427+
* @param <R> the result's type.
428+
* @return the result from the function.
429+
* @since 9.9.0
430+
*/
431+
default <A, T, R> @Nullable R getInWriteLock(
432+
@NotNull A first,
433+
@NotNull T second,
434+
@NotNull NotNullNullableTripleFunction<ConcurrentArray<E>, A, T, R> function
435+
) {
436+
var stamp = writeLock();
437+
try {
438+
return function.apply(this, first, second);
439+
} finally {
440+
writeUnlock(stamp);
441+
}
442+
}
443+
394444
/**
395445
* Execute a function under {@link #readLock()} block.
396446
*
@@ -433,6 +483,33 @@ default void writeUnlock(long stamp) {
433483
return this;
434484
}
435485

486+
/**
487+
* Execute a function under {@link #readLock()} block.
488+
*
489+
* @param <A> the first argument's type.
490+
* @param <T> the second argument's type.
491+
* @param first the first argument.
492+
* @param second the second argument.
493+
* @param function the function.
494+
* @return this array.
495+
* @since 9.9.0
496+
*/
497+
default <A, T> @NotNull ConcurrentArray<E> runInReadLock(
498+
@NotNull A first,
499+
@NotNull T second,
500+
@NotNull NotNullTripleConsumer<ConcurrentArray<E>, A, T> function
501+
) {
502+
503+
var stamp = readLock();
504+
try {
505+
function.accept(this, first, second);
506+
} finally {
507+
readUnlock(stamp);
508+
}
509+
510+
return this;
511+
}
512+
436513
/**
437514
* Execute a function under {@link #writeLock()} block.
438515
*
@@ -474,6 +551,31 @@ default void writeUnlock(long stamp) {
474551
return this;
475552
}
476553

554+
/**
555+
* Execute a function under {@link #writeLock()} block.
556+
*
557+
* @param first the first argument for the function.
558+
* @param second the second argument for the function.
559+
* @param function the function.
560+
* @param <A> the first argument's type.
561+
* @param <T> the second argument's type.
562+
* @return this array.
563+
* @since 9.9.0
564+
*/
565+
default <A, T> @NotNull ConcurrentArray<E> runInWriteLock(
566+
@NotNull A first,
567+
@NotNull T second,
568+
@NotNull NotNullTripleConsumer<ConcurrentArray<E>, A, T> function
569+
) {
570+
var stamp = writeLock();
571+
try {
572+
function.accept(this, first, second);
573+
} finally {
574+
writeUnlock(stamp);
575+
}
576+
return this;
577+
}
578+
477579
/**
478580
* Search an element using the condition under {@link #readLock()} block.
479581
*

rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ConcurrentArrayTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ void runInWriteLockTest() {
241241

242242
var array = ConcurrentArray.of("First", "Second", "Third", " ");
243243

244+
array.runInWriteLock(1, "Second", (arr, first, second) -> assertEquals(second, arr.get(first)));
245+
244246
array.runInWriteLock(object -> object.remove("Second"));
245247

246248
assertEquals(3, array.size());
@@ -270,6 +272,13 @@ void runInReadLockTest() {
270272

271273
array.runInReadLock("Third", (arr, arg) ->
272274
assertEquals(2, arr.count(arg, String::equals)));
275+
276+
array.runInReadLock(2, "Third", (arr, first, second) -> {
277+
assertType(arr, Array.class);
278+
assertIntType(first);
279+
assertType(second, String.class);
280+
assertEquals(first, arr.count(second, String::equals));
281+
});
273282
}
274283

275284
@Test
@@ -279,6 +288,10 @@ void getInWriteLockTest() {
279288

280289
assertEquals("Third", array.getInWriteLock(arr -> arr.get(2)));
281290
assertEquals("Second", array.getInWriteLock(1, Array::get));
291+
assertEquals(
292+
"FirstSecond",
293+
array.getInWriteLock(0, 1, (arr, first, second) -> arr.get(first) + arr.get(second))
294+
);
282295
}
283296

284297
@Test
@@ -288,6 +301,10 @@ void getInReadLockTest() {
288301

289302
assertEquals("Third", array.getInReadLock(arr -> arr.get(2)));
290303
assertEquals("Second", array.getInReadLock(1, Array::get));
304+
assertEquals(
305+
"FirstSecond",
306+
array.getInReadLock(0, 1, (arr, first, second) -> arr.get(first) + arr.get(second))
307+
);
291308
}
292309

293310
@Test

0 commit comments

Comments
 (0)