private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(_size); Listable<E> cur = _head.next(); while (cur != null) { out.writeObject(cur.value()); cur = cur.next(); } }
/** * 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(); }
/** * Returns the element at the end of this list. */ public Object getLast() { try { return _head.prev().value(); } catch(NullPointerException e) { throw new NoSuchElementException(); } }
/** * Removes the last element of this list, if any. */ public E removeLast() { if(_head.prev() != null) { E val = _head.prev().value(); removeListable(_head.prev()); return val; } else { throw new NoSuchElementException(); } }
/** * Removes the first element of this list, if any. */ public E removeFirst() { if(_head.next() != null) { E val = _head.next().value(); removeListable(_head.next()); return val; } else { throw new NoSuchElementException(); } }
key = _evictionKeyCursor._lastReturned.value();
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * Removes the element at the specified position in this list (optional * operation). Shifts any subsequent elements to the left (subtracts one * from their indices). Returns the element that was removed from the * list. * * @param index the index of the element to removed. * @return the element previously at the specified position. * * @throws IndexOutOfBoundsException if the index is out of range (index * < 0 || index >= size()). */ public E remove(int index) { Listable<E> elt = getListableAt(index); E ret = elt.value(); removeListable(elt); return ret; }
/** * 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 beginning of this list. */ public E getFirst() { try { return _head.next().value(); } catch(NullPointerException e) { throw new NoSuchElementException(); } }
/** * Removes the first element of this list, if any. */ public E removeFirst() { if(_head.next() != null) { E val = _head.next().value(); removeListable(_head.next()); return val; } else { throw new NoSuchElementException(); } }
/** * Returns the element at the end of this list. */ public E getLast() { try { return _head.prev().value(); } catch(NullPointerException e) { throw new NoSuchElementException(); } }
/** * Returns the element at the specified position in this list. * * @param index index of element to return. * @return the element at the specified position in this list. * * @throws IndexOutOfBoundsException if the index is out of range (index * < 0 || index >= size()). */ public E get(int index) { return getListableAt(index).value(); }
/** * Removes the last element of this list, if any. */ public E removeLast() { if(_head.prev() != null) { E val = _head.prev().value(); removeListable(_head.prev()); return val; } else { throw new NoSuchElementException(); } }