Skip to content

Commit bc10c1e

Browse files
committed
add new findAny methods to arrays
1 parent 0e0327e commit bc10c1e

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ss.rlib.common.function;
2+
3+
/**
4+
* @author JavaSaBr
5+
*/
6+
@FunctionalInterface
7+
public interface BiIntPredicate {
8+
9+
boolean test(int first, int second);
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
@FunctionalInterface
6+
public interface NotNullFunctionInt<T> extends FunctionInt<T> {
7+
8+
int apply(@NotNull T object);
9+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ default <A, B> boolean removeIf(
731731
* @param <A> the argument's type.
732732
* @param <B> the element converted type.
733733
* @return {@code true} if any elements were removed.
734+
* @since 9.6.0
734735
*/
735736
default <A, B> boolean removeConvertedIf(
736737
@NotNull A argument,
@@ -962,6 +963,38 @@ default <T> boolean anyMatchR(@NotNull T argument, @NotNull NotNullBiPredicate<?
962963
return null;
963964
}
964965

966+
/**
967+
* Search an element using the condition.
968+
*
969+
* @param argument the argument.
970+
* @param filter the condition.
971+
* @return the found element or null.
972+
* @since 9.6.0
973+
*/
974+
default @Nullable E findAnyConvertedToInt(
975+
int argument,
976+
@NotNull NotNullFunctionInt<? super E> converter,
977+
@NotNull BiIntPredicate filter
978+
) {
979+
980+
if (isEmpty()) {
981+
return null;
982+
}
983+
984+
var array = array();
985+
986+
for (int i = 0, length = size(); i < length; i++) {
987+
988+
var element = array[i];
989+
990+
if (filter.test(argument, converter.apply(element))) {
991+
return element;
992+
}
993+
}
994+
995+
return null;
996+
}
997+
965998
/**
966999
* Calculate a count of matched elements.
9671000
*

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.jetbrains.annotations.Nullable;
77

88
import java.util.function.Function;
9-
import java.util.function.Supplier;
109

1110
/**
1211
* The interface with methods to manage thread-safe access with arrays.
@@ -416,6 +415,31 @@ default void writeUnlock(long stamp) {
416415
}
417416
}
418417

418+
/**
419+
* Search an element using the condition under {@link #readLock()} block.
420+
*
421+
* @param argument the argument.
422+
* @param filter the condition.
423+
* @return the found element or null.
424+
* @since 9.6.0
425+
*/
426+
default @Nullable E findAnyConvertedToIntInReadLock(
427+
int argument,
428+
@NotNull NotNullFunctionInt<? super E> converter,
429+
@NotNull BiIntPredicate filter
430+
) {
431+
if (isEmpty()) {
432+
return null;
433+
}
434+
435+
var stamp = readLock();
436+
try {
437+
return findAnyConvertedToInt(argument, converter, filter);
438+
} finally {
439+
readUnlock(stamp);
440+
}
441+
}
442+
419443
/**
420444
* Search an element by condition under {@link #readLock()} block.
421445
*
@@ -523,6 +547,7 @@ default <A, B> boolean removeIfInWriteLock(
523547
* @param <A> the argument's type.
524548
* @param <B> the element converted type.
525549
* @return {@code true} if any elements were removed.
550+
* @since 9.6.0
526551
*/
527552
default <A, B> boolean removeConvertedIfInWriteLock(
528553
@NotNull A argument,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ void findAnyTest() {
224224
}));
225225

226226
Assertions.assertNotNull(array.findAnyL("First".hashCode(), (num, element) -> num == element.hashCode()));
227+
228+
Assertions.assertNotNull(array.findAnyConvertedToInt(
229+
"First".hashCode(),
230+
String::hashCode,
231+
(first, second) -> first == second
232+
));
227233
}
228234

229235
@Test

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ void findAnyInReadLockTest() {
102102

103103
Assertions.assertNotNull(array.findAnyInReadLock("Second".hashCode(), (val, string) -> val == string.hashCode()));
104104
Assertions.assertNull(array.findAnyInReadLock("None".hashCode(), (val, string) -> val == string.hashCode()));
105+
106+
Assertions.assertNotNull(array.findAnyConvertedToInt(
107+
"First".hashCode(),
108+
String::hashCode,
109+
(first, second) -> first == second
110+
));
105111
}
106112

107113
@Test

0 commit comments

Comments
 (0)