@Override public boolean hasNext() { return nextIndex < parent.size(); }
/** * Checks whether the index is valid. * * @param index the index to check * @param startIndex the first allowed index * @param endIndex the last allowed index * @throws IndexOutOfBoundsException if the index is invalid */ private void checkInterval(final int index, final int startIndex, final int endIndex) { if (index < startIndex || index > endIndex) { throw new IndexOutOfBoundsException("Invalid index:" + index + ", size=" + size()); } }
/** * Converts the list into an array. * * @return the list as an array */ @Override public Object[] toArray() { // override to go 20% faster final Object[] array = new Object[size()]; if (root != null) { root.toArray(array, root.relativePosition); } return array; }
/** * Gets a ListIterator over the list. * * @param fromIndex the index to start from * @return the new iterator */ @Override public ListIterator<E> listIterator(final int fromIndex) { // override to go 75% faster // cannot use EmptyIterator as iterator.add() must work checkInterval(fromIndex, 0, size()); return new TreeListIterator<>(this, fromIndex); }
/** * Gets the element at the specified index. * * @param index the index to retrieve * @return the element at the specified index */ @Override public E get(final int index) { checkInterval(index, 0, size() - 1); return root.get(index).getValue(); }
/** * Sets the element at the specified index. * * @param index the index to set * @param obj the object to store at the specified index * @return the previous object at that index * @throws IndexOutOfBoundsException if the index is invalid */ @Override public E set(final int index, final E obj) { checkInterval(index, 0, size() - 1); final AVLNode<E> node = root.get(index); final E result = node.value; node.setValue(obj); return result; }
/** * Removes the element at the specified index. * * @param index the index to remove * @return the previous object at that index */ @Override public E remove(final int index) { modCount++; checkInterval(index, 0, size() - 1); final E result = get(index); root = root.remove(index); size--; return result; }
/** * Adds a new element to the list. * * @param index the index to add before * @param obj the element to add */ @Override public void add(final int index, final E obj) { modCount++; checkInterval(index, 0, size()); if (root == null) { root = new AVLNode<>(index, obj, null, null); } else { root = root.insert(index, obj); } size++; }