mirror of https://github.com/Minestom/Minestom.git
Simplify ObjectArray
This commit is contained in:
parent
3764e83a4f
commit
4ab2f43eed
|
@ -1,6 +1,10 @@
|
|||
package net.minestom.server.utils.collection;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.UnknownNullability;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Represents an array which will be resized to the highest required index.
|
||||
|
@ -13,38 +17,30 @@ public final class ObjectArray<T> {
|
|||
private int max;
|
||||
|
||||
public ObjectArray(int size) {
|
||||
this.array = allocate(size);
|
||||
//noinspection unchecked
|
||||
this.array = (T[]) new Object[size];
|
||||
}
|
||||
|
||||
public ObjectArray() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public void set(int index, T object) {
|
||||
public void set(int index, @Nullable T object) {
|
||||
T[] array = this.array;
|
||||
if (index >= array.length) {
|
||||
T[] temp = allocate(index * 2 + 1);
|
||||
System.arraycopy(array, 0, temp, 0, array.length);
|
||||
this.array = array = temp;
|
||||
final int newLength = index * 2 + 1;
|
||||
this.array = array = Arrays.copyOf(array, newLength);
|
||||
}
|
||||
array[index] = object;
|
||||
this.max = Math.max(max, index);
|
||||
}
|
||||
|
||||
public T get(int index) {
|
||||
public @UnknownNullability T get(int index) {
|
||||
final T[] array = this.array;
|
||||
return index < array.length ? array[index] : null;
|
||||
}
|
||||
|
||||
public void trim() {
|
||||
final int max = this.max;
|
||||
T[] temp = allocate(max + 1);
|
||||
System.arraycopy(array, 0, temp, 0, max + 1);
|
||||
this.array = temp;
|
||||
}
|
||||
|
||||
private static <T> T[] allocate(int length) {
|
||||
//noinspection unchecked
|
||||
return (T[]) new Object[length];
|
||||
this.array = Arrays.copyOf(array, max + 1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.minestom.server.utils.collection;
|
|||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class ObjectArrayTest {
|
||||
|
||||
|
@ -12,16 +13,24 @@ public class ObjectArrayTest {
|
|||
|
||||
array.set(50, "Hey");
|
||||
assertEquals("Hey", array.get(50));
|
||||
assertNull(array.get(49));
|
||||
assertNull(array.get(51));
|
||||
|
||||
array.set(0, "Hey2");
|
||||
assertEquals("Hey2", array.get(0));
|
||||
assertEquals("Hey", array.get(50));
|
||||
|
||||
array.trim();
|
||||
assertEquals("Hey2", array.get(0));
|
||||
assertEquals("Hey", array.get(50));
|
||||
|
||||
array.set(250, "Hey3");
|
||||
assertEquals("Hey3", array.get(250));
|
||||
assertEquals("Hey2", array.get(0));
|
||||
assertEquals("Hey", array.get(50));
|
||||
|
||||
assertNull(array.get(49));
|
||||
assertNull(array.get(251));
|
||||
assertNull(array.get(Integer.MAX_VALUE));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue