Skip to content

Commit 4cf5415

Browse files
committed
Merge remote-tracking branch 'original/develop' into feature-9.9.1
# Conflicts: # build.gradle
2 parents dd27a60 + a8db981 commit 4cf5415

3 files changed

Lines changed: 122 additions & 10 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.Predicate;
6+
import java.util.function.Supplier;
7+
8+
public class Functions {
9+
10+
public static class Predicates {
11+
12+
public static @NotNull Predicate<Boolean> isTrue() {
13+
14+
return bool -> bool;
15+
}
16+
17+
public static @NotNull Predicate<Boolean> ifTrue(@NotNull Runnable task) {
18+
19+
return bool -> {
20+
21+
if (bool) {
22+
task.run();
23+
}
24+
25+
return true;
26+
};
27+
}
28+
29+
public static @NotNull Predicate<Boolean> throwIfTrue(@NotNull Supplier<? extends RuntimeException> factory) {
30+
31+
return bool -> {
32+
33+
if (bool) {
34+
throw factory.get();
35+
}
36+
37+
return true;
38+
};
39+
}
40+
41+
public static @NotNull Predicate<Boolean> isFalse() {
42+
43+
return bool -> !bool;
44+
}
45+
46+
public static @NotNull Predicate<Boolean> ifFalse(@NotNull Runnable task) {
47+
48+
return bool -> {
49+
50+
if (!bool) {
51+
task.run();
52+
}
53+
54+
return true;
55+
};
56+
}
57+
58+
public static @NotNull Predicate<Boolean> throwIfFalse(@NotNull Supplier<? extends RuntimeException> factory) {
59+
60+
return bool -> {
61+
62+
if (!bool) {
63+
throw factory.get();
64+
}
65+
66+
return true;
67+
};
68+
}
69+
}
70+
}

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import org.jetbrains.annotations.NotNull;
88
import org.jetbrains.annotations.Nullable;
99

10-
import java.util.Arrays;
11-
import java.util.Comparator;
12-
import java.util.Objects;
10+
import java.util.*;
1311
import java.util.function.*;
1412

1513
/**
@@ -244,6 +242,48 @@ public static <T, F> void fill(@NotNull T[] array, @Nullable F argument, @NotNul
244242
return result;
245243
}
246244

245+
/**
246+
* Combine two arrays to one single array with uniq elements.
247+
*
248+
* @param <T> the base array's component type.
249+
* @param <E> the added array's component type.
250+
* @param base the base array.
251+
* @param added the additional array.
252+
* @return the combined array with uniq elements.
253+
*/
254+
public static <T, E extends T> @NotNull T[] combineUniq(@NotNull T[] base, @Nullable E[] added) {
255+
return combineUniq(base, added, getComponentType(base));
256+
}
257+
258+
/**
259+
* Combine two arrays to one single array with uniq elements.
260+
*
261+
* @param <T> the base array's component type.
262+
* @param <E> the added array's component type.
263+
* @param base the base array.
264+
* @param added the additional array.
265+
* @param type the base array's component type.
266+
* @return the combined array with uniq elements.
267+
*/
268+
public static <T, E extends T> @NotNull T[] combineUniq(
269+
@Nullable T[] base,
270+
@Nullable E[] added,
271+
@NotNull Class<T> type
272+
) {
273+
274+
if (base == null) {
275+
return added == null ? create(type, 0) : added;
276+
} else if (added == null || added.length < 1) {
277+
return base;
278+
}
279+
280+
var result = new HashSet<T>(base.length + added.length);
281+
result.addAll(Arrays.asList(base));
282+
result.addAll(Arrays.asList(added));
283+
284+
return result.toArray(create(type, result.size()));
285+
}
286+
247287
/**
248288
* Check the array on contains the value.
249289
*

rlib-network/src/main/java/com/ss/rlib/network/packet/impl/AbstractPacketReader.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,7 @@ else if (packetLength > tempPendingBuffer.capacity()) {
278278

279279
if (packet != null) {
280280
LOGGER.debug(packet, pck -> "Created instance of packet to read data: " + pck);
281-
282-
if (packet.read(connection, bufferToRead, dataLength)) {
283-
readPacketHandler.accept(packet);
284-
} else {
285-
LOGGER.error("Packet " + packet + " was read incorrectly");
286-
}
287-
281+
readAndHandlePacket(bufferToRead, dataLength, packet);
288282
LOGGER.debug(packet, pck -> "Finished reading data of packet: " + pck);
289283
readPackets++;
290284
} else {
@@ -319,6 +313,14 @@ else if (packetLength > tempPendingBuffer.capacity()) {
319313
return readPackets;
320314
}
321315

316+
protected void readAndHandlePacket(@NotNull ByteBuffer bufferToRead, int dataLength, @NotNull R packet) {
317+
if (packet.read(connection, bufferToRead, dataLength)) {
318+
readPacketHandler.accept(packet);
319+
} else {
320+
LOGGER.error("Packet " + packet + " was read incorrectly");
321+
}
322+
}
323+
322324
/**
323325
* Check buffer's data.
324326
*

0 commit comments

Comments
 (0)