Skip to content

Commit 3a7a75b

Browse files
committed
add new methods to Array API
1 parent 9ecc214 commit 3a7a75b

File tree

4 files changed

+105
-8
lines changed

4 files changed

+105
-8
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,10 +1040,11 @@ default <T> boolean anyMatchR(@NotNull T argument, @NotNull NotNullBiPredicate<?
10401040
}
10411041

10421042
/**
1043-
* Search an element using the condition.
1043+
* Search a converted element to int using the condition.
10441044
*
10451045
* @param argument the argument.
1046-
* @param filter the condition.
1046+
* @param converter the converter element to int.
1047+
* @param filter the condition.
10471048
* @return the found element or null.
10481049
* @since 9.6.0
10491050
*/
@@ -1071,6 +1072,42 @@ default <T> boolean anyMatchR(@NotNull T argument, @NotNull NotNullBiPredicate<?
10711072
return null;
10721073
}
10731074

1075+
/**
1076+
* Search a converted element to int using the condition.
1077+
*
1078+
* @param argument the argument.
1079+
* @param firstConverter the converter element to T.
1080+
* @param secondConverter the converter element to int.
1081+
* @param filter the condition.
1082+
* @param <T> the first converted type.
1083+
* @return the found element or null.
1084+
* @since 9.7.0
1085+
*/
1086+
default <T> @Nullable E findAnyConvertedToInt(
1087+
int argument,
1088+
@NotNull NotNullFunction<? super E, T> firstConverter,
1089+
@NotNull NotNullFunctionInt<T> secondConverter,
1090+
@NotNull BiIntPredicate filter
1091+
) {
1092+
1093+
if (isEmpty()) {
1094+
return null;
1095+
}
1096+
1097+
var array = array();
1098+
1099+
for (int i = 0, length = size(); i < length; i++) {
1100+
1101+
var element = array[i];
1102+
1103+
if (filter.test(argument, secondConverter.apply(firstConverter.apply(element)))) {
1104+
return element;
1105+
}
1106+
}
1107+
1108+
return null;
1109+
}
1110+
10741111
/**
10751112
* Calculate a count of matched elements.
10761113
*

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,11 @@ default void writeUnlock(long stamp) {
468468
}
469469

470470
/**
471-
* Search an element using the condition under {@link #readLock()} block.
471+
* Search a converted element to int using the condition under {@link #readLock()} block.
472472
*
473-
* @param argument the argument.
474-
* @param filter the condition.
473+
* @param argument the argument.
474+
* @param converter the converter element to int.
475+
* @param filter the condition.
475476
* @return the found element or null.
476477
* @since 9.6.0
477478
*/
@@ -492,6 +493,35 @@ default void writeUnlock(long stamp) {
492493
}
493494
}
494495

496+
/**
497+
* Search a converted element to int using the condition under {@link #readLock()} block.
498+
*
499+
* @param argument the argument.
500+
* @param firstConverter the converter element to T.
501+
* @param secondConverter the converter element to int.
502+
* @param filter the condition.
503+
* @param <T> the first converted type.
504+
* @return the found element or null.
505+
* @since 9.7.0
506+
*/
507+
default <T> @Nullable E findAnyConvertedToIntInReadLock(
508+
int argument,
509+
@NotNull NotNullFunction<? super E, T> firstConverter,
510+
@NotNull NotNullFunctionInt<T> secondConverter,
511+
@NotNull BiIntPredicate filter
512+
) {
513+
if (isEmpty()) {
514+
return null;
515+
}
516+
517+
var stamp = readLock();
518+
try {
519+
return findAnyConvertedToInt(argument, firstConverter, secondConverter, filter);
520+
} finally {
521+
readUnlock(stamp);
522+
}
523+
}
524+
495525
/**
496526
* Search an element by condition under {@link #readLock()} block.
497527
*

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.ss.rlib.common.concurrent.atomic.AtomicInteger;
44
import com.ss.rlib.common.test.BaseTest;
5+
import com.ss.rlib.common.util.NumberUtils;
56
import com.ss.rlib.common.util.array.*;
67
import org.junit.jupiter.api.Assertions;
78
import org.junit.jupiter.api.Test;
@@ -225,7 +226,21 @@ void findAnyTest() {
225226
Assertions.assertNotNull(array.findAnyConvertedToInt(
226227
"First".hashCode(),
227228
String::hashCode,
228-
(first, second) -> first == second
229+
NumberUtils::equals
230+
));
231+
232+
Assertions.assertNotNull(array.findAnyConvertedToInt(
233+
"MyValue".hashCode(),
234+
object -> "MyValue",
235+
String::hashCode,
236+
NumberUtils::equals
237+
));
238+
239+
Assertions.assertNull(array.findAnyConvertedToInt(
240+
"MyValue".hashCode(),
241+
object -> "First",
242+
String::hashCode,
243+
NumberUtils::equals
229244
));
230245

231246
Assertions.assertNotNull(array.findAnyConverted(

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.ss.rlib.common.util.array.Array;
88
import com.ss.rlib.common.util.array.ArrayFactory;
99
import com.ss.rlib.common.util.array.ConcurrentArray;
10+
import org.junit.jupiter.api.Assertions;
1011
import org.junit.jupiter.api.Test;
1112

1213
import java.util.Collections;
@@ -208,17 +209,31 @@ void findAnyInReadLockTest() {
208209
assertNotNull(array.findAnyInReadLock("Second".hashCode(), (val, string) -> val == string.hashCode()));
209210
assertNull(array.findAnyInReadLock("None".hashCode(), (val, string) -> val == string.hashCode()));
210211

211-
assertNotNull(array.findAnyConvertedToInt(
212+
assertNotNull(array.findAnyConvertedToIntInReadLock(
212213
"First".hashCode(),
213214
String::hashCode,
214215
NumberUtils::equals
215216
));
216217

217-
assertNotNull(array.findAnyConverted(
218+
assertNotNull(array.findAnyConvertedInReadLock(
218219
"First".hashCode(),
219220
String::hashCode,
220221
Objects::equals
221222
));
223+
224+
Assertions.assertNotNull(array.findAnyConvertedToIntInReadLock(
225+
"MyValue".hashCode(),
226+
object -> "MyValue",
227+
String::hashCode,
228+
NumberUtils::equals
229+
));
230+
231+
Assertions.assertNull(array.findAnyConvertedToIntInReadLock(
232+
"MyValue".hashCode(),
233+
object -> "First",
234+
String::hashCode,
235+
NumberUtils::equals
236+
));
222237
}
223238

224239
@Test

0 commit comments

Comments
 (0)