File tree Expand file tree Collapse file tree
rlib-common/src/main/java/com/ss/rlib/common/function
rlib-network/src/main/java/com/ss/rlib/network/packet/impl Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ buildscript {
88 }
99}
1010
11- rootProject. version = ' 9.9 .0'
11+ rootProject. version = ' 9.10 .0'
1212group = ' com.spaceshift'
1313
1414allprojects {
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 *
You can’t perform that action at this time.
0 commit comments