Skip to content

Commit 7b3833f

Browse files
authored
Merge pull request #3 from AndEditor7/improvements
* API improvements * Add list iterator
1 parent 6d51bc1 commit 7b3833f

22 files changed

+151
-24
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
group=dev.ultreon
22
archivesBaseName=ubo
3-
version=1.5.0
3+
version=1.6.0

src/main/java/dev/ultreon/ubo/DataTypeRegistry.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class DataTypeRegistry {
1313
private static final Map<String, Integer> ID_MAP = new HashMap<>();
1414

1515
static {
16+
init();
17+
}
18+
19+
public static void init() {
1620
register(DataTypes.BYTE, ByteType::read);
1721
register(DataTypes.SHORT, ShortType::read);
1822
register(DataTypes.INT, IntType::read);
@@ -37,6 +41,12 @@ public class DataTypeRegistry {
3741
register(DataTypes.BIT_SET, BitSetType::read);
3842
}
3943

44+
public static void clear() {
45+
READERS.clear();
46+
TYPES.clear();
47+
ID_MAP.clear();
48+
}
49+
4050
@SafeVarargs
4151
@SuppressWarnings("unchecked")
4252
public static <T extends DataType<?>> void register(int id, DataReader<T> reader, T... type) {
@@ -47,10 +57,12 @@ public static <T extends DataType<?>> void register(int id, DataReader<T> reader
4757
}
4858

4959
public static DataType<?> read(int id, DataInput input) throws IOException {
50-
if (!READERS.containsKey(id))
60+
DataReader<? extends DataType<?>> reader = READERS.get(id);
61+
62+
if (reader == null)
5163
throw new DataTypeException("Unknown datatype id: " + id);
5264

53-
return READERS.get(id).read(input);
65+
return reader.read(input);
5466
}
5567

5668
public static Class<? extends DataType<?>> getType(int id) {

src/main/java/dev/ultreon/ubo/UsoParser.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
import java.util.UUID;
1111

1212
public class UsoParser {
13-
private final String input;
1413
private final char[] chars;
1514
private int pos;
16-
private char c;
1715

1816
public UsoParser(String input) {
19-
this.input = input;
2017
this.chars = input.toCharArray();
2118
}
2219

@@ -609,15 +606,15 @@ private int unread() {
609606
return -1;
610607
}
611608

612-
return this.c = this.chars[--this.pos];
609+
return this.chars[--this.pos];
613610
}
614611

615612
private int read() {
616613
if (this.pos >= this.chars.length) {
617614
return -1;
618615
}
619616

620-
return this.c = this.chars[this.pos++];
617+
return this.chars[this.pos++];
621618
}
622619

623620
public DataType<?> parse() throws IOException {

src/main/java/dev/ultreon/ubo/types/ArrayType.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package dev.ultreon.ubo.types;
22

3-
import org.jetbrains.annotations.NotNull;
4-
53
public interface ArrayType<T, B> extends DataType<T>, Iterable<B> {
64
int size();
75

6+
default boolean isEmpty() {
7+
return size() == 0;
8+
}
9+
810
B get(int index);
911

1012
void set(int index, B value);

src/main/java/dev/ultreon/ubo/types/BitSetType.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public int id() {
4848

4949
@Override
5050
public void write(DataOutput output) throws IOException {
51-
byte[] arr = this.obj.toByteArray();
52-
if (arr.length >= 32768) throw new IllegalArgumentException("Bitset is too big to be written");
51+
byte[] arr = obj.toByteArray();
52+
if (arr.length > 65535) throw new IllegalArgumentException("Bitset is too big to be written");
5353
output.writeShort(arr.length);
5454
for (byte b : arr) {
5555
output.writeByte(b);

src/main/java/dev/ultreon/ubo/types/BooleanType.java

+8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public Boolean getValue() {
1818
return obj;
1919
}
2020

21+
public boolean getBooleanValue() {
22+
return obj;
23+
}
24+
2125
@Override
2226
public void setValue(Boolean obj) {
2327
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
2428
this.obj = obj;
2529
}
2630

31+
public void setValue(boolean val) {
32+
this.obj = val;
33+
}
34+
2735
@Override
2836
public int id() {
2937
return DataTypes.BOOLEAN;

src/main/java/dev/ultreon/ubo/types/ByteType.java

+8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ public Byte getValue() {
2222
return obj;
2323
}
2424

25+
public byte getByteValue() {
26+
return obj;
27+
}
28+
2529
@Override
2630
public void setValue(Byte obj) {
2731
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
2832
this.obj = obj;
2933
}
3034

35+
public void setValue(byte val) {
36+
this.obj = val;
37+
}
38+
3139
@Override
3240
public int id() {
3341
return DataTypes.BYTE;

src/main/java/dev/ultreon/ubo/types/CharType.java

+8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public Character getValue() {
1818
return obj;
1919
}
2020

21+
public char getCharValue() {
22+
return obj;
23+
}
24+
2125
@Override
2226
public void setValue(Character obj) {
2327
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
2428
this.obj = obj;
2529
}
2630

31+
public void setValue(char val) {
32+
this.obj = val;
33+
}
34+
2735
@Override
2836
public int id() {
2937
return DataTypes.CHAR;

src/main/java/dev/ultreon/ubo/types/DataType.java

+2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public interface DataType<T> {
1515

1616
void write(DataOutput output) throws IOException;
1717

18+
@Override
1819
boolean equals(Object other);
1920

21+
@Override
2022
int hashCode();
2123

2224
DataType<T> copy();

src/main/java/dev/ultreon/ubo/types/DoubleType.java

+8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public Double getValue() {
1818
return obj;
1919
}
2020

21+
public double getDoubleValue() {
22+
return obj;
23+
}
24+
2125
@Override
2226
public void setValue(Double obj) {
2327
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
2428
this.obj = obj;
2529
}
2630

31+
public void setValue(double val) {
32+
this.obj = val;
33+
}
34+
2735
@Override
2836
public int id() {
2937
return DataTypes.DOUBLE;

src/main/java/dev/ultreon/ubo/types/FloatArrayType.java

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public void set(int index, Float value) {
8686
obj[index] = value;
8787
}
8888

89+
public float getFloat(int index) {
90+
return obj[index];
91+
}
92+
93+
public void set(int index, float value) {
94+
obj[index] = value;
95+
}
96+
8997
@Override
9098
public String writeUso() {
9199
StringBuilder builder = new StringBuilder("(f;");

src/main/java/dev/ultreon/ubo/types/FloatType.java

+8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public Float getValue() {
1818
return obj;
1919
}
2020

21+
public float getFloatValue() {
22+
return obj;
23+
}
24+
2125
@Override
2226
public void setValue(Float obj) {
2327
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
2428
this.obj = obj;
2529
}
2630

31+
public void setValue(float val) {
32+
this.obj = val;
33+
}
34+
2735
@Override
2836
public int id() {
2937
return DataTypes.FLOAT;

src/main/java/dev/ultreon/ubo/types/IntArrayType.java

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public IntArrayType(int[] obj) {
1616
this.obj = obj;
1717
}
1818

19+
public IntArrayType(int size) {
20+
this.obj = new int[size];
21+
}
22+
1923
@Override
2024
public int[] getValue() {
2125
return obj;

src/main/java/dev/ultreon/ubo/types/IntType.java

+8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public Integer getValue() {
1818
return obj;
1919
}
2020

21+
public int getIntValue() {
22+
return obj;
23+
}
24+
2125
@Override
2226
public void setValue(Integer obj) {
2327
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
2428
this.obj = obj;
2529
}
2630

31+
public void setValue(int val) {
32+
this.obj = val;
33+
}
34+
2735
@Override
2836
public int id() {
2937
return DataTypes.INT;

src/main/java/dev/ultreon/ubo/types/ListType.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public List<T> getValue() {
5757
@Override
5858
public void setValue(List<T> obj) {
5959
int id = -1;
60-
List<T> list = new ArrayList<>();
60+
List<T> list = new ArrayList<>(obj.size());
6161
for (int i = 0, objSize = obj.size(); i < objSize; i++) {
6262
T iType = obj.get(i);
6363
if (id == -1) {
@@ -106,7 +106,11 @@ public void add(T type) {
106106

107107
@Override
108108
public @NotNull Iterator<T> iterator() {
109-
return getValue().listIterator();
109+
return obj.iterator();
110+
}
111+
112+
public @NotNull ListIterator<T> listIterator() {
113+
return obj.listIterator();
110114
}
111115

112116
public int type() {

src/main/java/dev/ultreon/ubo/types/LongArrayType.java

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public LongArrayType(long[] obj) {
1616
this.obj = obj;
1717
}
1818

19+
public LongArrayType(int size) {
20+
this.obj = new long[size];
21+
}
22+
1923
@Override
2024
public long[] getValue() {
2125
return obj;

src/main/java/dev/ultreon/ubo/types/LongType.java

+8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public Long getValue() {
1818
return obj;
1919
}
2020

21+
public long getLongValue() {
22+
return obj;
23+
}
24+
2125
@Override
2226
public void setValue(Long obj) {
2327
if (obj == null) throw new IllegalArgumentException("Value can't be set to null");
2428
this.obj = obj;
2529
}
2630

31+
public void setValue(long val) {
32+
this.obj = val;
33+
}
34+
2735
@Override
2836
public int id() {
2937
return DataTypes.LONG;

src/main/java/dev/ultreon/ubo/types/MapType.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public byte getByte(String key) {
197197
public byte getByte(String key, byte def) {
198198
DataType<?> dataType = get(key);
199199
if (dataType instanceof ByteType) {
200-
return ((ByteType) dataType).getValue();
200+
return ((ByteType) dataType).getByteValue();
201201
}
202202
return def;
203203
}
@@ -209,7 +209,7 @@ public short getShort(String key) {
209209
public short getShort(String key, short def) {
210210
DataType<?> dataType = get(key);
211211
if (dataType instanceof ShortType) {
212-
return ((ShortType) dataType).getValue();
212+
return ((ShortType) dataType).getShortValue();
213213
}
214214
return def;
215215
}
@@ -221,7 +221,7 @@ public int getInt(String key) {
221221
public int getInt(String key, int def) {
222222
DataType<?> dataType = get(key);
223223
if (dataType instanceof IntType) {
224-
return ((IntType) dataType).getValue();
224+
return ((IntType) dataType).getIntValue();
225225
}
226226
return def;
227227
}
@@ -233,7 +233,7 @@ public long getLong(String key) {
233233
public long getLong(String key, long def) {
234234
DataType<?> dataType = get(key);
235235
if (dataType instanceof LongType) {
236-
return ((LongType) dataType).getValue();
236+
return ((LongType) dataType).getLongValue();
237237
}
238238
return def;
239239
}
@@ -257,7 +257,7 @@ public float getFloat(String key) {
257257
public float getFloat(String key, float def) {
258258
DataType<?> dataType = get(key);
259259
if (dataType instanceof FloatType) {
260-
return ((FloatType) dataType).getValue();
260+
return ((FloatType) dataType).getFloatValue();
261261
}
262262
return def;
263263
}
@@ -269,7 +269,7 @@ public double getDouble(String key) {
269269
public double getDouble(String key, double def) {
270270
DataType<?> dataType = get(key);
271271
if (dataType instanceof DoubleType) {
272-
return ((DoubleType) dataType).getValue();
272+
return ((DoubleType) dataType).getDoubleValue();
273273
}
274274
return def;
275275
}
@@ -293,7 +293,7 @@ public char getChar(String key) {
293293
public char getChar(String key, char def) {
294294
DataType<?> dataType = get(key);
295295
if (dataType instanceof CharType) {
296-
return ((CharType) dataType).getValue();
296+
return ((CharType) dataType).getCharValue();
297297
}
298298
return def;
299299
}
@@ -305,7 +305,7 @@ public boolean getBoolean(String key) {
305305
public boolean getBoolean(String key, boolean def) {
306306
DataType<?> dataType = get(key);
307307
if (dataType instanceof BooleanType) {
308-
return ((BooleanType) dataType).getValue();
308+
return ((BooleanType) dataType).getBooleanValue();
309309
}
310310
return def;
311311
}

0 commit comments

Comments
 (0)