/** * Appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the specified * {@link Collection}'s {@link Iterator}. The behavior of this operation is * unspecified if the specified collection is modified while * the operation is in progress. (Note that this will occur if the * specified collection is this list, and it's nonempty.) * * @param c collection whose elements are to be added to this list. * @return <tt>true</tt> if this list changed as a result of the call. * * @throws ClassCastException if the class of an element in the specified * collection prevents it from being added to this list. * @throws IllegalArgumentException if some aspect of an element in the * specified collection prevents it from being added to this * list. */ public boolean addAll(Collection<? extends E> c) { if(c.isEmpty()) { return false; } Iterator<? extends E> it = c.iterator(); while(it.hasNext()) { insertListable(_head.prev(),null,it.next()); } return true; }
/** * Returns a {@link String} representation of this list, suitable for debugging. * @return a {@link String} representation of this list, suitable for debugging. */ @Override public String toString() { StringBuffer buf = new StringBuffer(); buf.append("["); for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { if(_head.next() != elt) { buf.append(", "); } buf.append(elt.value()); } buf.append("]"); return buf.toString(); }
/** * Compares the specified object with this list for equality. Returns * <tt>true</tt> if and only if the specified object is also a list, both * lists have the same size, and all corresponding pairs of elements in * the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null : * e1.equals(e2))</tt>.) In other words, two lists are defined to be * equal if they contain the same elements in the same order. This * definition ensures that the equals method works properly across * different implementations of the <tt>List</tt> interface. * * @param o the object to be compared for equality with this list. * @return <tt>true</tt> if the specified object is equal to this list. */ @Override public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof List)) { return false; } Iterator<?> it = ((List<?>)o).listIterator(); for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { if(!it.hasNext() || (null == elt.value() ? null != it.next() : !(elt.value().equals(it.next()))) ) { return false; } } return !it.hasNext(); }
for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { if (null == elt.value()) { return ndx; for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { if (o.equals(elt.value())) { return ndx;
/** * Returns an array containing all of the elements in this list in proper * sequence; the runtime type of the returned array is that of the * specified array. Obeys the general contract of the * {@link Collection#toArray()} method. * * @param a the array into which the elements of this list are to * be stored, if it is big enough; otherwise, a new array of the * same runtime type is allocated for this purpose. * @return an array containing the elements of this list. * @exception ArrayStoreException * if the runtime type of the specified array * is not a supertype of the runtime type of every element in * this list. */ @SuppressWarnings("unchecked") public <T> T[] toArray(T a[]) { if(a.length < _size) { a = (T[])Array.newInstance(a.getClass().getComponentType(), _size); } int i = 0; for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { a[i++] = (T) elt.value(); } if(a.length > _size) { a[_size] = null; // should we null out the rest of the array also? java.util.LinkedList doesn't } return a; }
for(Listable<E> elt = _head.prev(), past = null; null != elt && past != _head.next(); elt = (past = elt).prev()) { if (null == elt.value()) { return ndx; for(Listable<E> elt = _head.prev(), past = null; null != elt && past != _head.next(); elt = (past = elt).prev()) { if (o.equals(elt.value())) { return ndx;
/** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted. * @param element element to be inserted. * * @throws ClassCastException if the class of the specified element * prevents it from being added to this list. * @throws IllegalArgumentException if some aspect of the specified * element prevents it from being added to this list. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > size()). */ public void add(int index, E element) { if(index == _size) { add(element); } else { if(index < 0 || index > _size) { throw new IndexOutOfBoundsException(String.valueOf(index) + " < 0 or " + String.valueOf(index) + " > " + _size); } Listable<E> succ = (isEmpty() ? null : getListableAt(index)); Listable<E> pred = (null == succ ? null : succ.prev()); insertListable(pred,succ,element); } }
CursorableSubList(CursorableLinkedList<E> list, int from, int to) { if(0 > from || list.size() < to) { throw new IndexOutOfBoundsException(); } else if(from > to) { throw new IllegalArgumentException(); } _list = list; if(from < list.size()) { _head.setNext(_list.getListableAt(from)); _pre = (null == _head.next()) ? null : _head.next().prev(); } else { _pre = _list.getListableAt(from-1); } if(from == to) { _head.setNext(null); _head.setPrev(null); if(to < list.size()) { _post = _list.getListableAt(to); } else { _post = null; } } else { _head.setPrev(_list.getListableAt(to-1)); _post = _head.prev().next(); } _size = to - from; _modCount = _list._modCount; }
/** * Returns the hash code value for this list. The hash code of a list * is defined to be the result of the following calculation: * <pre> * hashCode = 1; * Iterator i = list.iterator(); * while (i.hasNext()) { * Object obj = i.next(); * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); * } * </pre> * This ensures that <tt>list1.equals(list2)</tt> implies that * <tt>list1.hashCode()==list2.hashCode()</tt> for any two lists, * <tt>list1</tt> and <tt>list2</tt>, as required by the general * contract of <tt>Object.hashCode</tt>. * * @return the hash code value for this list. * @see Object#hashCode() * @see Object#equals(Object) * @see #equals(Object) */ @Override public int hashCode() { int hash = 1; for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { hash = 31*hash + (null == elt.value() ? 0 : elt.value().hashCode()); } return hash; }
/** * Returns the * {@link org.apache.commons.collections.CursorableLinkedList.Listable} * at the specified index. * * @throws IndexOutOfBoundsException if index is less than zero or * greater than or equal to the size of this list. */ protected Listable<E> getListableAt(int index) { if(index < 0 || index >= _size) { throw new IndexOutOfBoundsException(String.valueOf(index) + " < 0 or " + String.valueOf(index) + " >= " + _size); } if(index <=_size/2) { Listable<E> elt = _head.next(); for(int i = 0; i < index; i++) { elt = elt.next(); } return elt; } else { Listable<E> elt = _head.prev(); for(int i = (_size-1); i > index; i--) { elt = elt.prev(); } return elt; } }
/** * Inserts a new <i>value</i> into my * list, after the specified <i>before</i> element, and before the * specified <i>after</i> element * * @return the newly created {@link CursorableLinkedList.Listable} */ @Override protected Listable<E> insertListable(Listable<E> before, Listable<E> after, E value) { _modCount++; _size++; Listable<E> elt = _list.insertListable((null == before ? _pre : before), (null == after ? _post : after),value); if(null == _head.next()) { _head.setNext(elt); _head.setPrev(elt); } if(before == _head.prev()) { _head.setPrev(elt); } if(after == _head.next()) { _head.setNext(elt); } broadcastListableInserted(elt); return elt; }
/** * Removes the first occurrence in this list of the specified element. * If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index i * such that <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> (if * such an element exists). * * @param o element to be removed from this list, if present. * @return <tt>true</tt> if this list contained the specified element. */ public boolean remove(Object o) { for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { if(null == o && null == elt.value()) { removeListable(elt); return true; } else if(o != null && o.equals(elt.value())) { removeListable(elt); return true; } } return false; }
/** * Removes the given * {@link org.apache.commons.collections.CursorableLinkedList.Listable} * from my list. */ protected void removeListable(Listable<E> elt) { _modCount++; _size--; if(_head.next() == elt) { _head.setNext(elt.next()); } if(null != elt.next()) { elt.next().setPrev(elt.prev()); } if(_head.prev() == elt) { _head.setPrev(elt.prev()); } if(null != elt.prev()) { elt.prev().setNext(elt.next()); } broadcastListableRemoved(elt); }
/** * Removes the given {@link CursorableLinkedList.Listable} from my list. */ @Override protected void removeListable(Listable<E> elt) { _modCount++; _size--; if(_head.next() == elt && _head.prev() == elt) { _head.setNext(null); _head.setPrev(null); } if(_head.next() == elt) { _head.setNext(elt.next()); } if(_head.prev() == elt) { _head.setPrev(elt.prev()); } _list.removeListable(elt); broadcastListableRemoved(elt); }
/** * Returns <tt>true</tt> if this list contains the specified element. * More formally, returns <tt>true</tt> if and only if this list contains * at least one element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this list is to be tested. * @return <tt>true</tt> if this list contains the specified element. */ public boolean contains(Object o) { for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { if((null == o && null == elt.value()) || (o != null && o.equals(elt.value()))) { return true; } } return false; }
/** * Returns an array containing all of the elements in this list in proper * sequence. Obeys the general contract of the {@link Collection#toArray()} method. * * @return an array containing all of the elements in this list in proper * sequence. */ public Object[] toArray() { Object[] array = new Object[_size]; int i = 0; for(Listable<E> elt = _head.next(), past = null; null != elt && past != _head.prev(); elt = (past = elt).next()) { array[i++] = elt.value(); } return array; }
/** * Returns the element at the end of this list. */ public E getLast() { try { return _head.prev().value(); } catch(NullPointerException e) { throw new NoSuchElementException(); } }
/** * Inserts the specified element at the end of this list. * (Equivalent to {@link #add(java.lang.Object)}). * * @param o element to be appended to this list. * @return <tt>true</tt> */ public boolean addLast(E o) { insertListable(_head.prev(),null,o); return true; }
/** * Appends the specified element to the end of this list. * * @param o element to be appended to this list. * @return <tt>true</tt> */ public boolean add(E o) { insertListable(_head.prev(),null,o); return true; }