-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazily deserialize BaseComponent in packets
- Loading branch information
Showing
21 changed files
with
706 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
protocol/src/main/java/net/md_5/bungee/protocol/Deserializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.md_5.bungee.protocol; | ||
|
||
/** | ||
* Represents a value that can be deserialized from another value if needed. | ||
* @param <OV> the original value | ||
* @param <D> the deserialized value | ||
*/ | ||
public interface Deserializable<OV, D> | ||
{ | ||
/** | ||
* @return the deserialized value | ||
*/ | ||
D get(); | ||
|
||
/** | ||
* If {@link #hasDeserialized()} returns true, this method may return null. This usually hapens after code has | ||
* edited the deserialized value and wrote it back to its original place. | ||
* @return the original value, if available | ||
*/ | ||
OV original(); | ||
|
||
/** | ||
* If the value has been deserialized, it is adviced to no longer call {@link #original()}, as the deserialized | ||
* value may have been modified. | ||
* @return true if the value has been deserialized | ||
*/ | ||
boolean hasDeserialized(); | ||
} |
22 changes: 22 additions & 0 deletions
22
protocol/src/main/java/net/md_5/bungee/protocol/FunctionDeserializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.md_5.bungee.protocol; | ||
|
||
import java.util.function.Function; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class FunctionDeserializable<OV, D> extends SimpleDeserializable<OV, D> | ||
{ | ||
private final Function<OV, D> function; | ||
|
||
public FunctionDeserializable(OV ov, Function<OV, D> supplier) | ||
{ | ||
super( ov ); | ||
this.function = supplier; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public D deserialize() | ||
{ | ||
return function.apply( original() ); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
protocol/src/main/java/net/md_5/bungee/protocol/NoOrigDeserializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package net.md_5.bungee.protocol; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class NoOrigDeserializable<OV, D> implements Deserializable<OV, D> | ||
{ | ||
private final D value; | ||
|
||
@Override | ||
public D get() | ||
{ | ||
return value; | ||
} | ||
|
||
@Override | ||
public OV original() | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean hasDeserialized() | ||
{ | ||
return true; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
protocol/src/main/java/net/md_5/bungee/protocol/SimpleDeserializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.md_5.bungee.protocol; | ||
|
||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public abstract class SimpleDeserializable<OV, D> implements Deserializable<OV, D> | ||
{ | ||
private final OV original; | ||
private D deserialized; | ||
|
||
/** | ||
* Method called to get the deserialized value. Called only once unless multiple threads are calling get() at the | ||
* same time. | ||
* @return the deserialized value | ||
*/ | ||
@NonNull | ||
protected abstract D deserialize(); | ||
|
||
@Override | ||
public final D get() | ||
{ | ||
if ( !hasDeserialized() ) | ||
{ | ||
return deserialized = deserialize(); | ||
} | ||
return deserialized; | ||
} | ||
|
||
@Override | ||
public final boolean hasDeserialized() | ||
{ | ||
return deserialized != null; | ||
} | ||
|
||
@Override | ||
public final OV original() | ||
{ | ||
return original; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.