Skip to content

Commit 29fb789

Browse files
committed
Refactor ArrayAccessor constructor and add newArray method
1 parent c84690c commit 29fb789

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

‎src/main/java/org/glavo/nbt/internal/ArrayAccessor.java‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public static int nextCapacity(int currentCapacity, int minCapacity) {
3939
return Math.max(growCap, minCapacity);
4040
}
4141

42-
private final Class<?> primitiveClass;
4342
private final Class<A> arrayClass;
43+
private final Class<?> primitiveClass;
4444
protected final A empty;
4545

46-
private ArrayAccessor(Class<?> primitiveClass, Class<A> arrayClass, A empty) {
47-
this.primitiveClass = primitiveClass;
46+
private ArrayAccessor(Class<A> arrayClass, A empty) {
4847
this.arrayClass = arrayClass;
48+
this.primitiveClass = arrayClass.getComponentType();
4949
this.empty = empty;
5050
}
5151

@@ -64,6 +64,8 @@ public final Class<A> getArrayClass() {
6464

6565
public abstract A copyOf(A array, int newLength);
6666

67+
public abstract A newArray(int newLength);
68+
6769
public final int getLength(A array) {
6870
return Array.getLength(array);
6971
}
@@ -86,7 +88,12 @@ public final int getLength(A array) {
8688

8789
public abstract T newTagFromElement(A array, int index);
8890

89-
public static final ArrayAccessor<Byte, ByteTag, byte[], ByteBuffer> BYTE_ARRAY = new ArrayAccessor<>(byte.class, byte[].class, new byte[0]) {
91+
public static final ArrayAccessor<Byte, ByteTag, byte[], ByteBuffer> BYTE_ARRAY = new ArrayAccessor<>(byte[].class, new byte[0]) {
92+
@Override
93+
public byte[] newArray(int newLength) {
94+
return new byte[newLength];
95+
}
96+
9097
@Override
9198
public byte[] copyOf(byte[] array, int newLength) {
9299
return Arrays.copyOf(array, newLength);
@@ -157,7 +164,12 @@ public ByteTag newTagFromElement(byte[] array, int index) {
157164
}
158165
};
159166

160-
public static final ArrayAccessor<Integer, IntTag, int[], IntBuffer> INT_ARRAY = new ArrayAccessor<>(int.class, int[].class, new int[0]) {
167+
public static final ArrayAccessor<Integer, IntTag, int[], IntBuffer> INT_ARRAY = new ArrayAccessor<>(int[].class, new int[0]) {
168+
169+
@Override
170+
public int[] newArray(int newLength) {
171+
return new int[newLength];
172+
}
161173

162174
@Override
163175
public int[] copyOf(int[] array, int newLength) {
@@ -229,7 +241,12 @@ public IntTag newTagFromElement(int[] array, int index) {
229241
}
230242
};
231243

232-
public static final ArrayAccessor<Long, LongTag, long[], LongBuffer> LONG_ARRAY = new ArrayAccessor<>(long.class, long[].class, new long[0]) {
244+
public static final ArrayAccessor<Long, LongTag, long[], LongBuffer> LONG_ARRAY = new ArrayAccessor<>(long[].class, new long[0]) {
245+
246+
@Override
247+
public long[] newArray(int newLength) {
248+
return new long[newLength];
249+
}
233250

234251
@Override
235252
public long[] copyOf(long[] array, int newLength) {

‎src/test/java/org/glavo/nbt/tag/ArrayTagTests.java‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.nio.ByteBuffer;
2323
import java.nio.IntBuffer;
2424
import java.nio.LongBuffer;
25-
import java.util.Objects;
2625
import java.util.Random;
2726

2827
import static org.junit.jupiter.api.Assertions.*;
@@ -36,14 +35,30 @@ abstract class ArrayTagTests<AT extends ArrayTag<E, T, A, B>, E extends Number,
3635

3736
abstract AT create(String name, A array);
3837

38+
final A newArray(int size) {
39+
return accessor().newArray(size);
40+
}
41+
42+
final int getLength(A array) {
43+
return accessor().getLength(array);
44+
}
45+
46+
final E get(A array, int index) {
47+
return accessor().get(array, index);
48+
}
49+
50+
final void set(A array, int index, E value) {
51+
accessor().set(array, index, value);
52+
}
53+
3954
abstract A randomArray(Random random, int size);
4055

4156
private void assertArrayEquals(A expected, A actual) {
42-
if (accessor().getLength(expected) != accessor().getLength(actual)) {
43-
throw new AssertionError("Array lengths differ: " + accessor().getLength(expected) + " != " + accessor().getLength(actual));
57+
if (getLength(expected) != getLength(actual)) {
58+
throw new AssertionError("Array lengths differ: " + getLength(expected) + " != " + getLength(actual));
4459
}
4560

46-
for (int i = 0, end = accessor().getLength(expected); i < end; i++) {
61+
for (int i = 0, end = getLength(expected); i < end; i++) {
4762
if (!accessor().get(expected, i).equals(accessor().get(actual, i))) {
4863
throw new AssertionError("Array contents differ at index " + i + ": " + accessor().get(expected, i) + " != " + accessor().get(actual, i));
4964
}
@@ -55,17 +70,17 @@ void testConstructor() {
5570
AT tag = create();
5671
assertEquals("", tag.getName());
5772
assertEquals(0, tag.size());
58-
assertEquals(0, accessor().getLength(tag.getArray()));
73+
assertEquals(0, getLength(tag.getArray()));
5974

6075
tag = create("test");
6176
assertEquals("test", tag.getName());
6277
assertEquals(0, tag.size());
63-
assertEquals(0, accessor().getLength(tag.getArray()));
78+
assertEquals(0, getLength(tag.getArray()));
6479

6580
tag = create("test", accessor().empty());
6681
assertEquals("test", tag.getName());
6782
assertEquals(0, tag.size());
68-
assertEquals(0, accessor().getLength(tag.getArray()));
83+
assertEquals(0, getLength(tag.getArray()));
6984

7085
Random random = new Random(0);
7186
A array = randomArray(random, 10);

0 commit comments

Comments
 (0)