/** * 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; }
/** {@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; }
/** * @return the remaining space in this sequence (capacity() - size()) */ public int remaining() { return capacity() - size(); }
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; }
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()); list.swap(indexA, indexB); Collections.swap(expectedList, indexA, indexB); assertTrue(list.size() == currentSize);
@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); }
@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); }