Skip to content

Commit 98c0064

Browse files
committed
add new funtion utils
1 parent 63bc7c4 commit 98c0064

3 files changed

Lines changed: 80 additions & 8 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.9.0'
11+
rootProject.version = '9.10.0'
1212
group = 'com.spaceshift'
1313

1414
allprojects {
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-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)