Skip to content

Commit cd39881

Browse files
committed
API improvements
1 parent cba5b6d commit cd39881

13 files changed

+446
-9
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.4.0
3+
version=1.5.0

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5-
public interface ArrayType<T> extends DataType<T> {
5+
public interface ArrayType<T, B> extends DataType<T>, Iterable<B> {
66
int size();
7+
8+
B get(int index);
9+
10+
void set(int index, B value);
711
}

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

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.ultreon.ubo.types;
22

33
import dev.ultreon.ubo.DataTypes;
4+
import org.jetbrains.annotations.NotNull;
45

56
import java.io.DataInput;
67
import java.io.DataOutput;
@@ -9,8 +10,9 @@
910
import java.nio.charset.Charset;
1011
import java.nio.charset.StandardCharsets;
1112
import java.util.Arrays;
13+
import java.util.Iterator;
1214

13-
public class ByteArrayType implements ArrayType<byte[]> {
15+
public class ByteArrayType implements ArrayType<byte[], Byte> {
1416
private byte[] obj;
1517

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

111+
@Override
109112
public int size() {
110113
return obj.length;
111114
}
112115

116+
@Override
117+
public @NotNull Byte get(int index) {
118+
return obj[index];
119+
}
120+
121+
@Override
122+
public void set(int index, Byte value) {
123+
obj[index] = value;
124+
}
125+
126+
public byte getByte(int index) {
127+
return obj[index];
128+
}
129+
130+
public void set(int index, byte value) {
131+
obj[index] = value;
132+
}
133+
113134
@Override
114135
public String toString() {
115136
return writeUso();
116137
}
138+
139+
@Override
140+
public @NotNull ByteIterator iterator() {
141+
return new ByteIterator(obj);
142+
}
143+
144+
public static class ByteIterator implements Iterator<@NotNull Byte> {
145+
private final byte[] obj;
146+
private int index;
147+
148+
public ByteIterator(byte[] obj) {
149+
this.obj = obj;
150+
}
151+
152+
@Override
153+
public boolean hasNext() {
154+
return index < obj.length;
155+
}
156+
157+
@Override
158+
@Deprecated
159+
public Byte next() {
160+
return obj[index++];
161+
}
162+
163+
public byte nextByte() {
164+
return obj[index++];
165+
}
166+
}
117167
}

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

+54-1
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
package dev.ultreon.ubo.types;
33

44
import dev.ultreon.ubo.DataTypes;
5+
import org.jetbrains.annotations.NotNull;
56

67
import java.io.DataInput;
78
import java.io.DataOutput;
89
import java.io.IOException;
910
import java.util.Arrays;
11+
import java.util.Iterator;
1012

11-
public class CharArrayType implements ArrayType<char[]> {
13+
public class CharArrayType implements ArrayType<char[], Character> {
1214
private char[] obj;
1315

1416
public CharArrayType(char[] obj) {
1517
this.obj = obj;
1618
}
1719

20+
public CharArrayType(int length) {
21+
this.obj = new char[length];
22+
}
23+
1824
@Override
1925
public char[] getValue() {
2026
return obj;
@@ -76,12 +82,59 @@ public String writeUso() {
7682
return builder.substring(0, builder.length() - 1) + ")";
7783
}
7884

85+
@Override
7986
public int size() {
8087
return obj.length;
8188
}
8289

90+
@Override
91+
public @NotNull Character get(int index) {
92+
return obj[index];
93+
}
94+
95+
@Override
96+
public void set(int index, Character value) {
97+
obj[index] = value;
98+
}
99+
100+
public char getChar(int index) {
101+
return obj[index];
102+
}
103+
104+
public void set(int index, char value) {
105+
obj[index] = value;
106+
}
107+
83108
@Override
84109
public String toString() {
85110
return writeUso();
86111
}
112+
113+
@Override
114+
public @NotNull Iterator<Character> iterator() {
115+
return new CharIterator(obj);
116+
}
117+
118+
public static class CharIterator implements Iterator<Character> {
119+
private final char[] obj;
120+
private int index;
121+
122+
public CharIterator(char[] obj) {
123+
this.obj = obj;
124+
}
125+
126+
@Override
127+
public boolean hasNext() {
128+
return index < obj.length;
129+
}
130+
131+
@Override
132+
public Character next() {
133+
return obj[index++];
134+
}
135+
136+
public char nextChar() {
137+
return obj[index++];
138+
}
139+
}
87140
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.DataOutput;
66
import java.io.IOException;
7+
import java.util.function.Function;
78

89
public interface DataType<T> {
910
T getValue();
@@ -25,4 +26,20 @@ public interface DataType<T> {
2526
default <R> R accept(DataTypeVisitor<R> visitor) {
2627
return visitor.visit(this);
2728
}
29+
30+
default <R> R map(Function<DataType<T>, R> mapper) {
31+
return mapper.apply(this);
32+
}
33+
34+
default <R extends DataType<?>> R cast(Class<R> type, R def) {
35+
if (this.equals(def)) {
36+
return def;
37+
}
38+
39+
if (type.isInstance(this)) {
40+
return type.cast(this);
41+
}
42+
43+
return def;
44+
}
2845
}

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

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package dev.ultreon.ubo.types;
22

33
import dev.ultreon.ubo.DataTypes;
4+
import org.jetbrains.annotations.NotNull;
45

56
import java.io.DataInput;
67
import java.io.DataOutput;
78
import java.io.IOException;
89
import java.util.Arrays;
10+
import java.util.Iterator;
911

10-
public class DoubleArrayType implements ArrayType<double[]> {
12+
public class DoubleArrayType implements ArrayType<double[], Double> {
1113
private double[] obj;
1214

1315
public DoubleArrayType(double[] obj) {
1416
this.obj = obj;
1517
}
1618

19+
public DoubleArrayType(int size) {
20+
this.obj = new double[size];
21+
}
22+
1723
@Override
1824
public double[] getValue() {
1925
return obj;
@@ -65,10 +71,29 @@ public DoubleArrayType copy() {
6571
return new DoubleArrayType(obj.clone());
6672
}
6773

74+
@Override
6875
public int size() {
6976
return obj.length;
7077
}
7178

79+
@Override
80+
public Double get(int index) {
81+
return obj[index];
82+
}
83+
84+
@Override
85+
public void set(int index, Double value) {
86+
obj[index] = value;
87+
}
88+
89+
public double getDouble(int index) {
90+
return obj[index];
91+
}
92+
93+
public void set(int index, double value) {
94+
obj[index] = value;
95+
}
96+
7297
@Override
7398
public String writeUso() {
7499
StringBuilder builder = new StringBuilder("(d;");
@@ -87,4 +112,32 @@ public String writeUso() {
87112
public String toString() {
88113
return writeUso();
89114
}
115+
116+
@Override
117+
public @NotNull Iterator<Double> iterator() {
118+
return new DoubleIterator(obj);
119+
}
120+
121+
public static class DoubleIterator implements Iterator<Double> {
122+
private final double[] obj;
123+
private int index;
124+
125+
public DoubleIterator(double[] obj) {
126+
this.obj = obj;
127+
}
128+
129+
@Override
130+
public boolean hasNext() {
131+
return index < obj.length;
132+
}
133+
134+
@Override
135+
public Double next() {
136+
return obj[index++];
137+
}
138+
139+
public double nextDouble() {
140+
return obj[index++];
141+
}
142+
}
90143
}

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

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package dev.ultreon.ubo.types;
22

33
import dev.ultreon.ubo.DataTypes;
4+
import org.jetbrains.annotations.NotNull;
45

56
import java.io.DataInput;
67
import java.io.DataOutput;
78
import java.io.IOException;
89
import java.util.Arrays;
10+
import java.util.Iterator;
911

10-
public class FloatArrayType implements ArrayType<float[]> {
12+
public class FloatArrayType implements ArrayType<float[], Float> {
1113
private float[] obj;
1214

1315
public FloatArrayType(float[] obj) {
1416
this.obj = obj;
1517
}
1618

19+
public FloatArrayType(int size) {
20+
this.obj = new float[size];
21+
}
22+
1723
@Override
1824
public float[] getValue() {
1925
return obj;
@@ -65,10 +71,21 @@ public FloatArrayType copy() {
6571
return new FloatArrayType(obj.clone());
6672
}
6773

74+
@Override
6875
public int size() {
6976
return obj.length;
7077
}
7178

79+
@Override
80+
public Float get(int index) {
81+
return obj[index];
82+
}
83+
84+
@Override
85+
public void set(int index, Float value) {
86+
obj[index] = value;
87+
}
88+
7289
@Override
7390
public String writeUso() {
7491
StringBuilder builder = new StringBuilder("(f;");
@@ -87,4 +104,33 @@ public String writeUso() {
87104
public String toString() {
88105
return writeUso();
89106
}
107+
108+
@Override
109+
public @NotNull Iterator<Float> iterator() {
110+
return new FloatIterator(obj);
111+
}
112+
113+
public static class FloatIterator implements Iterator<Float> {
114+
private final float[] obj;
115+
private int index;
116+
117+
public FloatIterator(float[] obj) {
118+
this.obj = obj;
119+
}
120+
121+
@Override
122+
public boolean hasNext() {
123+
return index < obj.length;
124+
}
125+
126+
@Override
127+
@Deprecated
128+
public Float next() {
129+
return obj[index++];
130+
}
131+
132+
public float nextFloat() {
133+
return obj[index++];
134+
}
135+
}
90136
}

0 commit comments

Comments
 (0)