Skip to content

Commit

Permalink
API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Oct 26, 2024
1 parent cba5b6d commit cd39881
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 9 deletions.
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.4.0
version=1.5.0
6 changes: 5 additions & 1 deletion src/main/java/dev/ultreon/ubo/types/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import org.jetbrains.annotations.NotNull;

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

B get(int index);

void set(int index, B value);
}
52 changes: 51 additions & 1 deletion src/main/java/dev/ultreon/ubo/types/ByteArrayType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.ultreon.ubo.types;

import dev.ultreon.ubo.DataTypes;
import org.jetbrains.annotations.NotNull;

import java.io.DataInput;
import java.io.DataOutput;
Expand All @@ -9,8 +10,9 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;

public class ByteArrayType implements ArrayType<byte[]> {
public class ByteArrayType implements ArrayType<byte[], Byte> {
private byte[] obj;

public ByteArrayType(byte[] obj) {
Expand Down Expand Up @@ -106,12 +108,60 @@ public String writeUso() {
return builder.substring(0, builder.length() - 1) + ")";
}

@Override
public int size() {
return obj.length;
}

@Override
public @NotNull Byte get(int index) {
return obj[index];
}

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

public byte getByte(int index) {
return obj[index];
}

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

@Override
public String toString() {
return writeUso();
}

@Override
public @NotNull ByteIterator iterator() {
return new ByteIterator(obj);
}

public static class ByteIterator implements Iterator<@NotNull Byte> {
private final byte[] obj;
private int index;

public ByteIterator(byte[] obj) {
this.obj = obj;
}

@Override
public boolean hasNext() {
return index < obj.length;
}

@Override
@Deprecated
public Byte next() {
return obj[index++];
}

public byte nextByte() {
return obj[index++];
}
}
}
55 changes: 54 additions & 1 deletion src/main/java/dev/ultreon/ubo/types/CharArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
package dev.ultreon.ubo.types;

import dev.ultreon.ubo.DataTypes;
import org.jetbrains.annotations.NotNull;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;

public class CharArrayType implements ArrayType<char[]> {
public class CharArrayType implements ArrayType<char[], Character> {
private char[] obj;

public CharArrayType(char[] obj) {
this.obj = obj;
}

public CharArrayType(int length) {
this.obj = new char[length];
}

@Override
public char[] getValue() {
return obj;
Expand Down Expand Up @@ -76,12 +82,59 @@ public String writeUso() {
return builder.substring(0, builder.length() - 1) + ")";
}

@Override
public int size() {
return obj.length;
}

@Override
public @NotNull Character get(int index) {
return obj[index];
}

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

public char getChar(int index) {
return obj[index];
}

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

@Override
public String toString() {
return writeUso();
}

@Override
public @NotNull Iterator<Character> iterator() {
return new CharIterator(obj);
}

public static class CharIterator implements Iterator<Character> {
private final char[] obj;
private int index;

public CharIterator(char[] obj) {
this.obj = obj;
}

@Override
public boolean hasNext() {
return index < obj.length;
}

@Override
public Character next() {
return obj[index++];
}

public char nextChar() {
return obj[index++];
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/dev/ultreon/ubo/types/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.DataOutput;
import java.io.IOException;
import java.util.function.Function;

public interface DataType<T> {
T getValue();
Expand All @@ -25,4 +26,20 @@ public interface DataType<T> {
default <R> R accept(DataTypeVisitor<R> visitor) {
return visitor.visit(this);
}

default <R> R map(Function<DataType<T>, R> mapper) {
return mapper.apply(this);
}

default <R extends DataType<?>> R cast(Class<R> type, R def) {
if (this.equals(def)) {
return def;
}

if (type.isInstance(this)) {
return type.cast(this);
}

return def;
}
}
55 changes: 54 additions & 1 deletion src/main/java/dev/ultreon/ubo/types/DoubleArrayType.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package dev.ultreon.ubo.types;

import dev.ultreon.ubo.DataTypes;
import org.jetbrains.annotations.NotNull;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;

public class DoubleArrayType implements ArrayType<double[]> {
public class DoubleArrayType implements ArrayType<double[], Double> {
private double[] obj;

public DoubleArrayType(double[] obj) {
this.obj = obj;
}

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

@Override
public double[] getValue() {
return obj;
Expand Down Expand Up @@ -65,10 +71,29 @@ public DoubleArrayType copy() {
return new DoubleArrayType(obj.clone());
}

@Override
public int size() {
return obj.length;
}

@Override
public Double get(int index) {
return obj[index];
}

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

public double getDouble(int index) {
return obj[index];
}

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

@Override
public String writeUso() {
StringBuilder builder = new StringBuilder("(d;");
Expand All @@ -87,4 +112,32 @@ public String writeUso() {
public String toString() {
return writeUso();
}

@Override
public @NotNull Iterator<Double> iterator() {
return new DoubleIterator(obj);
}

public static class DoubleIterator implements Iterator<Double> {
private final double[] obj;
private int index;

public DoubleIterator(double[] obj) {
this.obj = obj;
}

@Override
public boolean hasNext() {
return index < obj.length;
}

@Override
public Double next() {
return obj[index++];
}

public double nextDouble() {
return obj[index++];
}
}
}
48 changes: 47 additions & 1 deletion src/main/java/dev/ultreon/ubo/types/FloatArrayType.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package dev.ultreon.ubo.types;

import dev.ultreon.ubo.DataTypes;
import org.jetbrains.annotations.NotNull;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;

public class FloatArrayType implements ArrayType<float[]> {
public class FloatArrayType implements ArrayType<float[], Float> {
private float[] obj;

public FloatArrayType(float[] obj) {
this.obj = obj;
}

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

@Override
public float[] getValue() {
return obj;
Expand Down Expand Up @@ -65,10 +71,21 @@ public FloatArrayType copy() {
return new FloatArrayType(obj.clone());
}

@Override
public int size() {
return obj.length;
}

@Override
public Float get(int index) {
return obj[index];
}

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

@Override
public String writeUso() {
StringBuilder builder = new StringBuilder("(f;");
Expand All @@ -87,4 +104,33 @@ public String writeUso() {
public String toString() {
return writeUso();
}

@Override
public @NotNull Iterator<Float> iterator() {
return new FloatIterator(obj);
}

public static class FloatIterator implements Iterator<Float> {
private final float[] obj;
private int index;

public FloatIterator(float[] obj) {
this.obj = obj;
}

@Override
public boolean hasNext() {
return index < obj.length;
}

@Override
@Deprecated
public Float next() {
return obj[index++];
}

public float nextFloat() {
return obj[index++];
}
}
}
Loading

0 comments on commit cd39881

Please sign in to comment.