Give PacketValue a generic type

This commit is contained in:
Nassim Jahnke 2023-10-20 12:31:16 +10:00
parent 8ebc8c34f3
commit 84a054aac1
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F

View File

@ -46,8 +46,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class PacketWrapperImpl implements PacketWrapper {
private static final Protocol[] PROTOCOL_ARRAY = new Protocol[0];
private final Deque<PacketValue> readableObjects = new ArrayDeque<>();
private final List<PacketValue> packetValues = new ArrayList<>();
private final Deque<PacketValue<?>> readableObjects = new ArrayDeque<>();
private final List<PacketValue<?>> packetValues = new ArrayList<>();
private final ByteBuf inputBuffer;
private final UserConnection userConnection;
private boolean send = true;
@ -73,7 +73,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override
public <T> T get(Type<T> type, int index) throws Exception {
int currentIndex = 0;
for (PacketValue packetValue : packetValues) {
for (PacketValue<?> packetValue : packetValues) {
if (packetValue.type() != type) {
continue;
}
@ -89,7 +89,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override
public boolean is(Type type, int index) {
int currentIndex = 0;
for (PacketValue packetValue : packetValues) {
for (PacketValue<?> packetValue : packetValues) {
if (packetValue.type() != type) {
continue;
}
@ -104,7 +104,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override
public boolean isReadable(Type type, int index) {
int currentIndex = 0;
for (PacketValue packetValue : readableObjects) {
for (PacketValue<?> packetValue : readableObjects) {
if (packetValue.type().getBaseClass() != type.getBaseClass()) {
continue;
}
@ -146,7 +146,7 @@ public class PacketWrapperImpl implements PacketWrapper {
}
PacketValue readValue = readableObjects.poll();
Type readType = readValue.type();
Type<?> readType = readValue.type();
if (readType == type
|| (type.getBaseClass() == readType.getBaseClass()
&& type.getOutputClass() == readType.getOutputClass())) {
@ -159,7 +159,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override
public <T> void write(Type<T> type, T value) {
packetValues.add(new PacketValue(type, attemptTransform(type, value)));
packetValues.add(new PacketValue<>(type, attemptTransform(type, value)));
}
/**
@ -169,11 +169,12 @@ public class PacketWrapperImpl implements PacketWrapper {
* @param value value
* @return value if already matching, else the converted value or possibly unmatched value
*/
private @Nullable Object attemptTransform(Type<?> expectedType, @Nullable Object value) {
private <T> @Nullable T attemptTransform(Type<T> expectedType, @Nullable T value) {
if (value != null && !expectedType.getOutputClass().isAssignableFrom(value.getClass())) {
// Attempt conversion
if (expectedType instanceof TypeConverter) {
return ((TypeConverter<?>) expectedType).from(value);
if (expectedType instanceof TypeConverter<?>) {
//noinspection unchecked
return ((TypeConverter<T>) expectedType).from(value);
}
Via.getPlatform().getLogger().warning("Possible type mismatch: " + value.getClass().getName() + " -> " + expectedType.getOutputClass());
@ -210,10 +211,10 @@ public class PacketWrapperImpl implements PacketWrapper {
}
int index = 0;
for (PacketValue packetValue : packetValues) {
for (final PacketValue<?> packetValue : packetValues) {
try {
packetValue.type().write(buffer, packetValue.value());
} catch (Exception e) {
packetValue.write(buffer);
} catch (final Exception e) {
throw createInformativeException(e, packetValue.type(), index);
}
index++;
@ -563,16 +564,16 @@ public class PacketWrapperImpl implements PacketWrapper {
'}';
}
public static final class PacketValue {
private final Type type;
private Object value;
public static final class PacketValue<T> {
private final Type<T> type;
private T value;
private PacketValue(Type type, @Nullable Object value) {
private PacketValue(final Type<T> type, @Nullable final T value) {
this.type = type;
this.value = value;
}
public Type type() {
public Type<T> type() {
return type;
}
@ -580,7 +581,11 @@ public class PacketWrapperImpl implements PacketWrapper {
return value;
}
public void setValue(@Nullable Object value) {
public void write(final ByteBuf buffer) throws Exception {
type.write(buffer, value);
}
public void setValue(@Nullable final T value) {
this.value = value;
}
@ -588,7 +593,7 @@ public class PacketWrapperImpl implements PacketWrapper {
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final PacketValue that = (PacketValue) o;
final PacketValue<?> that = (PacketValue<?>) o;
if (!type.equals(that.type)) return false;
return Objects.equals(value, that.value);
}