Some misc changes in InventoryHandlerList
This commit is contained in:
parent
a53b2773cd
commit
e1f40c7684
2 changed files with 41 additions and 13 deletions
|
@ -3,6 +3,7 @@ package gregtechmod.api.util;
|
|||
import java.util.AbstractList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -15,16 +16,16 @@ import gnu.trove.map.hash.TIntObjectHashMap;
|
|||
*/
|
||||
public class InventoryHandlerList<E> extends AbstractList<E> {
|
||||
|
||||
protected final TIntObjectMap<InventoryHandler<E>> indexMapping;
|
||||
protected final TIntObjectMap<List<E>> indexMapping;
|
||||
protected final Map<Integer, Integer> offsetMap;
|
||||
|
||||
@SafeVarargs
|
||||
public InventoryHandlerList(InventoryHandler<E>... initial) { // TODO varagrs check
|
||||
public InventoryHandlerList(List<E>... initial) {
|
||||
Objects.requireNonNull(initial);
|
||||
indexMapping = new TIntObjectHashMap<>();
|
||||
offsetMap = new HashMap<>();
|
||||
int offset = 0;
|
||||
for (InventoryHandler<E> list : initial) {
|
||||
for (List<E> list : initial) {
|
||||
if (list == null)
|
||||
throw new IllegalArgumentException("Input lists can not be null!");
|
||||
for (int i = offset; i < offset + list.size(); i++)
|
||||
|
@ -37,14 +38,14 @@ public class InventoryHandlerList<E> extends AbstractList<E> {
|
|||
@Override
|
||||
public E get(int index) {
|
||||
rangeCheck(index);
|
||||
InventoryHandler<E> handler = indexMapping.get(index);
|
||||
List<E> handler = indexMapping.get(index);
|
||||
return handler.get(getTrueIdx(index, handler));
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
rangeCheck(index);
|
||||
InventoryHandler<E> handler = indexMapping.get(index);
|
||||
List<E> handler = indexMapping.get(index);
|
||||
int realIdx = this.getTrueIdx(index, handler);
|
||||
E old = handler.get(realIdx);
|
||||
handler.set(realIdx, element);
|
||||
|
@ -54,7 +55,7 @@ public class InventoryHandlerList<E> extends AbstractList<E> {
|
|||
@Override
|
||||
public E remove(int index) {
|
||||
rangeCheck(index);
|
||||
InventoryHandler<E> handler = indexMapping.get(index);
|
||||
List<E> handler = indexMapping.get(index);
|
||||
int realIdx = this.getTrueIdx(index, handler);
|
||||
E value = handler.remove(realIdx);
|
||||
return value;
|
||||
|
@ -62,7 +63,7 @@ public class InventoryHandlerList<E> extends AbstractList<E> {
|
|||
|
||||
@Override
|
||||
public void clear() {
|
||||
for (InventoryHandler<E> handler : indexMapping.valueCollection()) {
|
||||
for (List<E> handler : indexMapping.valueCollection()) {
|
||||
handler.clear();
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +71,7 @@ public class InventoryHandlerList<E> extends AbstractList<E> {
|
|||
@Override
|
||||
public boolean isEmpty() {
|
||||
boolean result = true;
|
||||
for (InventoryHandler<E> handler : indexMapping.valueCollection()) {
|
||||
for (List<E> handler : indexMapping.valueCollection()) {
|
||||
result &= handler.isEmpty();
|
||||
}
|
||||
|
||||
|
@ -111,7 +112,7 @@ public class InventoryHandlerList<E> extends AbstractList<E> {
|
|||
return "Index: "+index+", Size: "+indexMapping.size();
|
||||
}
|
||||
|
||||
protected int getTrueIdx(int index, InventoryHandler<E> handler) {
|
||||
protected int getTrueIdx(int index, List<E> handler) {
|
||||
int offset = offsetMap.get(System.identityHashCode(handler));
|
||||
return index - offset;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,12 @@ package gregtechmod.tests;
|
|||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import gregtechmod.api.util.InventoryHandler;
|
||||
import gregtechmod.api.util.InventoryHandlerList;
|
||||
|
@ -11,6 +16,8 @@ import gregtechmod.api.util.InventoryHandlerList;
|
|||
* @author TheDarkDnKTv
|
||||
*
|
||||
*/
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class MultipleAdapterTest {
|
||||
|
||||
InventoryHandler<Integer> first;
|
||||
|
@ -27,21 +34,41 @@ public class MultipleAdapterTest {
|
|||
second.set(1, 94);
|
||||
second.set(3, 47);
|
||||
second.set(5, 35);
|
||||
|
||||
list = new InventoryHandlerList<>(first, second);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void mergeTest() {
|
||||
System.err.println(first);
|
||||
System.err.println(second);
|
||||
System.out.println("Fisrt list: " + first);
|
||||
System.out.println("Second list: " + second);
|
||||
list = new InventoryHandlerList<>(first, second);
|
||||
System.out.print("New list: ");
|
||||
System.err.println(list);
|
||||
assertTrue("Size: " + list.size(), list.size() == 11);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
public void getTest() {
|
||||
Integer value = list.get(5);
|
||||
assertTrue("Value: " + value, value == 54);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void remove() {
|
||||
Integer a = list.remove(list.size() - 1);
|
||||
assertTrue(a == 35);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void emptyTest() {
|
||||
boolean value = list.isEmpty();
|
||||
list.clear();
|
||||
assertTrue(!value);
|
||||
System.out.println("Fisrt list after clean: " + first);
|
||||
System.out.println("Second list after clean: " + second);
|
||||
assertTrue("Not empty after cleaning", first.isEmpty() && second.isEmpty());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue