diff --git a/src/main/java/gregtechmod/api/util/ListAdapter.java b/src/main/java/gregtechmod/api/util/ListAdapter.java new file mode 100644 index 0000000..b7bb401 --- /dev/null +++ b/src/main/java/gregtechmod/api/util/ListAdapter.java @@ -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 extends AbstractList { + // {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 vals) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(int index, Collection vals) { + throw new UnsupportedOperationException(); + } +} diff --git a/src/test/java/AdapterTest.java b/src/test/java/AdapterTest.java new file mode 100644 index 0000000..5f91994 --- /dev/null +++ b/src/test/java/AdapterTest.java @@ -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 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 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 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(']'); + } +}