Skip to content

Commit 9c511c1

Browse files
committed
add new API to number utils and arrays
1 parent 44a6e10 commit 9c511c1

7 files changed

Lines changed: 244 additions & 74 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
}
1010

11-
rootProject.version = '9.6.0'
11+
rootProject.version = '9.7.0'
1212
group = 'com.spaceshift'
1313

1414
allprojects {

rlib-common/src/main/java/com/ss/rlib/common/util/NumberUtils.java

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,73 +13,6 @@
1313
*/
1414
public final class NumberUtils {
1515

16-
/**
17-
* Bytes to int int.
18-
*
19-
* @param array the array
20-
* @param offset the offset
21-
* @param bigEndian the big endian
22-
* @return the int
23-
*/
24-
@Deprecated(forRemoval = true)
25-
public static int bytesToInt(@NotNull byte[] array, int offset, boolean bigEndian) {
26-
27-
if (bigEndian) {
28-
return makeInt(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
29-
}
30-
31-
return makeInt(array[offset + 3], array[offset + 2], array[offset + 1], array[offset]);
32-
}
33-
34-
/**
35-
* Bytes to u int long.
36-
*
37-
* @param array the array
38-
* @param offset the offset
39-
* @param bigEndian the big endian
40-
* @return the long
41-
*/
42-
@Deprecated(forRemoval = true)
43-
public static long bytesToUInt(@NotNull byte[] array, int offset, boolean bigEndian) {
44-
45-
long value = 0;
46-
47-
if (bigEndian) {
48-
value = makeInt(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
49-
} else {
50-
value = makeInt(array[offset + 3], array[offset + 2], array[offset + 1], array[offset]);
51-
}
52-
53-
return value & 0xFFFFFFFFL;
54-
}
55-
56-
/**
57-
* Make int int.
58-
*
59-
* @param byte1 the byte 1
60-
* @param byte2 the byte 2
61-
* @param byte3 the byte 3
62-
* @param byte4 the byte 4
63-
* @return the int
64-
*/
65-
@Deprecated(forRemoval = true)
66-
public static int makeInt(byte byte1, byte byte2, byte byte3, byte byte4) {
67-
return (byte4 & 0xFF) << 24 | (byte3 & 0xFF) << 16 | (byte2 & 0xFF) << 8 | byte1 & 0xFF;
68-
}
69-
70-
/**
71-
* Make long long.
72-
*
73-
* @param bytes the bytes
74-
* @return the long
75-
*/
76-
@Deprecated(forRemoval = true)
77-
public static long makeLong(@NotNull byte[] bytes) {
78-
return ((long) bytes[7] & 0xFF) << 56 | ((long) bytes[6] & 0xFF) << 48 | ((long) bytes[5] & 0xFF) << 40 |
79-
((long) bytes[4] & 0xFF) << 32 | ((long) bytes[3] & 0xFF) << 24 | ((long) bytes[2] & 0xFF) << 16 |
80-
((long) bytes[1] & 0xFF) << 8 | (long) bytes[0] & 0xFF;
81-
}
82-
8316
/**
8417
* Get a short value from a byte array.
8518
*
@@ -402,6 +335,84 @@ public static boolean toBoolean(
402335
}
403336
}
404337

338+
/**
339+
* Returns {@code true} if the numbers are equal to each other
340+
* and {@code false} otherwise.
341+
*
342+
* @param first the first number.
343+
* @param second the second number to be compared with {@code first} for equality.
344+
* @return {@code true} if the arguments are equal to each other and {@code false} otherwise.
345+
* @since 9.7.0
346+
*/
347+
public static boolean equals(byte first, byte second) {
348+
return first == second;
349+
}
350+
351+
/**
352+
* Returns {@code true} if the numbers are equal to each other
353+
* and {@code false} otherwise.
354+
*
355+
* @param first the first number.
356+
* @param second the second number to be compared with {@code first} for equality.
357+
* @return {@code true} if the arguments are equal to each other and {@code false} otherwise.
358+
* @since 9.7.0
359+
*/
360+
public static boolean equals(short first, short second) {
361+
return first == second;
362+
}
363+
364+
/**
365+
* Returns {@code true} if the numbers are equal to each other
366+
* and {@code false} otherwise.
367+
*
368+
* @param first the first number.
369+
* @param second the second number to be compared with {@code first} for equality.
370+
* @return {@code true} if the arguments are equal to each other and {@code false} otherwise.
371+
* @since 9.7.0
372+
*/
373+
public static boolean equals(int first, int second) {
374+
return first == second;
375+
}
376+
377+
/**
378+
* Returns {@code true} if the numbers are equal to each other
379+
* and {@code false} otherwise.
380+
*
381+
* @param first the first number.
382+
* @param second the second number to be compared with {@code first} for equality.
383+
* @return {@code true} if the arguments are equal to each other and {@code false} otherwise.
384+
* @since 9.7.0
385+
*/
386+
public static boolean equals(long first, long second) {
387+
return first == second;
388+
}
389+
390+
/**
391+
* Returns {@code true} if the numbers are equal to each other
392+
* and {@code false} otherwise.
393+
*
394+
* @param first the first number.
395+
* @param second the second number to be compared with {@code first} for equality.
396+
* @return {@code true} if the arguments are equal to each other and {@code false} otherwise.
397+
* @since 9.7.0
398+
*/
399+
public static boolean equals(float first, float second) {
400+
return first == second;
401+
}
402+
403+
/**
404+
* Returns {@code true} if the numbers are equal to each other
405+
* and {@code false} otherwise.
406+
*
407+
* @param first the first number.
408+
* @param second the second number to be compared with {@code first} for equality.
409+
* @return {@code true} if the arguments are equal to each other and {@code false} otherwise.
410+
* @since 9.7.0
411+
*/
412+
public static boolean equals(double first, double second) {
413+
return first == second;
414+
}
415+
405416
private NumberUtils() {
406417
throw new IllegalArgumentException();
407418
}

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ default <A, B> boolean removeIf(
774774
* @return {@code true} if any elements were removed.
775775
* @since 9.6.0
776776
*/
777-
default <A, B> boolean removeConvertedIf(
777+
default <A, B> boolean removeIfConverted(
778778
@NotNull A argument,
779779
@NotNull NotNullFunction<? super E, B> converter,
780780
@NotNull NotNullBiPredicate<A, B> filter
@@ -810,6 +810,25 @@ default <T> boolean anyMatch(@NotNull T argument, @NotNull NotNullBiPredicate<T,
810810
return findAny(argument, filter) != null;
811811
}
812812

813+
/**
814+
* Return true if there is at least a converted element for the condition.
815+
*
816+
* @param argument the argument.
817+
* @param converter the converter element to another type.
818+
* @param filter the condition.
819+
* @param <T> the argument's type.
820+
* @param <C> the converted element's type.
821+
* @return true if there is at least an element for the condition.
822+
* @since 9.7.0
823+
*/
824+
default <T, C> boolean anyMatchConverted(
825+
@NotNull T argument,
826+
@NotNull NotNullFunction<? super E, C> converter,
827+
@NotNull NotNullBiPredicate<T, C> filter
828+
) {
829+
return findAnyConverted(argument, converter, filter) != null;
830+
}
831+
813832
/**
814833
* Return true if there is at least an element for the condition.
815834
*
@@ -887,6 +906,41 @@ default <T> boolean anyMatchR(@NotNull T argument, @NotNull NotNullBiPredicate<?
887906
return null;
888907
}
889908

909+
/**
910+
* Search an element using the condition by converted value.
911+
*
912+
* @param argument the argument.
913+
* @param converter the converted an element to another type.
914+
* @param filter the condition.
915+
* @param <T> the argument's type.
916+
* @param <C> the converted element's type.
917+
* @return the found element or null.
918+
* @since 9.7.0
919+
*/
920+
default <T, C> @Nullable E findAnyConverted(
921+
@NotNull T argument,
922+
@NotNull NotNullFunction<? super E, C> converter,
923+
@NotNull NotNullBiPredicate<T, C> filter
924+
) {
925+
926+
if (isEmpty()) {
927+
return null;
928+
}
929+
930+
var array = array();
931+
932+
for (int i = 0, length = size(); i < length; i++) {
933+
934+
var element = array[i];
935+
936+
if (filter.test(argument, converter.apply(element))) {
937+
return element;
938+
}
939+
}
940+
941+
return null;
942+
}
943+
890944
/**
891945
* Find an element for the condition.
892946
*

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,34 @@ default void writeUnlock(long stamp) {
415415
}
416416
}
417417

418+
/**
419+
* Search an element using the condition by converted value under {@link #readLock()} block.
420+
*
421+
* @param argument the argument.
422+
* @param converter the converted an element to another type.
423+
* @param filter the condition.
424+
* @param <T> the argument's type.
425+
* @param <C> the converted element's type.
426+
* @return the found element or null.
427+
* @since 9.7.0
428+
*/
429+
default <T, C> @Nullable E findAnyConvertedInReadLock(
430+
@NotNull T argument,
431+
@NotNull NotNullFunction<? super E, C> converter,
432+
@NotNull NotNullBiPredicate<T, C> filter
433+
) {
434+
if (isEmpty()) {
435+
return null;
436+
}
437+
438+
var stamp = readLock();
439+
try {
440+
return findAnyConverted(argument, converter, filter);
441+
} finally {
442+
readUnlock(stamp);
443+
}
444+
}
445+
418446
/**
419447
* Search an element using the condition under {@link #readLock()} block.
420448
*
@@ -483,6 +511,34 @@ default boolean anyMatchInReadLock(int argument, @NotNull NotNullIntObjectPredic
483511
}
484512
}
485513

514+
/**
515+
* Return true if there is at least a converted element for the condition under {@link #readLock()} block.
516+
*
517+
* @param argument the argument.
518+
* @param converter the converter element to another type.
519+
* @param filter the condition.
520+
* @param <T> the argument's type.
521+
* @param <C> the converted element's type.
522+
* @return true if there is at least an element for the condition.
523+
* @since 9.7.0
524+
*/
525+
default <T, C> boolean anyMatchConvertedInReadLock(
526+
@NotNull T argument,
527+
@NotNull NotNullFunction<? super E, C> converter,
528+
@NotNull NotNullBiPredicate<T, C> filter
529+
) {
530+
if (isEmpty()) {
531+
return false;
532+
}
533+
534+
var stamp = readLock();
535+
try {
536+
return anyMatchConverted(argument, converter, filter);
537+
} finally {
538+
readUnlock(stamp);
539+
}
540+
}
541+
486542
/**
487543
* Removes all of the elements of this collection that satisfy the given predicate under {@link #writeLock()} block.
488544
*
@@ -549,14 +605,14 @@ default <A, B> boolean removeIfInWriteLock(
549605
* @return {@code true} if any elements were removed.
550606
* @since 9.6.0
551607
*/
552-
default <A, B> boolean removeConvertedIfInWriteLock(
608+
default <A, B> boolean removeIfConvertedInWriteLock(
553609
@NotNull A argument,
554610
@NotNull NotNullFunction<? super E, B> converter,
555611
@NotNull NotNullBiPredicate<A, B> filter
556612
) {
557613
var stamp = writeLock();
558614
try {
559-
return removeConvertedIf(argument, converter, filter);
615+
return removeIfConverted(argument, converter, filter);
560616
} finally {
561617
writeUnlock(stamp);
562618
}

rlib-common/src/test/java/com/ss/rlib/common/test/util/NumberUtilsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,22 @@ void shouldValidateBooleanCorrectly() {
214214
assertThrows(IllegalArgumentException.class, () -> NumberUtils.toBoolean(2L));
215215
assertThrows(IllegalArgumentException.class, () -> NumberUtils.toBoolean(-1L));
216216
}
217+
218+
@Test
219+
void shouldEqualsNumbersCorrectly() {
220+
221+
assertTrue(NumberUtils.equals((byte) 10, (byte) 10));
222+
assertTrue(NumberUtils.equals((short) 10, (short) 10));
223+
assertTrue(NumberUtils.equals(10, 10));
224+
assertTrue(NumberUtils.equals(10L, 10L));
225+
assertTrue(NumberUtils.equals(10F, 10F));
226+
assertTrue(NumberUtils.equals(10D, 10D));
227+
228+
assertFalse(NumberUtils.equals((byte) -10, (byte) 10));
229+
assertFalse(NumberUtils.equals((short) -10, (short) 10));
230+
assertFalse(NumberUtils.equals(-10, 10));
231+
assertFalse(NumberUtils.equals(-10L, 10L));
232+
assertFalse(NumberUtils.equals(-10F, 10F));
233+
assertFalse(NumberUtils.equals(-10D, 10D));
234+
}
217235
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.ArrayList;
1010
import java.util.Arrays;
1111
import java.util.Collection;
12+
import java.util.Objects;
1213
import java.util.stream.IntStream;
1314

1415
/**
@@ -80,7 +81,7 @@ void removeIfTest() {
8081

8182
array = ArrayFactory.asArray("10", "5", "2", "1");
8283

83-
Assertions.assertTrue(array.removeConvertedIf(
84+
Assertions.assertTrue(array.removeIfConverted(
8485
5,
8586
Integer::parseInt,
8687
Integer::equals
@@ -230,6 +231,12 @@ void findAnyTest() {
230231
String::hashCode,
231232
(first, second) -> first == second
232233
));
234+
235+
Assertions.assertNotNull(array.findAnyConverted(
236+
"First".hashCode(),
237+
String::hashCode,
238+
Objects::equals
239+
));
233240
}
234241

235242
@Test
@@ -263,6 +270,9 @@ void anyMatchTest() {
263270

264271
Assertions.assertTrue(array.anyMatchR("Second".hashCode(), (element, num) -> element.hashCode() == num));
265272
Assertions.assertFalse(array.anyMatchR("None".hashCode(), (element, num) -> element.hashCode() == num));
273+
274+
Assertions.assertTrue(array.anyMatchConverted("Second".hashCode(), String::hashCode, Objects::equals));
275+
Assertions.assertFalse(array.anyMatchConverted("None".hashCode(), String::hashCode, Objects::equals));
266276
}
267277

268278
@Test

0 commit comments

Comments
 (0)