Added array range adapter

Also tested, will work fine.
This commit is contained in:
TheDarkDnKTv 2021-02-06 06:50:15 +02:00
parent 39edf31cc1
commit 701baaed72
2 changed files with 214 additions and 0 deletions

View file

@ -0,0 +1,105 @@
package gregtechmod.api.util;
import java.util.AbstractList;
import java.util.Collection;
import java.util.Objects;
/**
* An array to list adapter with range controll function
* This adapter is used to go through array contents or replace it, not to extend and etc.
*
* @author TheDarkDnKTv
*/
public class ListAdapter<E> extends AbstractList<E> {
// {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
private E[] data; // {10, 12, 13, 14, 15, 16, 17, 18, 91, 20}
private int idxStart; // 1
private int idxEnd; // 7
public ListAdapter(E[] items) {
this(items, 0, items.length-1);
}
public ListAdapter(E[] items, int fromIdx, int toIdx) {
Objects.requireNonNull(items);
this.data = items;
this.idxStart = fromIdx;
this.idxEnd = toIdx;
}
private void rangeCheck(int index) {
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+data.length;
}
private int getIdxWithOffset(int index) {
return index + idxStart;
}
@Override
public E set(int index, E element) {
rangeCheck(index);
E old = data[getIdxWithOffset(index)];
data[getIdxWithOffset(index)] = element;
return old;
}
@Override
public E get(int index) {
rangeCheck(index);
return data[getIdxWithOffset(index)];
}
@Override
public int size() {
return idxEnd - idxStart + 1;
}
@Override
public E remove(int index) {
rangeCheck(index);
E value = data[getIdxWithOffset(index)];
data[getIdxWithOffset(index)] = null;
return value;
}
@Override
public void clear() {
for (int i = idxStart; i <= idxEnd; i++) {
data[i] = null;
}
}
@Override
public boolean isEmpty() {
for (int i = idxStart; i <= idxEnd; i++) {
if (data[i] != null) return false;
}
return true;
}
@Override
public boolean add(E item) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, E item) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends E> vals) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends E> vals) {
throw new UnsupportedOperationException();
}
}

View file

@ -0,0 +1,109 @@
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
import gregtechmod.api.util.ListAdapter;
import java.util.Arrays;
import java.util.Iterator;
import static org.junit.Assert.assertTrue;
/**
* @author TheDarkDnKTv
*
*/
public class AdapterTest {
@SuppressWarnings("deprecation")
@Rule
public ExpectedException thrown = ExpectedException.none();
public static ListAdapter<Integer> adapter;
public static Integer[] values, sub;
public AdapterTest() {
// 0 1 2 3 4 5
sub = new Integer[] { 92, 52, 64, 61, 91, 86 };
// 0 1 2 3 4 5 6 7 8 9
values = new Integer[] { 10, 72, 92, 52, 64, 61, 91, 86, 20, 48 };
adapter = new ListAdapter<>(values, 2, 7);
}
@Test
public void subarray() {
assertTrue("subarray not equal", Arrays.equals(sub, adapter.toArray()));
}
@Test
public void get() {
Integer a = adapter.get(3);
assertTrue("Get returned wrong value: " + a, a.intValue() == 61);
}
@Test
public void size() {
assertTrue("Wrong size: " + adapter.size(), adapter.size() == sub.length);
}
@Test
public void indexOf() {
int idx = adapter.indexOf(64);
assertTrue("Wrong index: " + idx, idx == 2);
}
@Test
public void contains() {
assertTrue("Not contains", adapter.contains(Integer.valueOf(52)));
}
@Test
public void containsAll() {
assertTrue("Not contains all", adapter.containsAll(Arrays.asList(sub)));
}
@Test
public void iterator() {
System.out.println("Iterator");
Iterator<Integer> iter = adapter.iterator();
while (iter.hasNext()) {
System.err.print(iter.next() + " ");
}
System.out.println();
}
@Test
public void set() {
Integer old = adapter.set(5, 228);
Integer n = adapter.get(5);
assertTrue("Set returned wrong value: " + old, old.intValue() == 86);
assertTrue("Get after set returned wrong value: " + n, n.intValue() == 228);
}
@Test
public void remove() {
Integer old = adapter.remove(2);
assertTrue("Remove returned wrong number: " + old, old.intValue() == 64);
}
@Test
public void clear() {
System.out.print("Before clening: ");
printArr(values);
adapter.clear();
System.out.print("After clening: ");
printArr(values);
assertTrue("Adapter not empty", adapter.isEmpty());
}
private <T> void printArr(T[] arr) {
System.out.print('[');
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i != arr.length-1) System.out.print(", ");
}
System.out.println(']');
}
}