Skip to content

Commit

Permalink
✨ General improvements (slight breaking change)
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Nov 10, 2023
1 parent 37efee9 commit f18a35f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ bin/
.DS_Store
/map-compressed.ubo
/map-normal.ubo
/list-compressed.ubo
/list-normal.ubo
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ Assuming you use gradle.

~~Here's the [jitpack listing](https://jitpack.io/#Ultreon/ultreon-data) for the current versions and builds you can use.~~ (Temporarily not used)
Check out the releases page for latest releases.
## Naming conventions
The following conventions are for map keys:
* `MapType` and `ListType` are in `DromedaryCase`.
* Any other types are in `camelCase`.
Do note that in some cases keys can have a different case.
For example if the key is used for identifiers. (Like those: `namespace:id`)
Tho it's generally not recommended to use map keys for objects like identifiers or numbers.
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/DataIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static <T extends IType<?>> T read(InputStream stream, T... type) throws

Class<T> componentType = (Class<T>) type.getClass().getComponentType();
int componentId = TypeRegistry.getId(componentType);
int id = inputStream.readByte();
int id = inputStream.readUnsignedByte();

if (componentId != id) {
throw new DataTypeException("The read data id " + id + " is different from the expected id: " + componentId);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/ultreon/data/types/BitSetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ public int id() {
@Override
public void write(DataOutputStream stream) throws IOException {
byte[] arr = this.obj.toByteArray();
if (arr.length >= 32768) throw new IllegalArgumentException("Bitset is too big to be written");
stream.writeShort(arr.length);
for (byte b : arr) {
stream.writeByte(b);
}
}

public static BitSetType read(DataInputStream stream) throws IOException {
int len = stream.readUnsignedShort();
int len = stream.readShort();
byte[] arr = new byte[len];
for (int i = 0; i < len; i++) {
arr[i] = stream.readByte();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ultreon/data/types/ListType.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void write(DataOutputStream stream) throws IOException {
}

public static ListType<?> read(DataInputStream stream) throws IOException {
byte id = stream.readByte();
int id = stream.readUnsignedByte();
int len = stream.readInt();
List<IType<?>> list = new ArrayList<>();
for (int i = 0; i < len; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ultreon/data/types/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static MapType read(DataInputStream stream) throws IOException {
bytes[j] = stream.readByte();
}
String key = new String(bytes, StandardCharsets.UTF_8);
byte id = stream.readByte();
int id = stream.readUnsignedByte();
map.put(key, TypeRegistry.read(id, stream));
}

Expand Down Expand Up @@ -248,7 +248,7 @@ public BigInteger getBigInt(String key, BigInteger def) {
}

public float getFloat(String key) {
return getFloat(key, (float) 0);
return getFloat(key, 0);
}

public float getFloat(String key, float def) {
Expand Down
76 changes: 65 additions & 11 deletions src/test/java/com/ultreon/tests/data/ReadWriteTests.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
package com.ultreon.tests.data;

import com.ultreon.data.DataIo;
import com.ultreon.data.Types;
import com.ultreon.data.types.ListType;
import com.ultreon.data.types.MapType;
import com.ultreon.data.types.StringType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.BitSet;
import java.util.UUID;

public class ReadWriteTests {
class ReadWriteTests {
@Test
@DisplayName("Write -> Map")
void writeMap() {
@DisplayName("MapTypes")
void readWriteMap() {
BitSet bitSet = new BitSet(16);
bitSet.set(0);
bitSet.set(2);
bitSet.set(5);
bitSet.set(8);
bitSet.set(12);
bitSet.set(14);
bitSet.set(15);

MapType type = new MapType();
type.putInt("integer", 123456789);
type.putString("string", "Hello World");
type.putByte("byte", 64);
type.putShort("short", 1024);
type.putUUID("uuid", UUID.nameUUIDFromBytes("Hello World".getBytes()));
type.putBitSet("bitSet", bitSet);
type.putDouble("double", 123456.123456789);
type.putFloat("float", 123.456f);
type.putLong("long", 7342041283402173783L);
Expand All @@ -45,7 +56,7 @@ void writeMap() {
inner.putLong("fileSize", 7_323_358_494L);
type.put("Map", inner);

ListType list = new ListType(Types.STRING);
ListType<StringType> list = new ListType<>();
list.add(new StringType("Glitch"));
list.add(new StringType("Qboi"));
list.add(new StringType("QTechCommunity"));
Expand All @@ -65,23 +76,66 @@ void writeMap() {
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Test
@DisplayName("Read -> Map")
void readMap() {
MapType readMap;
try {
System.out.println("Reading normal map data...");
MapType map = DataIo.read(new File("map-normal.ubo"));
readMap = DataIo.read(new File("map-normal.ubo"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Assertions.assertEquals(readMap, type);

MapType readCompressedMap;
try {
System.out.println("Reading compressed map data...");
MapType map = DataIo.readCompressed(new File("map-compressed.ubo"));
readCompressedMap = DataIo.readCompressed(new File("map-compressed.ubo"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Assertions.assertEquals(readCompressedMap, type);
}

@Test
@DisplayName("ListTypes")
void readWriteList() {
ListType<StringType> list = new ListType<>();
list.add(new StringType("Apple"));
list.add(new StringType("Banana"));
list.add(new StringType("Pear"));
list.add(new StringType("Orange"));
list.add(new StringType("Watermelon"));

try {
System.out.println("Writing normal list data...");
DataIo.write(list, new File("list-normal.ubo"));
} catch (IOException e) {
throw new RuntimeException(e);
}

try {
System.out.println("Writing compressed list data...");
DataIo.writeCompressed(list, new File("list-compressed.ubo"));
} catch (IOException e) {
throw new RuntimeException(e);
}

ListType<StringType> readList;
try {
System.out.println("Reading normal list data...");
readList = DataIo.read(new File("list-normal.ubo"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Assertions.assertEquals(readList, list);

ListType<StringType> readCompressedList;
try {
System.out.println("Reading compressed list data...");
readCompressedList = DataIo.readCompressed(new File("list-compressed.ubo"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Assertions.assertEquals(readCompressedList, list);
}
}

0 comments on commit f18a35f

Please sign in to comment.