public E setValue(final int index, final E value) { checkBounds(index); final E oldValue = getValue(index); array[index] = value; return oldValue; }
@Override public boolean contains(final Object o) { if (!compatibleClass(o)) return false; return indexOf(o) >= 0; }
/** Tests {@link ObjectArray#addValue(Object)}. */ @Test public void testAddValue() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); final Integer e6 = 1, e7 = 2; array.addValue(e6); array.addValue(e7); assertEquals(raw.length + 2, array.size()); for (int i = 0; i < raw.length; i++) { assertEquals("@" + i, raw[i], array.getValue(i)); } assertEquals(e6, array.getValue(5)); assertEquals(e7, array.getValue(6)); }
/** Tests {@link ObjectArray#removeValue(Object)}. */ public void testRemoveValue() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); assertEquals(raw.length, array.size()); array.removeValue(raw[0]); assertEquals(raw.length - 1, array.size()); array.removeValue(raw[2]); assertEquals(raw.length - 2, array.size()); array.removeValue(raw[4]); assertEquals(raw.length - 3, array.size()); assertEquals(raw[1], array.getValue(0)); assertEquals(raw[3], array.getValue(1)); }
/** Tests {@link ObjectArray#ObjectArray(Object[])}. */ @Test public void testConstructorArray() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw); assertSame(raw, array.getArray()); assertEquals(raw.length, array.size()); for (int i = 0; i < raw.length; i++) { assertEquals("@" + i, raw[i], array.getValue(i)); } assertArrayEquals(raw, array.copyArray()); }
/** Tests {@link ObjectArray#set(int, Object)}. */ @Test public void testSet() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); final Integer e0 = 7, e2 = 1, e4 = 2; array.set(0, e0); array.set(2, e2); array.set(4, e4); assertEquals(raw.length, array.size()); assertEquals(e0, array.get(0)); assertEquals(raw[1], array.getValue(1)); assertEquals(e2, array.get(2)); assertEquals(raw[3], array.getValue(3)); assertEquals(e4, array.get(4)); }
/** Tests {@link ObjectArray#setValue(int, Object)}. */ @Test public void testSetValue() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); final Integer e0 = 7, e2 = 1, e4 = 2; array.setValue(0, e0); array.setValue(2, e2); array.setValue(4, e4); assertEquals(raw.length, array.size()); assertEquals(e0, array.getValue(0)); assertEquals(raw[1], array.getValue(1)); assertEquals(e2, array.getValue(2)); assertEquals(raw[3], array.getValue(3)); assertEquals(e4, array.getValue(4)); }
/** Tests {@link ObjectArray#add(int, Object)}. */ @Test public void testAdd() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); final Integer e6 = 1, e7 = 2; array.add(e6); array.add(e7); assertEquals(raw.length + 2, array.size()); for (int i = 0; i < raw.length; i++) { assertEquals("@" + i, raw[i], array.getValue(i)); } assertEquals(e6, array.get(5)); assertEquals(e7, array.get(6)); }
/** Tests {@link ObjectArray#remove(int)}. */ public void testRemoveIndex() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); assertEquals(raw.length, array.size()); array.remove(0); assertEquals(raw.length - 1, array.size()); array.remove(2); assertEquals(raw.length - 2, array.size()); array.remove(4); assertEquals(raw.length - 3, array.size()); assertEquals(raw[1], array.getValue(0)); assertEquals(raw[3], array.getValue(1)); }
/** Tests {@link ObjectArray#removeAll}. */ @Test public void testRemoveAll() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); final ObjectArray<Integer> toRemove = new ObjectArray<>(new Integer[] { 3, 8, 21 }); assertEquals(raw.length, array.size()); array.removeAll(toRemove); assertEquals(raw.length - 3, array.size()); assertEquals(raw[1], array.getValue(0)); assertEquals(raw[3], array.getValue(1)); }
/** Tests {@link ObjectArray#ObjectArray(Class)}. */ @Test public void testConstructorNoArgs() { final ObjectArray<Integer> array = new ObjectArray<>(Integer.class); assertEquals(0, array.size()); assertEquals(0, array.copyArray().length); }
/** Tests {@link ObjectArray#addAll(int, java.util.Collection)}. */ @Test public void testAddAll() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); final Integer[] add = { 1, 7 }; final ObjectArray<Integer> toAdd = new ObjectArray<>(add.clone()); final int index = 3; array.addAll(index, toAdd); for (int i = 0; i < index; i++) { assertEquals(raw[i], array.getValue(i)); } for (int i = index; i < index + add.length; i++) { assertEquals(add[i - index], array.getValue(i)); } for (int i = index + add.length; i < raw.length + add.length; i++) { assertEquals(raw[i - add.length], array.getValue(i)); } }
@Override public int indexOf(final Object o) { if (!compatibleClass(o)) return -1; for (int i = 0; i < size(); i++) { if (array[i].equals(o)) return i; } return -1; }
@Override public boolean remove(final Object o) { if (!compatibleClass(o)) return false; final E value = Types.cast(o, objectClass); return removeValue(value); }
/** Tests {@link ObjectArray#indexOf(Object)}. */ @Test public void testIndexOf() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); for (int i = 0; i < raw.length; i++) { assertEquals("@" + i, i, array.indexOf(raw[i])); } assertEquals(-1, array.indexOf(-1)); assertEquals(-1, array.indexOf(0)); assertEquals(-1, array.indexOf(1)); assertEquals(-1, array.indexOf(Integer.MAX_VALUE)); assertEquals(-1, array.indexOf(Integer.MIN_VALUE)); }
/** * Tests {@link ObjectArray#getArray()} and * {@link ObjectArray#setArray(Object[])}. */ @Test public void testSetArray() { final ObjectArray<Integer> array = new ObjectArray<>(Integer.class); final Integer[] raw = { 1, 2, 3, 5, 8, 13, 21 }; array.setArray(raw); assertSame(raw, array.getArray()); }
/** Tests {@link ObjectArray#getValue(int)}. */ public void testGetValue() { final Integer[] raw = { 3, 5, 8, 13, 21 }; final ObjectArray<Integer> array = new ObjectArray<>(raw.clone()); for (int i = 0; i < raw.length; i++) { assertEquals("@" + i, raw[i], array.getValue(i)); } }