|
| 1 | +package me.jeremiah.data; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | + |
| 5 | +import java.nio.ByteBuffer; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +public record ByteTranslatable(byte[] bytes) { |
| 11 | + |
| 12 | + @Override |
| 13 | + public int hashCode() { |
| 14 | + return Arrays.hashCode(bytes); |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public boolean equals(Object obj) { |
| 19 | + if (this == obj) return true; |
| 20 | + if (!(obj instanceof ByteTranslatable(byte[] bytes1))) |
| 21 | + return false; |
| 22 | + if (bytes.length != bytes1.length) return false; |
| 23 | + return Arrays.equals(bytes, bytes1); |
| 24 | + } |
| 25 | + |
| 26 | + public static ByteTranslatable from(@NotNull Object object) { |
| 27 | + return switch (object) { |
| 28 | + case ByteTranslatable byteTranslatable -> byteTranslatable; |
| 29 | + case byte[] b -> fromBytes(b); |
| 30 | + case Boolean b -> fromBoolean(b); |
| 31 | + case Byte b -> fromBytes(b); |
| 32 | + case Short i -> fromShort(i); |
| 33 | + case Integer i -> fromInt(i); |
| 34 | + case Long l -> fromLong(l); |
| 35 | + case Float v -> fromFloat(v); |
| 36 | + case Double v -> fromDouble(v); |
| 37 | + case Character c -> fromChar(c); |
| 38 | + case String string -> fromString(string); |
| 39 | + case UUID uuid -> fromUUID(uuid); |
| 40 | + default -> throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + public static ByteTranslatable fromBoolean(boolean value) { |
| 45 | + return new ByteTranslatable(new byte[]{(byte) (value ? 1 : 0)}); |
| 46 | + } |
| 47 | + |
| 48 | + public boolean asBoolean() { |
| 49 | + return bytes[0] == 1; |
| 50 | + } |
| 51 | + |
| 52 | + public static ByteTranslatable fromBytes(byte... value) { |
| 53 | + return new ByteTranslatable(value); |
| 54 | + } |
| 55 | + |
| 56 | + public byte asByte() { |
| 57 | + return bytes[0]; |
| 58 | + } |
| 59 | + |
| 60 | + public static ByteTranslatable fromShort(short value) { |
| 61 | + return new ByteTranslatable(ByteBuffer.allocate(Short.BYTES).putShort(value).array()); |
| 62 | + } |
| 63 | + |
| 64 | + public short asShort() { |
| 65 | + return ByteBuffer.wrap(bytes).getShort(); |
| 66 | + } |
| 67 | + |
| 68 | + public static ByteTranslatable fromInt(int value) { |
| 69 | + return new ByteTranslatable(ByteBuffer.allocate(Integer.BYTES).putInt(value).array()); |
| 70 | + } |
| 71 | + |
| 72 | + public int asInt() { |
| 73 | + return ByteBuffer.wrap(bytes).getInt(); |
| 74 | + } |
| 75 | + |
| 76 | + public static ByteTranslatable fromLong(long value) { |
| 77 | + return new ByteTranslatable(ByteBuffer.allocate(Long.BYTES).putLong(value).array()); |
| 78 | + } |
| 79 | + |
| 80 | + public long asLong() { |
| 81 | + return ByteBuffer.wrap(bytes).getLong(); |
| 82 | + } |
| 83 | + |
| 84 | + public static ByteTranslatable fromFloat(float value) { |
| 85 | + return new ByteTranslatable(ByteBuffer.allocate(Float.BYTES).putFloat(value).array()); |
| 86 | + } |
| 87 | + |
| 88 | + public float asFloat() { |
| 89 | + return ByteBuffer.wrap(bytes).getFloat(); |
| 90 | + } |
| 91 | + |
| 92 | + public static ByteTranslatable fromDouble(double value) { |
| 93 | + return new ByteTranslatable(ByteBuffer.allocate(Double.BYTES).putDouble(value).array()); |
| 94 | + } |
| 95 | + |
| 96 | + public double asDouble() { |
| 97 | + return ByteBuffer.wrap(bytes).getDouble(); |
| 98 | + } |
| 99 | + |
| 100 | + public static ByteTranslatable fromChar(char value) { |
| 101 | + return new ByteTranslatable(ByteBuffer.allocate(Character.BYTES).putChar(value).array()); |
| 102 | + } |
| 103 | + |
| 104 | + public char asChar() { |
| 105 | + return ByteBuffer.wrap(bytes).getChar(); |
| 106 | + } |
| 107 | + |
| 108 | + public static ByteTranslatable fromString(String value) { |
| 109 | + return new ByteTranslatable(value.getBytes(StandardCharsets.UTF_8)); |
| 110 | + } |
| 111 | + |
| 112 | + public String asString() { |
| 113 | + return new String(bytes, StandardCharsets.UTF_8); |
| 114 | + } |
| 115 | + |
| 116 | + private static final int UUID_BYTE_SIZE = Long.BYTES * 2; |
| 117 | + |
| 118 | + public static ByteTranslatable fromUUID(UUID value) { |
| 119 | + return new ByteTranslatable( |
| 120 | + ByteBuffer.allocate(UUID_BYTE_SIZE) |
| 121 | + .putLong(value.getMostSignificantBits()) |
| 122 | + .putLong(value.getLeastSignificantBits()) |
| 123 | + .array() |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + public UUID asUUID() { |
| 128 | + ByteBuffer buffer = ByteBuffer.wrap(bytes); |
| 129 | + long mostSigBits = buffer.getLong(); |
| 130 | + long leastSigBits = buffer.getLong(); |
| 131 | + return new UUID(mostSigBits, leastSigBits); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments