/** * Returns the first element of this list. If the list is empty, it returns {@code null}. * * @return the first element of this list. */ public T getFirst() { if (isEmpty()) { return null; } else { return values[0]; } }
/** * Returns {@code true} if this list contains no elements. * * @return {@code true} if this list contains no elements. */ @Override public boolean isEmpty() { return size() == 0; }
YoPreallocatedList<YoDouble> list = new YoPreallocatedList<>(YoDouble.class, "Test", 20, new YoVariableRegistry("Test")); ArrayList<YoDouble> expectedList = new ArrayList<>(); YoDouble lastObject = list.add(); expectedList.add(lastObject); assertFalse(list.isEmpty()); assertTrue(list.size() == finalSize); for (int i = 0; i < finalSize; i++) assertTrue(list.get(i) == expectedList.get(i)); assertTrue(list.getLast() == expectedList.get(finalSize - 1)); list.get(finalSize); fail(); list.clear(); expectedList.clear(); assertTrue(list.getLast() == null); YoDouble lastObject = list.add(); expectedList.add(lastObject); assertFalse(list.isEmpty()); assertTrue(list.size() == finalSize); for (int i = 0; i < finalSize; i++) assertTrue(list.get(i) == expectedList.get(i));
@Test(timeout = 30000) public void testRemove() YoPreallocatedList<YoInteger> list = new YoPreallocatedList<>(YoInteger.class, "Test", 10, new YoVariableRegistry("Test")); int currentSize = 10; while (list.size() < currentSize) list.add().set(10 + list.size()); expectedList.add(list.get(i)); list.remove(indexOfRemovedObject); expectedList.remove(indexOfRemovedObject); currentSize--; assertTrue(list.size() == currentSize); assertTrue(list.get(i) == expectedList.get(i)); list.remove(indexOfRemovedObject); expectedList.remove(indexOfRemovedObject); currentSize--; assertTrue(list.size() == currentSize); assertTrue(list.get(i) == expectedList.get(i)); list.remove(currentSize); fail();
YoPreallocatedList<YoDouble> list = new YoPreallocatedList<>(YoDouble.class, "Test", 10, testRegistry); for (int i = 0; i < size; i++) list.add().set(i); list.set(0, testDouble); fail(); list.add(testDouble); fail(); list.add(0, testDouble); fail(); list.addAll(new HashSet<>()); fail(); list.replaceAll(UnaryOperator.identity()); fail(); list.iterator(); fail(); list.listIterator(); fail(); list.listIterator(0); fail();
@Test(timeout = 30000) public void testConstructor() { YoPreallocatedList<YoDouble> list = new YoPreallocatedList<>(YoDouble.class, "Test", 10, new YoVariableRegistry("Test")); assertTrue(list.isEmpty()); assertTrue(list.size() == 0); assertTrue(list.getLast() == null); }
YoPreallocatedList<YoInteger> list = new YoPreallocatedList<>(YoInteger.class, "Test", 10, new YoVariableRegistry("Test")); int currentSize = 10; while (list.size() < currentSize) list.add().set(10 + list.size()); expectedList.add(list.get(i)); list.swap(indexA, indexB); Collections.swap(expectedList, indexA, indexB); assertTrue(list.size() == currentSize); assertTrue(list.get(i) == expectedList.get(i)); list.swap(0, currentSize); fail(); list.swap(currentSize, 0); fail();
@Test(timeout = 30000) public void testSort() { YoPreallocatedList<YoInteger> list = new YoPreallocatedList<>(YoInteger.class, "Test", 10, new YoVariableRegistry("Test")); list.add().set(-3); list.add().set(20); list.add().set(-10); list.add().set(19); list.add().set(50); list.sort((o1, o2) -> NumberUtils.compare(o1.getIntegerValue(), o2.getIntegerValue())); assertTrue(list.get(0).getValue() == -10); assertTrue(list.get(1).getValue() == -3); assertTrue(list.get(2).getValue() == 19); assertTrue(list.get(3).getValue() == 20); assertTrue(list.get(4).getValue() == 50); }
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param i the index of the element to be removed */ @Override public T remove(int i) { if (i == position.getIntegerValue()) { remove(); return values[i]; } rangeCheck(i); T t = values[i]; while (i < position.getIntegerValue()) { values[i] = values[++i]; } // Do not throw away the removed element, put it at the end of the list instead. values[position.getIntegerValue()] = t; position.decrement(); return t; }
/** {@inheritDoc} */ @Override public boolean containsAll(Collection<?> c) { for (Object o : c) { if (!contains(o)) return false; } return true; }
@Test(timeout = 30000) public void testRemoveIndex() { int size = 10; YoPreallocatedList<YoInteger> list = new YoPreallocatedList<>(YoInteger.class, "Test", size, new YoVariableRegistry("Test")); for (int i = 0; i < size; i++) { list.add().set(i); } assertTrue(list.remove(8).getValue() == 8); assertTrue(list.size() == size - 1); assertTrue(list.remove(4).getValue() == 4); assertTrue(list.size() == size - 2); assertTrue(list.remove(2).getValue() == 2); assertTrue(list.size() == size - 3); assertTrue(list.remove(size - 4).getValue() == size - 1); assertTrue(list.size() == size - 4); }
/** * Returns the last element of this list. If the list is empty, it returns {@code null}. * * @return the last element of this list. */ public T getLast() { if (isEmpty()) { return null; } else { return values[position.getIntegerValue()]; } }
/** {@inheritDoc} */ @Override public boolean contains(Object o) { for (int i = 0; i < size(); i++) { if(values[i].equals(o)) { return true; } } return false; }
/** * Returns the elements in this list as array * * This method allocates a new array * * @return new array of length size(); */ @Override public T[] toArray() { @SuppressWarnings("unchecked") T[] array = (T[]) Array.newInstance(clazz, size()); System.arraycopy(values, 0, array, 0, size()); return array; }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof List)) return false; List<?> other = (List<?>) obj; if (size() != other.size()) return false; for (int i = 0; i < size(); i++) { if (!values[i].equals(other.get(i))) return false; } return true; }
/** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public <S> S[] toArray(S[] dest) { int size = size(); if (dest.length < size) { return (S[]) Arrays.copyOf(values, size, dest.getClass()); } System.arraycopy(values, 0, dest, 0, size); if (dest.length > size) dest[size] = null; return dest; }
private void rangeCheck(int index) { if(index < 0 || index > this.position.getIntegerValue()) { throw new ArrayIndexOutOfBoundsException("Position is not valid in the list, size is " + size() + ", requested element is " + index); } }
@Override public String toString() { String s = ""; s += clazz.getSimpleName(); s += " pos: " + position.getIntegerValue(); s += " ["; for (int i = 0; i < size(); i++) { if (i > 0) s += ", "; s += values[i].toString(); } s += "]"; return s; }