Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API improvements #3

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=dev.ultreon
archivesBaseName=ubo
version=1.5.0
version=1.6.0
16 changes: 14 additions & 2 deletions src/main/java/dev/ultreon/ubo/DataTypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class DataTypeRegistry {
private static final Map<String, Integer> ID_MAP = new HashMap<>();

static {
init();
}

public static void init() {
register(DataTypes.BYTE, ByteType::read);
register(DataTypes.SHORT, ShortType::read);
register(DataTypes.INT, IntType::read);
Expand All @@ -37,6 +41,12 @@ public class DataTypeRegistry {
register(DataTypes.BIT_SET, BitSetType::read);
}

public static void clear() {
READERS.clear();
TYPES.clear();
ID_MAP.clear();
}

@SafeVarargs
@SuppressWarnings("unchecked")
public static <T extends DataType<?>> void register(int id, DataReader<T> reader, T... type) {
Expand All @@ -47,10 +57,12 @@ public static <T extends DataType<?>> void register(int id, DataReader<T> reader
}

public static DataType<?> read(int id, DataInput input) throws IOException {
if (!READERS.containsKey(id))
DataReader<? extends DataType<?>> reader = READERS.get(id);

if (reader == null)
throw new DataTypeException("Unknown datatype id: " + id);

return READERS.get(id).read(input);
return reader.read(input);
}

public static Class<? extends DataType<?>> getType(int id) {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/dev/ultreon/ubo/UsoParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
import java.util.UUID;

public class UsoParser {
private final String input;
private final char[] chars;
private int pos;
private char c;

public UsoParser(String input) {
this.input = input;
this.chars = input.toCharArray();
}

Expand Down Expand Up @@ -609,15 +606,15 @@ private int unread() {
return -1;
}

return this.c = this.chars[--this.pos];
return this.chars[--this.pos];
}

private int read() {
if (this.pos >= this.chars.length) {
return -1;
}

return this.c = this.chars[this.pos++];
return this.chars[this.pos++];
}

public DataType<?> parse() throws IOException {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/dev/ultreon/ubo/types/ArrayType.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.ultreon.ubo.types;

import org.jetbrains.annotations.NotNull;

public interface ArrayType<T, B> extends DataType<T>, Iterable<B> {
int size();

default boolean isEmpty() {
return size() == 0;
}

B get(int index);

void set(int index, B value);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/ultreon/ubo/types/BitSetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public int id() {

@Override
public void write(DataOutput output) throws IOException {
byte[] arr = this.obj.toByteArray();
if (arr.length >= 32768) throw new IllegalArgumentException("Bitset is too big to be written");
byte[] arr = obj.toByteArray();
if (arr.length > 65535) throw new IllegalArgumentException("Bitset is too big to be written");
output.writeShort(arr.length);
for (byte b : arr) {
output.writeByte(b);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/BooleanType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ public Boolean getValue() {
return obj;
}

public boolean getBooleanValue() {
return obj;
}

@Override
public void setValue(Boolean obj) {
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
this.obj = obj;
}

public void setValue(boolean val) {
this.obj = val;
}

@Override
public int id() {
return DataTypes.BOOLEAN;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/ByteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ public Byte getValue() {
return obj;
}

public byte getByteValue() {
return obj;
}

@Override
public void setValue(Byte obj) {
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
this.obj = obj;
}

public void setValue(byte val) {
this.obj = val;
}

@Override
public int id() {
return DataTypes.BYTE;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/CharType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ public Character getValue() {
return obj;
}

public char getCharValue() {
return obj;
}

@Override
public void setValue(Character obj) {
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
this.obj = obj;
}

public void setValue(char val) {
this.obj = val;
}

@Override
public int id() {
return DataTypes.CHAR;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public interface DataType<T> {

void write(DataOutput output) throws IOException;

@Override
boolean equals(Object other);

@Override
int hashCode();

DataType<T> copy();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/DoubleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ public Double getValue() {
return obj;
}

public double getDoubleValue() {
return obj;
}

@Override
public void setValue(Double obj) {
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
this.obj = obj;
}

public void setValue(double val) {
this.obj = val;
}

@Override
public int id() {
return DataTypes.DOUBLE;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/FloatArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public void set(int index, Float value) {
obj[index] = value;
}

public float getFloat(int index) {
return obj[index];
}

public void set(int index, float value) {
obj[index] = value;
}

@Override
public String writeUso() {
StringBuilder builder = new StringBuilder("(f;");
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/FloatType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ public Float getValue() {
return obj;
}

public float getFloatValue() {
return obj;
}

@Override
public void setValue(Float obj) {
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
this.obj = obj;
}

public void setValue(float val) {
this.obj = val;
}

@Override
public int id() {
return DataTypes.FLOAT;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/IntArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public IntArrayType(int[] obj) {
this.obj = obj;
}

public IntArrayType(int size) {
this.obj = new int[size];
}

@Override
public int[] getValue() {
return obj;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/IntType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ public Integer getValue() {
return obj;
}

public int getIntValue() {
return obj;
}

@Override
public void setValue(Integer obj) {
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
this.obj = obj;
}

public void setValue(int val) {
this.obj = val;
}

@Override
public int id() {
return DataTypes.INT;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/dev/ultreon/ubo/types/ListType.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public List<T> getValue() {
@Override
public void setValue(List<T> obj) {
int id = -1;
List<T> list = new ArrayList<>();
List<T> list = new ArrayList<>(obj.size());
for (int i = 0, objSize = obj.size(); i < objSize; i++) {
T iType = obj.get(i);
if (id == -1) {
Expand Down Expand Up @@ -106,7 +106,11 @@ public void add(T type) {

@Override
public @NotNull Iterator<T> iterator() {
return getValue().listIterator();
return obj.iterator();
}

public @NotNull ListIterator<T> listIterator() {
return obj.listIterator();
}

public int type() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/LongArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public LongArrayType(long[] obj) {
this.obj = obj;
}

public LongArrayType(int size) {
this.obj = new long[size];
}

@Override
public long[] getValue() {
return obj;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/LongType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ public Long getValue() {
return obj;
}

public long getLongValue() {
return obj;
}

@Override
public void setValue(Long obj) {
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
this.obj = obj;
}

public void setValue(long val) {
this.obj = val;
}

@Override
public int id() {
return DataTypes.LONG;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/dev/ultreon/ubo/types/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public byte getByte(String key) {
public byte getByte(String key, byte def) {
DataType<?> dataType = get(key);
if (dataType instanceof ByteType) {
return ((ByteType) dataType).getValue();
return ((ByteType) dataType).getByteValue();
}
return def;
}
Expand All @@ -209,7 +209,7 @@ public short getShort(String key) {
public short getShort(String key, short def) {
DataType<?> dataType = get(key);
if (dataType instanceof ShortType) {
return ((ShortType) dataType).getValue();
return ((ShortType) dataType).getShortValue();
}
return def;
}
Expand All @@ -221,7 +221,7 @@ public int getInt(String key) {
public int getInt(String key, int def) {
DataType<?> dataType = get(key);
if (dataType instanceof IntType) {
return ((IntType) dataType).getValue();
return ((IntType) dataType).getIntValue();
}
return def;
}
Expand All @@ -233,7 +233,7 @@ public long getLong(String key) {
public long getLong(String key, long def) {
DataType<?> dataType = get(key);
if (dataType instanceof LongType) {
return ((LongType) dataType).getValue();
return ((LongType) dataType).getLongValue();
}
return def;
}
Expand All @@ -257,7 +257,7 @@ public float getFloat(String key) {
public float getFloat(String key, float def) {
DataType<?> dataType = get(key);
if (dataType instanceof FloatType) {
return ((FloatType) dataType).getValue();
return ((FloatType) dataType).getFloatValue();
}
return def;
}
Expand All @@ -269,7 +269,7 @@ public double getDouble(String key) {
public double getDouble(String key, double def) {
DataType<?> dataType = get(key);
if (dataType instanceof DoubleType) {
return ((DoubleType) dataType).getValue();
return ((DoubleType) dataType).getDoubleValue();
}
return def;
}
Expand All @@ -293,7 +293,7 @@ public char getChar(String key) {
public char getChar(String key, char def) {
DataType<?> dataType = get(key);
if (dataType instanceof CharType) {
return ((CharType) dataType).getValue();
return ((CharType) dataType).getCharValue();
}
return def;
}
Expand All @@ -305,7 +305,7 @@ public boolean getBoolean(String key) {
public boolean getBoolean(String key, boolean def) {
DataType<?> dataType = get(key);
if (dataType instanceof BooleanType) {
return ((BooleanType) dataType).getValue();
return ((BooleanType) dataType).getBooleanValue();
}
return def;
}
Expand Down
Loading