17.50/7.49 YES
17.68/7.52 proof of /export/starexec/sandbox/benchmark/theBenchmark.jar
17.68/7.52 # AProVE Commit ID: 48fb2092695e11cc9f56e44b17a92a5f88ffb256 marcel 20180622 unpublished dirty
17.68/7.52
17.68/7.52
17.68/7.52 termination of the given Bare JBC problem could be proven:
17.68/7.52
17.68/7.52 (0) Bare JBC problem
17.68/7.52 (1) BareJBCToJBCProof [EQUIVALENT, 96 ms]
17.68/7.52 (2) JBC problem
17.68/7.52 (3) JBCToGraph [EQUIVALENT, 1625 ms]
17.68/7.52 (4) JBCTerminationGraph
17.68/7.52 (5) TerminationGraphToSCCProof [SOUND, 0 ms]
17.68/7.52 (6) JBCTerminationSCC
17.68/7.52 (7) SCCToIRSProof [SOUND, 453 ms]
17.68/7.52 (8) IRSwT
17.68/7.52 (9) IRSFormatTransformerProof [EQUIVALENT, 0 ms]
17.68/7.52 (10) IRSwT
17.68/7.52 (11) IRSwTTerminationDigraphProof [EQUIVALENT, 107 ms]
17.68/7.52 (12) IRSwT
17.68/7.52 (13) IntTRSCompressionProof [EQUIVALENT, 0 ms]
17.68/7.52 (14) IRSwT
17.68/7.52 (15) TempFilterProof [SOUND, 130 ms]
17.68/7.52 (16) IntTRS
17.68/7.52 (17) PolynomialOrderProcessor [EQUIVALENT, 0 ms]
17.68/7.52 (18) IntTRS
17.68/7.52 (19) PolynomialOrderProcessor [EQUIVALENT, 9 ms]
17.68/7.52 (20) YES
17.68/7.52
17.68/7.52
17.68/7.52 ----------------------------------------
17.68/7.52
17.68/7.52 (0)
17.68/7.52 Obligation:
17.68/7.52 need to prove termination of the following program:
17.68/7.52 /*
17.68/7.52 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.52 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.52 *
17.68/7.52 * This code is free software; you can redistribute it and/or modify it
17.68/7.52 * under the terms of the GNU General Public License version 2 only, as
17.68/7.52 * published by the Free Software Foundation. Sun designates this
17.68/7.52 * particular file as subject to the "Classpath" exception as provided
17.68/7.52 * by Sun in the LICENSE file that accompanied this code.
17.68/7.52 *
17.68/7.52 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.52 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.52 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.52 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.52 * accompanied this code).
17.68/7.52 *
17.68/7.52 * You should have received a copy of the GNU General Public License version
17.68/7.52 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.52 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.52 *
17.68/7.52 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.52 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.52 * have any questions.
17.68/7.52 */
17.68/7.52
17.68/7.52 package javaUtilEx;
17.68/7.52
17.68/7.52 /**
17.68/7.52 * This class provides a skeletal implementation of the Collection
17.68/7.52 * interface, to minimize the effort required to implement this interface.
17.68/7.52 *
17.68/7.52 * To implement an unmodifiable collection, the programmer needs only to
17.68/7.52 * extend this class and provide implementations for the iterator and
17.68/7.52 * size methods. (The iterator returned by the iterator
17.68/7.52 * method must implement hasNext and next.)
17.68/7.52 *
17.68/7.52 * To implement a modifiable collection, the programmer must additionally
17.68/7.52 * override this class's add method (which otherwise throws an
17.68/7.52 * UnsupportedOperationException), and the iterator returned by the
17.68/7.52 * iterator method must additionally implement its remove
17.68/7.52 * method.
17.68/7.52 *
17.68/7.52 * The programmer should generally provide a void (no argument) and
17.68/7.52 * Collection constructor, as per the recommendation in the
17.68/7.52 * Collection interface specification.
17.68/7.52 *
17.68/7.52 * The documentation for each non-abstract method in this class describes its
17.68/7.52 * implementation in detail. Each of these methods may be overridden if
17.68/7.52 * the collection being implemented admits a more efficient implementation.
17.68/7.52 *
17.68/7.52 * This class is a member of the
17.68/7.52 *
17.68/7.52 * Java Collections Framework.
17.68/7.52 *
17.68/7.52 * @author Josh Bloch
17.68/7.52 * @author Neal Gafter
17.68/7.52 * @see Collection
17.68/7.52 * @since 1.2
17.68/7.52 */
17.68/7.52
17.68/7.52 public abstract class AbstractCollection implements Collection {
17.68/7.52 /**
17.68/7.52 * Sole constructor. (For invocation by subclass constructors, typically
17.68/7.52 * implicit.)
17.68/7.52 */
17.68/7.52 protected AbstractCollection() {
17.68/7.52 }
17.68/7.52
17.68/7.52 // Query Operations
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Returns an iterator over the elements contained in this collection.
17.68/7.52 *
17.68/7.52 * @return an iterator over the elements contained in this collection
17.68/7.52 */
17.68/7.52 public abstract Iterator iterator();
17.68/7.52
17.68/7.52 public abstract int size();
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation returns size() == 0.
17.68/7.52 */
17.68/7.52 public boolean isEmpty() {
17.68/7.52 return size() == 0;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation iterates over the elements in the collection,
17.68/7.52 * checking each element in turn for equality with the specified element.
17.68/7.52 *
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public boolean contains(Object o) {
17.68/7.52 Iterator e = iterator();
17.68/7.52 if (o==null) {
17.68/7.52 while (e.hasNext())
17.68/7.52 if (e.next()==null)
17.68/7.52 return true;
17.68/7.52 } else {
17.68/7.52 while (e.hasNext())
17.68/7.52 if (o.equals(e.next()))
17.68/7.52 return true;
17.68/7.52 }
17.68/7.52 return false;
17.68/7.52 }
17.68/7.52
17.68/7.52 // Modification Operations
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation always throws an
17.68/7.52 * UnsupportedOperationException.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IllegalStateException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public boolean add(E e) {
17.68/7.52 throw new UnsupportedOperationException();
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation iterates over the collection looking for the
17.68/7.52 * specified element. If it finds the element, it removes the element
17.68/7.52 * from the collection using the iterator's remove method.
17.68/7.52 *
17.68/7.52 *
Note that this implementation throws an
17.68/7.52 * UnsupportedOperationException if the iterator returned by this
17.68/7.52 * collection's iterator method does not implement the remove
17.68/7.52 * method and this collection contains the specified object.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public boolean remove(Object o) {
17.68/7.52 Iterator e = iterator();
17.68/7.52 if (o==null) {
17.68/7.52 while (e.hasNext()) {
17.68/7.52 if (e.next()==null) {
17.68/7.52 e.remove();
17.68/7.52 return true;
17.68/7.52 }
17.68/7.52 }
17.68/7.52 } else {
17.68/7.52 while (e.hasNext()) {
17.68/7.52 if (o.equals(e.next())) {
17.68/7.52 e.remove();
17.68/7.52 return true;
17.68/7.52 }
17.68/7.52 }
17.68/7.52 }
17.68/7.52 return false;
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 // Bulk Operations
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation iterates over the specified collection,
17.68/7.52 * checking each element returned by the iterator in turn to see
17.68/7.52 * if it's contained in this collection. If all elements are so
17.68/7.52 * contained true is returned, otherwise false.
17.68/7.52 *
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @see #contains(Object)
17.68/7.52 */
17.68/7.52 public boolean containsAll(Collection> c) {
17.68/7.52 Iterator> e = c.iterator();
17.68/7.52 while (e.hasNext())
17.68/7.52 if (!contains(e.next()))
17.68/7.52 return false;
17.68/7.52 return true;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation iterates over the specified collection, and adds
17.68/7.52 * each object returned by the iterator to this collection, in turn.
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException unless add is
17.68/7.52 * overridden (assuming the specified collection is non-empty).
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IllegalStateException {@inheritDoc}
17.68/7.52 *
17.68/7.52 * @see #add(Object)
17.68/7.52 */
17.68/7.52 public boolean addAll(Collection extends E> c) {
17.68/7.52 boolean modified = false;
17.68/7.52 Iterator extends E> e = c.iterator();
17.68/7.52 while (e.hasNext()) {
17.68/7.52 if (add(e.next()))
17.68/7.52 modified = true;
17.68/7.52 }
17.68/7.52 return modified;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation iterates over this collection, checking each
17.68/7.52 * element returned by the iterator in turn to see if it's contained
17.68/7.52 * in the specified collection. If it's so contained, it's removed from
17.68/7.52 * this collection with the iterator's remove method.
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException if the iterator returned by the
17.68/7.52 * iterator method does not implement the remove method
17.68/7.52 * and this collection contains one or more elements in common with the
17.68/7.52 * specified collection.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 *
17.68/7.52 * @see #remove(Object)
17.68/7.52 * @see #contains(Object)
17.68/7.52 */
17.68/7.52 public boolean removeAll(Collection> c) {
17.68/7.52 boolean modified = false;
17.68/7.52 Iterator> e = iterator();
17.68/7.52 while (e.hasNext()) {
17.68/7.52 if (c.contains(e.next())) {
17.68/7.52 e.remove();
17.68/7.52 modified = true;
17.68/7.52 }
17.68/7.52 }
17.68/7.52 return modified;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation iterates over this collection, checking each
17.68/7.52 * element returned by the iterator in turn to see if it's contained
17.68/7.52 * in the specified collection. If it's not so contained, it's removed
17.68/7.52 * from this collection with the iterator's remove method.
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException if the iterator returned by the
17.68/7.52 * iterator method does not implement the remove method
17.68/7.52 * and this collection contains one or more elements not present in the
17.68/7.52 * specified collection.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 *
17.68/7.52 * @see #remove(Object)
17.68/7.52 * @see #contains(Object)
17.68/7.52 */
17.68/7.52 public boolean retainAll(Collection> c) {
17.68/7.52 boolean modified = false;
17.68/7.52 Iterator e = iterator();
17.68/7.52 while (e.hasNext()) {
17.68/7.52 if (!c.contains(e.next())) {
17.68/7.52 e.remove();
17.68/7.52 modified = true;
17.68/7.52 }
17.68/7.52 }
17.68/7.52 return modified;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation iterates over this collection, removing each
17.68/7.52 * element using the Iterator.remove operation. Most
17.68/7.52 * implementations will probably choose to override this method for
17.68/7.52 * efficiency.
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException if the iterator returned by this
17.68/7.52 * collection's iterator method does not implement the
17.68/7.52 * remove method and this collection is non-empty.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public void clear() {
17.68/7.52 Iterator e = iterator();
17.68/7.52 while (e.hasNext()) {
17.68/7.52 e.next();
17.68/7.52 e.remove();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 // String conversion
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Returns a string representation of this collection. The string
17.68/7.52 * representation consists of a list of the collection's elements in the
17.68/7.52 * order they are returned by its iterator, enclosed in square brackets
17.68/7.52 * ("[]"). Adjacent elements are separated by the characters
17.68/7.52 * ", " (comma and space). Elements are converted to strings as
17.68/7.52 * by {@link String#valueOf(Object)}.
17.68/7.52 *
17.68/7.52 * @return a string representation of this collection
17.68/7.52 */
17.68/7.52 public String toString() {
17.68/7.52 Iterator i = iterator();
17.68/7.52 if (! i.hasNext())
17.68/7.52 return "[]";
17.68/7.52
17.68/7.52 String sb = "";
17.68/7.52 sb = sb + "[";
17.68/7.52 for (;;) {
17.68/7.52 E e = i.next();
17.68/7.52 sb = sb + (e == this ? "(this Collection)" : e);
17.68/7.52 if (! i.hasNext()) {
17.68/7.52 sb = sb + "]";
17.68/7.52 return sb;
17.68/7.52 }
17.68/7.52 sb = sb + ", ";
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 /*
17.68/7.52 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.52 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.52 *
17.68/7.52 * This code is free software; you can redistribute it and/or modify it
17.68/7.52 * under the terms of the GNU General Public License version 2 only, as
17.68/7.52 * published by the Free Software Foundation. Sun designates this
17.68/7.52 * particular file as subject to the "Classpath" exception as provided
17.68/7.52 * by Sun in the LICENSE file that accompanied this code.
17.68/7.52 *
17.68/7.52 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.52 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.52 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.52 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.52 * accompanied this code).
17.68/7.52 *
17.68/7.52 * You should have received a copy of the GNU General Public License version
17.68/7.52 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.52 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.52 *
17.68/7.52 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.52 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.52 * have any questions.
17.68/7.52 */
17.68/7.52
17.68/7.52 package javaUtilEx;
17.68/7.52
17.68/7.52 /**
17.68/7.52 * This class provides a skeletal implementation of the {@link List}
17.68/7.52 * interface to minimize the effort required to implement this interface
17.68/7.52 * backed by a "random access" data store (such as an array). For sequential
17.68/7.52 * access data (such as a linked list), {@link AbstractSequentialList} should
17.68/7.52 * be used in preference to this class.
17.68/7.52 *
17.68/7.52 * To implement an unmodifiable list, the programmer needs only to extend
17.68/7.52 * this class and provide implementations for the {@link #get(int)} and
17.68/7.52 * {@link List#size() size()} methods.
17.68/7.52 *
17.68/7.52 *
To implement a modifiable list, the programmer must additionally
17.68/7.52 * override the {@link #set(int, Object) set(int, E)} method (which otherwise
17.68/7.52 * throws an {@code UnsupportedOperationException}). If the list is
17.68/7.52 * variable-size the programmer must additionally override the
17.68/7.52 * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
17.68/7.52 *
17.68/7.52 *
The programmer should generally provide a void (no argument) and collection
17.68/7.52 * constructor, as per the recommendation in the {@link Collection} interface
17.68/7.52 * specification.
17.68/7.52 *
17.68/7.52 *
Unlike the other abstract collection implementations, the programmer does
17.68/7.52 * not have to provide an iterator implementation; the iterator and
17.68/7.52 * list iterator are implemented by this class, on top of the "random access"
17.68/7.52 * methods:
17.68/7.52 * {@link #get(int)},
17.68/7.52 * {@link #set(int, Object) set(int, E)},
17.68/7.52 * {@link #add(int, Object) add(int, E)} and
17.68/7.52 * {@link #remove(int)}.
17.68/7.52 *
17.68/7.52 *
The documentation for each non-abstract method in this class describes its
17.68/7.52 * implementation in detail. Each of these methods may be overridden if the
17.68/7.52 * collection being implemented admits a more efficient implementation.
17.68/7.52 *
17.68/7.52 *
This class is a member of the
17.68/7.52 *
17.68/7.52 * Java Collections Framework.
17.68/7.52 *
17.68/7.52 * @author Josh Bloch
17.68/7.52 * @author Neal Gafter
17.68/7.52 * @since 1.2
17.68/7.52 */
17.68/7.52
17.68/7.52 public abstract class AbstractList extends AbstractCollection implements List {
17.68/7.52 /**
17.68/7.52 * Sole constructor. (For invocation by subclass constructors, typically
17.68/7.52 * implicit.)
17.68/7.52 */
17.68/7.52 protected AbstractList() {
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Appends the specified element to the end of this list (optional
17.68/7.52 * operation).
17.68/7.52 *
17.68/7.52 * Lists that support this operation may place limitations on what
17.68/7.52 * elements may be added to this list. In particular, some
17.68/7.52 * lists will refuse to add null elements, and others will impose
17.68/7.52 * restrictions on the type of elements that may be added. List
17.68/7.52 * classes should clearly specify in their documentation any restrictions
17.68/7.52 * on what elements may be added.
17.68/7.52 *
17.68/7.52 *
This implementation calls {@code add(size(), e)}.
17.68/7.52 *
17.68/7.52 *
Note that this implementation throws an
17.68/7.52 * {@code UnsupportedOperationException} unless
17.68/7.52 * {@link #add(int, Object) add(int, E)} is overridden.
17.68/7.52 *
17.68/7.52 * @param e element to be appended to this list
17.68/7.52 * @return {@code true} (as specified by {@link Collection#add})
17.68/7.52 * @throws UnsupportedOperationException if the {@code add} operation
17.68/7.52 * is not supported by this list
17.68/7.52 * @throws ClassCastException if the class of the specified element
17.68/7.52 * prevents it from being added to this list
17.68/7.52 * @throws NullPointerException if the specified element is null and this
17.68/7.52 * list does not permit null elements
17.68/7.52 * @throws IllegalArgumentException if some property of this element
17.68/7.52 * prevents it from being added to this list
17.68/7.52 */
17.68/7.52 public boolean add(E e) {
17.68/7.52 add(size(), e);
17.68/7.52 return true;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 abstract public E get(int index);
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation always throws an
17.68/7.52 * {@code UnsupportedOperationException}.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public E set(int index, E element) {
17.68/7.52 throw new UnsupportedOperationException();
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation always throws an
17.68/7.52 * {@code UnsupportedOperationException}.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public void add(int index, E element) {
17.68/7.52 throw new UnsupportedOperationException();
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation always throws an
17.68/7.52 * {@code UnsupportedOperationException}.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public E remove(int index) {
17.68/7.52 throw new UnsupportedOperationException();
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 // Search Operations
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation first gets a list iterator (with
17.68/7.52 * {@code listIterator()}). Then, it iterates over the list until the
17.68/7.52 * specified element is found or the end of the list is reached.
17.68/7.52 *
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public int indexOf(Object o) {
17.68/7.52 ListIterator e = listIterator();
17.68/7.52 if (o==null) {
17.68/7.52 while (e.hasNext())
17.68/7.52 if (e.next()==null)
17.68/7.52 return e.previousIndex();
17.68/7.52 } else {
17.68/7.52 while (e.hasNext())
17.68/7.52 if (o.equals(e.next()))
17.68/7.52 return e.previousIndex();
17.68/7.52 }
17.68/7.52 return -1;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation first gets a list iterator that points to the end
17.68/7.52 * of the list (with {@code listIterator(size())}). Then, it iterates
17.68/7.52 * backwards over the list until the specified element is found, or the
17.68/7.52 * beginning of the list is reached.
17.68/7.52 *
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public int lastIndexOf(Object o) {
17.68/7.52 ListIterator e = listIterator(size());
17.68/7.52 if (o==null) {
17.68/7.52 while (e.hasPrevious())
17.68/7.52 if (e.previous()==null)
17.68/7.52 return e.nextIndex();
17.68/7.52 } else {
17.68/7.52 while (e.hasPrevious())
17.68/7.52 if (o.equals(e.previous()))
17.68/7.52 return e.nextIndex();
17.68/7.52 }
17.68/7.52 return -1;
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 // Bulk Operations
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Removes all of the elements from this list (optional operation).
17.68/7.52 * The list will be empty after this call returns.
17.68/7.52 *
17.68/7.52 * This implementation calls {@code removeRange(0, size())}.
17.68/7.52 *
17.68/7.52 *
Note that this implementation throws an
17.68/7.52 * {@code UnsupportedOperationException} unless {@code remove(int
17.68/7.52 * index)} or {@code removeRange(int fromIndex, int toIndex)} is
17.68/7.52 * overridden.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException if the {@code clear} operation
17.68/7.52 * is not supported by this list
17.68/7.52 */
17.68/7.52 public void clear() {
17.68/7.52 removeRange(0, size());
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 *
This implementation gets an iterator over the specified collection
17.68/7.52 * and iterates over it, inserting the elements obtained from the
17.68/7.52 * iterator into this list at the appropriate position, one at a time,
17.68/7.52 * using {@code add(int, E)}.
17.68/7.52 * Many implementations will override this method for efficiency.
17.68/7.52 *
17.68/7.52 *
Note that this implementation throws an
17.68/7.52 * {@code UnsupportedOperationException} unless
17.68/7.52 * {@link #add(int, Object) add(int, E)} is overridden.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public boolean addAll(int index, Collection extends E> c) {
17.68/7.52 rangeCheckForAdd(index);
17.68/7.52 boolean modified = false;
17.68/7.52 Iterator extends E> e = c.iterator();
17.68/7.52 while (e.hasNext()) {
17.68/7.52 add(index++, e.next());
17.68/7.52 modified = true;
17.68/7.52 }
17.68/7.52 return modified;
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 // Iterators
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Returns an iterator over the elements in this list in proper sequence.
17.68/7.52 *
17.68/7.52 *
This implementation returns a straightforward implementation of the
17.68/7.52 * iterator interface, relying on the backing list's {@code size()},
17.68/7.52 * {@code get(int)}, and {@code remove(int)} methods.
17.68/7.52 *
17.68/7.52 *
Note that the iterator returned by this method will throw an
17.68/7.52 * {@link UnsupportedOperationException} in response to its
17.68/7.52 * {@code remove} method unless the list's {@code remove(int)} method is
17.68/7.52 * overridden.
17.68/7.52 *
17.68/7.52 *
This implementation can be made to throw runtime exceptions in the
17.68/7.52 * face of concurrent modification, as described in the specification
17.68/7.52 * for the (protected) {@link #modCount} field.
17.68/7.52 *
17.68/7.52 * @return an iterator over the elements in this list in proper sequence
17.68/7.52 */
17.68/7.52 public Iterator iterator() {
17.68/7.52 return new Itr();
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation returns {@code listIterator(0)}.
17.68/7.52 *
17.68/7.52 * @see #listIterator(int)
17.68/7.52 */
17.68/7.52 public ListIterator listIterator() {
17.68/7.52 return listIterator(0);
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation returns a straightforward implementation of the
17.68/7.52 * {@code ListIterator} interface that extends the implementation of the
17.68/7.52 * {@code Iterator} interface returned by the {@code iterator()} method.
17.68/7.52 * The {@code ListIterator} implementation relies on the backing list's
17.68/7.52 * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
17.68/7.52 * and {@code remove(int)} methods.
17.68/7.52 *
17.68/7.52 *
Note that the list iterator returned by this implementation will
17.68/7.52 * throw an {@link UnsupportedOperationException} in response to its
17.68/7.52 * {@code remove}, {@code set} and {@code add} methods unless the
17.68/7.52 * list's {@code remove(int)}, {@code set(int, E)}, and
17.68/7.52 * {@code add(int, E)} methods are overridden.
17.68/7.52 *
17.68/7.52 *
This implementation can be made to throw runtime exceptions in the
17.68/7.52 * face of concurrent modification, as described in the specification for
17.68/7.52 * the (protected) {@link #modCount} field.
17.68/7.52 *
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public ListIterator listIterator(final int index) {
17.68/7.52 rangeCheckForAdd(index);
17.68/7.52
17.68/7.52 return new ListItr(index);
17.68/7.52 }
17.68/7.52
17.68/7.52 private class Itr implements Iterator {
17.68/7.52 /**
17.68/7.52 * Index of element to be returned by subsequent call to next.
17.68/7.52 */
17.68/7.52 int cursor = 0;
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Index of element returned by most recent call to next or
17.68/7.52 * previous. Reset to -1 if this element is deleted by a call
17.68/7.52 * to remove.
17.68/7.52 */
17.68/7.52 int lastRet = -1;
17.68/7.52
17.68/7.52 /**
17.68/7.52 * The modCount value that the iterator believes that the backing
17.68/7.52 * List should have. If this expectation is violated, the iterator
17.68/7.52 * has detected concurrent modification.
17.68/7.52 */
17.68/7.52 int expectedModCount = modCount;
17.68/7.52
17.68/7.52 public boolean hasNext() {
17.68/7.52 return cursor != size();
17.68/7.52 }
17.68/7.52
17.68/7.52 public E next() {
17.68/7.52 checkForComodification();
17.68/7.52 try {
17.68/7.52 int i = cursor;
17.68/7.52 E next = get(i);
17.68/7.52 lastRet = i;
17.68/7.52 cursor = i + 1;
17.68/7.52 return next;
17.68/7.52 } catch (IndexOutOfBoundsException e) {
17.68/7.52 checkForComodification();
17.68/7.52 throw new NoSuchElementException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 public void remove() {
17.68/7.52 if (lastRet < 0)
17.68/7.52 throw new IllegalStateException();
17.68/7.52 checkForComodification();
17.68/7.52
17.68/7.52 try {
17.68/7.52 AbstractList.this.remove(lastRet);
17.68/7.52 if (lastRet < cursor)
17.68/7.52 cursor--;
17.68/7.52 lastRet = -1;
17.68/7.52 expectedModCount = modCount;
17.68/7.52 } catch (IndexOutOfBoundsException e) {
17.68/7.52 throw new ConcurrentModificationException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 final void checkForComodification() {
17.68/7.52 if (modCount != expectedModCount)
17.68/7.52 throw new ConcurrentModificationException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 private class ListItr extends Itr implements ListIterator {
17.68/7.52 ListItr(int index) {
17.68/7.52 cursor = index;
17.68/7.52 }
17.68/7.52
17.68/7.52 public boolean hasPrevious() {
17.68/7.52 return cursor != 0;
17.68/7.52 }
17.68/7.52
17.68/7.52 public E previous() {
17.68/7.52 checkForComodification();
17.68/7.52 try {
17.68/7.52 int i = cursor - 1;
17.68/7.52 E previous = get(i);
17.68/7.52 lastRet = cursor = i;
17.68/7.52 return previous;
17.68/7.52 } catch (IndexOutOfBoundsException e) {
17.68/7.52 checkForComodification();
17.68/7.52 throw new NoSuchElementException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 public int nextIndex() {
17.68/7.52 return cursor;
17.68/7.52 }
17.68/7.52
17.68/7.52 public int previousIndex() {
17.68/7.52 return cursor-1;
17.68/7.52 }
17.68/7.52
17.68/7.52 public void set(E e) {
17.68/7.52 if (lastRet < 0)
17.68/7.52 throw new IllegalStateException();
17.68/7.52 checkForComodification();
17.68/7.52
17.68/7.52 try {
17.68/7.52 AbstractList.this.set(lastRet, e);
17.68/7.52 expectedModCount = modCount;
17.68/7.52 } catch (IndexOutOfBoundsException ex) {
17.68/7.52 throw new ConcurrentModificationException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 public void add(E e) {
17.68/7.52 checkForComodification();
17.68/7.52
17.68/7.52 try {
17.68/7.52 int i = cursor;
17.68/7.52 AbstractList.this.add(i, e);
17.68/7.52 lastRet = -1;
17.68/7.52 cursor = i + 1;
17.68/7.52 expectedModCount = modCount;
17.68/7.52 } catch (IndexOutOfBoundsException ex) {
17.68/7.52 throw new ConcurrentModificationException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * {@inheritDoc}
17.68/7.52 *
17.68/7.52 * This implementation returns a list that subclasses
17.68/7.52 * {@code AbstractList}. The subclass stores, in private fields, the
17.68/7.52 * offset of the subList within the backing list, the size of the subList
17.68/7.52 * (which can change over its lifetime), and the expected
17.68/7.52 * {@code modCount} value of the backing list. There are two variants
17.68/7.52 * of the subclass, one of which implements {@code RandomAccess}.
17.68/7.52 * If this list implements {@code RandomAccess} the returned list will
17.68/7.52 * be an instance of the subclass that implements {@code RandomAccess}.
17.68/7.52 *
17.68/7.52 *
The subclass's {@code set(int, E)}, {@code get(int)},
17.68/7.52 * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
17.68/7.52 * Collection)} and {@code removeRange(int, int)} methods all
17.68/7.52 * delegate to the corresponding methods on the backing abstract list,
17.68/7.52 * after bounds-checking the index and adjusting for the offset. The
17.68/7.52 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
17.68/7.52 * c)}.
17.68/7.52 *
17.68/7.52 *
The {@code listIterator(int)} method returns a "wrapper object"
17.68/7.52 * over a list iterator on the backing list, which is created with the
17.68/7.52 * corresponding method on the backing list. The {@code iterator} method
17.68/7.52 * merely returns {@code listIterator()}, and the {@code size} method
17.68/7.52 * merely returns the subclass's {@code size} field.
17.68/7.52 *
17.68/7.52 *
All methods first check to see if the actual {@code modCount} of
17.68/7.52 * the backing list is equal to its expected value, and throw a
17.68/7.52 * {@code ConcurrentModificationException} if it is not.
17.68/7.52 *
17.68/7.52 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
17.68/7.52 * {@code (fromIndex < 0 || toIndex > size)}
17.68/7.52 * @throws IllegalArgumentException if the endpoint indices are out of order
17.68/7.52 * {@code (fromIndex > toIndex)}
17.68/7.52 */
17.68/7.52 public List subList(int fromIndex, int toIndex) {
17.68/7.52 return (this instanceof RandomAccess ?
17.68/7.52 new RandomAccessSubList(this, fromIndex, toIndex) :
17.68/7.52 new SubList(this, fromIndex, toIndex));
17.68/7.52 }
17.68/7.52
17.68/7.52 // Comparison and hashing
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Compares the specified object with this list for equality. Returns
17.68/7.52 * {@code true} if and only if the specified object is also a list, both
17.68/7.52 * lists have the same size, and all corresponding pairs of elements in
17.68/7.52 * the two lists are equal. (Two elements {@code e1} and
17.68/7.52 * {@code e2} are equal if {@code (e1==null ? e2==null :
17.68/7.52 * e1.equals(e2))}.) In other words, two lists are defined to be
17.68/7.52 * equal if they contain the same elements in the same order.
17.68/7.52 *
17.68/7.52 * This implementation first checks if the specified object is this
17.68/7.52 * list. If so, it returns {@code true}; if not, it checks if the
17.68/7.52 * specified object is a list. If not, it returns {@code false}; if so,
17.68/7.52 * it iterates over both lists, comparing corresponding pairs of elements.
17.68/7.52 * If any comparison returns {@code false}, this method returns
17.68/7.52 * {@code false}. If either iterator runs out of elements before the
17.68/7.52 * other it returns {@code false} (as the lists are of unequal length);
17.68/7.52 * otherwise it returns {@code true} when the iterations complete.
17.68/7.52 *
17.68/7.52 * @param o the object to be compared for equality with this list
17.68/7.52 * @return {@code true} if the specified object is equal to this list
17.68/7.52 */
17.68/7.52 public boolean equals(Object o) {
17.68/7.52 if (o == this)
17.68/7.52 return true;
17.68/7.52 if (!(o instanceof List))
17.68/7.52 return false;
17.68/7.52
17.68/7.52 ListIterator e1 = listIterator();
17.68/7.52 ListIterator e2 = ((List) o).listIterator();
17.68/7.52 while(e1.hasNext() && e2.hasNext()) {
17.68/7.52 E o1 = e1.next();
17.68/7.52 Object o2 = e2.next();
17.68/7.52 if (!(o1==null ? o2==null : o1.equals(o2)))
17.68/7.52 return false;
17.68/7.52 }
17.68/7.52 return !(e1.hasNext() || e2.hasNext());
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Returns the hash code value for this list.
17.68/7.52 *
17.68/7.52 * This implementation uses exactly the code that is used to define the
17.68/7.52 * list hash function in the documentation for the {@link List#hashCode}
17.68/7.52 * method.
17.68/7.52 *
17.68/7.52 * @return the hash code value for this list
17.68/7.52 */
17.68/7.52 public int hashCode() {
17.68/7.52 int hashCode = 1;
17.68/7.52 Iterator it = this.iterator();
17.68/7.52 while (it.hasNext()) {
17.68/7.52 E e = it.next();
17.68/7.52 hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
17.68/7.52 }
17.68/7.52 return hashCode;
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Removes from this list all of the elements whose index is between
17.68/7.52 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
17.68/7.52 * Shifts any succeeding elements to the left (reduces their index).
17.68/7.52 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
17.68/7.52 * (If {@code toIndex==fromIndex}, this operation has no effect.)
17.68/7.52 *
17.68/7.52 * This method is called by the {@code clear} operation on this list
17.68/7.52 * and its subLists. Overriding this method to take advantage of
17.68/7.52 * the internals of the list implementation can substantially
17.68/7.52 * improve the performance of the {@code clear} operation on this list
17.68/7.52 * and its subLists.
17.68/7.52 *
17.68/7.52 *
This implementation gets a list iterator positioned before
17.68/7.52 * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
17.68/7.52 * followed by {@code ListIterator.remove} until the entire range has
17.68/7.52 * been removed. Note: if {@code ListIterator.remove} requires linear
17.68/7.52 * time, this implementation requires quadratic time.
17.68/7.52 *
17.68/7.52 * @param fromIndex index of first element to be removed
17.68/7.52 * @param toIndex index after last element to be removed
17.68/7.52 */
17.68/7.52 protected void removeRange(int fromIndex, int toIndex) {
17.68/7.52 ListIterator it = listIterator(fromIndex);
17.68/7.52 for (int i=0, n=toIndex-fromIndex; istructurally modified.
17.68/7.52 * Structural modifications are those that change the size of the
17.68/7.52 * list, or otherwise perturb it in such a fashion that iterations in
17.68/7.52 * progress may yield incorrect results.
17.68/7.52 *
17.68/7.52 * This field is used by the iterator and list iterator implementation
17.68/7.52 * returned by the {@code iterator} and {@code listIterator} methods.
17.68/7.52 * If the value of this field changes unexpectedly, the iterator (or list
17.68/7.52 * iterator) will throw a {@code ConcurrentModificationException} in
17.68/7.52 * response to the {@code next}, {@code remove}, {@code previous},
17.68/7.52 * {@code set} or {@code add} operations. This provides
17.68/7.52 * fail-fast behavior, rather than non-deterministic behavior in
17.68/7.52 * the face of concurrent modification during iteration.
17.68/7.52 *
17.68/7.52 *
Use of this field by subclasses is optional. If a subclass
17.68/7.52 * wishes to provide fail-fast iterators (and list iterators), then it
17.68/7.52 * merely has to increment this field in its {@code add(int, E)} and
17.68/7.52 * {@code remove(int)} methods (and any other methods that it overrides
17.68/7.52 * that result in structural modifications to the list). A single call to
17.68/7.52 * {@code add(int, E)} or {@code remove(int)} must add no more than
17.68/7.52 * one to this field, or the iterators (and list iterators) will throw
17.68/7.52 * bogus {@code ConcurrentModificationExceptions}. If an implementation
17.68/7.52 * does not wish to provide fail-fast iterators, this field may be
17.68/7.52 * ignored.
17.68/7.52 */
17.68/7.52 protected transient int modCount = 0;
17.68/7.52
17.68/7.52 private void rangeCheckForAdd(int index) {
17.68/7.52 if (index < 0 || index > size())
17.68/7.52 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
17.68/7.52 }
17.68/7.52
17.68/7.52 private String outOfBoundsMsg(int index) {
17.68/7.52 return "";
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 class SubList extends AbstractList {
17.68/7.52 private final AbstractList l;
17.68/7.52 private final int offset;
17.68/7.52 private int size;
17.68/7.52
17.68/7.52 SubList(AbstractList list, int fromIndex, int toIndex) {
17.68/7.52 if (fromIndex < 0)
17.68/7.52 throw new IndexOutOfBoundsException();
17.68/7.52 if (toIndex > list.size())
17.68/7.52 throw new IndexOutOfBoundsException();
17.68/7.52 if (fromIndex > toIndex)
17.68/7.52 throw new IllegalArgumentException();
17.68/7.52 l = list;
17.68/7.52 offset = fromIndex;
17.68/7.52 size = toIndex - fromIndex;
17.68/7.52 this.modCount = l.modCount;
17.68/7.52 }
17.68/7.52
17.68/7.52 public E set(int index, E element) {
17.68/7.52 rangeCheck(index);
17.68/7.52 checkForComodification();
17.68/7.52 return l.set(index+offset, element);
17.68/7.52 }
17.68/7.52
17.68/7.52 public E get(int index) {
17.68/7.52 rangeCheck(index);
17.68/7.52 checkForComodification();
17.68/7.52 return l.get(index+offset);
17.68/7.52 }
17.68/7.52
17.68/7.52 public int size() {
17.68/7.52 checkForComodification();
17.68/7.52 return size;
17.68/7.52 }
17.68/7.52
17.68/7.52 public void add(int index, E element) {
17.68/7.52 rangeCheckForAdd(index);
17.68/7.52 checkForComodification();
17.68/7.52 l.add(index+offset, element);
17.68/7.52 this.modCount = l.modCount;
17.68/7.52 size++;
17.68/7.52 }
17.68/7.52
17.68/7.52 public E remove(int index) {
17.68/7.52 rangeCheck(index);
17.68/7.52 checkForComodification();
17.68/7.52 E result = l.remove(index+offset);
17.68/7.52 this.modCount = l.modCount;
17.68/7.52 size--;
17.68/7.52 return result;
17.68/7.52 }
17.68/7.52
17.68/7.52 protected void removeRange(int fromIndex, int toIndex) {
17.68/7.52 checkForComodification();
17.68/7.52 l.removeRange(fromIndex+offset, toIndex+offset);
17.68/7.52 this.modCount = l.modCount;
17.68/7.52 size -= (toIndex-fromIndex);
17.68/7.52 }
17.68/7.52
17.68/7.52 public boolean addAll(Collection extends E> c) {
17.68/7.52 return addAll(size, c);
17.68/7.52 }
17.68/7.52
17.68/7.52 public boolean addAll(int index, Collection extends E> c) {
17.68/7.52 rangeCheckForAdd(index);
17.68/7.52 int cSize = c.size();
17.68/7.52 if (cSize==0)
17.68/7.52 return false;
17.68/7.52
17.68/7.52 checkForComodification();
17.68/7.52 l.addAll(offset+index, c);
17.68/7.52 this.modCount = l.modCount;
17.68/7.52 size += cSize;
17.68/7.52 return true;
17.68/7.52 }
17.68/7.52
17.68/7.52 public Iterator iterator() {
17.68/7.52 return listIterator();
17.68/7.52 }
17.68/7.52
17.68/7.52 public ListIterator listIterator(final int index) {
17.68/7.52 checkForComodification();
17.68/7.52 rangeCheckForAdd(index);
17.68/7.52
17.68/7.52 return new ListIterator() {
17.68/7.52 private final ListIterator i = l.listIterator(index+offset);
17.68/7.52
17.68/7.52 public boolean hasNext() {
17.68/7.52 return nextIndex() < size;
17.68/7.52 }
17.68/7.52
17.68/7.52 public E next() {
17.68/7.52 if (hasNext())
17.68/7.52 return i.next();
17.68/7.52 else
17.68/7.52 throw new NoSuchElementException();
17.68/7.52 }
17.68/7.52
17.68/7.52 public boolean hasPrevious() {
17.68/7.52 return previousIndex() >= 0;
17.68/7.52 }
17.68/7.52
17.68/7.52 public E previous() {
17.68/7.52 if (hasPrevious())
17.68/7.52 return i.previous();
17.68/7.52 else
17.68/7.52 throw new NoSuchElementException();
17.68/7.52 }
17.68/7.52
17.68/7.52 public int nextIndex() {
17.68/7.52 return i.nextIndex() - offset;
17.68/7.52 }
17.68/7.52
17.68/7.52 public int previousIndex() {
17.68/7.52 return i.previousIndex() - offset;
17.68/7.52 }
17.68/7.52
17.68/7.52 public void remove() {
17.68/7.52 i.remove();
17.68/7.52 SubList.this.modCount = l.modCount;
17.68/7.52 size--;
17.68/7.52 }
17.68/7.52
17.68/7.52 public void set(E e) {
17.68/7.52 i.set(e);
17.68/7.52 }
17.68/7.52
17.68/7.52 public void add(E e) {
17.68/7.52 i.add(e);
17.68/7.52 SubList.this.modCount = l.modCount;
17.68/7.52 size++;
17.68/7.52 }
17.68/7.52 };
17.68/7.52 }
17.68/7.52
17.68/7.52 public List subList(int fromIndex, int toIndex) {
17.68/7.52 return new SubList(this, fromIndex, toIndex);
17.68/7.52 }
17.68/7.52
17.68/7.52 private void rangeCheck(int index) {
17.68/7.52 if (index < 0 || index >= size)
17.68/7.52 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
17.68/7.52 }
17.68/7.52
17.68/7.52 private void rangeCheckForAdd(int index) {
17.68/7.52 if (index < 0 || index > size)
17.68/7.52 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
17.68/7.52 }
17.68/7.52
17.68/7.52 private String outOfBoundsMsg(int index) {
17.68/7.52 return "";
17.68/7.52 }
17.68/7.52
17.68/7.52 private void checkForComodification() {
17.68/7.52 if (this.modCount != l.modCount)
17.68/7.52 throw new ConcurrentModificationException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 class RandomAccessSubList extends SubList implements RandomAccess {
17.68/7.52 RandomAccessSubList(AbstractList list, int fromIndex, int toIndex) {
17.68/7.52 super(list, fromIndex, toIndex);
17.68/7.52 }
17.68/7.52
17.68/7.52 public List subList(int fromIndex, int toIndex) {
17.68/7.52 return new RandomAccessSubList(this, fromIndex, toIndex);
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 /*
17.68/7.52 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.52 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.52 *
17.68/7.52 * This code is free software; you can redistribute it and/or modify it
17.68/7.52 * under the terms of the GNU General Public License version 2 only, as
17.68/7.52 * published by the Free Software Foundation. Sun designates this
17.68/7.52 * particular file as subject to the "Classpath" exception as provided
17.68/7.52 * by Sun in the LICENSE file that accompanied this code.
17.68/7.52 *
17.68/7.52 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.52 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.52 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.52 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.52 * accompanied this code).
17.68/7.52 *
17.68/7.52 * You should have received a copy of the GNU General Public License version
17.68/7.52 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.52 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.52 *
17.68/7.52 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.52 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.52 * have any questions.
17.68/7.52 */
17.68/7.52
17.68/7.52 package javaUtilEx;
17.68/7.52
17.68/7.52 /**
17.68/7.52 * This class provides a skeletal implementation of the List
17.68/7.52 * interface to minimize the effort required to implement this interface
17.68/7.52 * backed by a "sequential access" data store (such as a linked list). For
17.68/7.52 * random access data (such as an array), AbstractList should be used
17.68/7.52 * in preference to this class.
17.68/7.52 *
17.68/7.52 * This class is the opposite of the AbstractList class in the sense
17.68/7.52 * that it implements the "random access" methods (get(int index),
17.68/7.52 * set(int index, E element), add(int index, E element) and
17.68/7.52 * remove(int index)) on top of the list's list iterator, instead of
17.68/7.52 * the other way around.
17.68/7.52 *
17.68/7.52 * To implement a list the programmer needs only to extend this class and
17.68/7.52 * provide implementations for the listIterator and size
17.68/7.52 * methods. For an unmodifiable list, the programmer need only implement the
17.68/7.52 * list iterator's hasNext, next, hasPrevious,
17.68/7.52 * previous and index methods.
17.68/7.52 *
17.68/7.52 * For a modifiable list the programmer should additionally implement the list
17.68/7.52 * iterator's set method. For a variable-size list the programmer
17.68/7.52 * should additionally implement the list iterator's remove and
17.68/7.52 * add methods.
17.68/7.52 *
17.68/7.52 * The programmer should generally provide a void (no argument) and collection
17.68/7.52 * constructor, as per the recommendation in the Collection interface
17.68/7.52 * specification.
17.68/7.52 *
17.68/7.52 * This class is a member of the
17.68/7.52 *
17.68/7.52 * Java Collections Framework.
17.68/7.52 *
17.68/7.52 * @author Josh Bloch
17.68/7.52 * @author Neal Gafter
17.68/7.52 * @see Collection
17.68/7.52 * @see List
17.68/7.52 * @see AbstractList
17.68/7.52 * @see AbstractCollection
17.68/7.52 * @since 1.2
17.68/7.52 */
17.68/7.52
17.68/7.52 public abstract class AbstractSequentialList extends AbstractList {
17.68/7.52 /**
17.68/7.52 * Sole constructor. (For invocation by subclass constructors, typically
17.68/7.52 * implicit.)
17.68/7.52 */
17.68/7.52 protected AbstractSequentialList() {
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Returns the element at the specified position in this list.
17.68/7.52 *
17.68/7.52 * This implementation first gets a list iterator pointing to the
17.68/7.52 * indexed element (with listIterator(index)). Then, it gets
17.68/7.52 * the element using ListIterator.next and returns it.
17.68/7.52 *
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public E get(int index) {
17.68/7.52 try {
17.68/7.52 return listIterator(index).next();
17.68/7.52 } catch (NoSuchElementException exc) {
17.68/7.52 throw new IndexOutOfBoundsException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Replaces the element at the specified position in this list with the
17.68/7.52 * specified element (optional operation).
17.68/7.52 *
17.68/7.52 *
This implementation first gets a list iterator pointing to the
17.68/7.52 * indexed element (with listIterator(index)). Then, it gets
17.68/7.52 * the current element using ListIterator.next and replaces it
17.68/7.52 * with ListIterator.set.
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException if the list iterator does not
17.68/7.52 * implement the set operation.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public E set(int index, E element) {
17.68/7.52 try {
17.68/7.52 ListIterator e = listIterator(index);
17.68/7.52 E oldVal = e.next();
17.68/7.52 e.set(element);
17.68/7.52 return oldVal;
17.68/7.52 } catch (NoSuchElementException exc) {
17.68/7.52 throw new IndexOutOfBoundsException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Inserts the specified element at the specified position in this list
17.68/7.52 * (optional operation). Shifts the element currently at that position
17.68/7.52 * (if any) and any subsequent elements to the right (adds one to their
17.68/7.52 * indices).
17.68/7.52 *
17.68/7.52 * This implementation first gets a list iterator pointing to the
17.68/7.52 * indexed element (with listIterator(index)). Then, it
17.68/7.52 * inserts the specified element with ListIterator.add.
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException if the list iterator does not
17.68/7.52 * implement the add operation.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public void add(int index, E element) {
17.68/7.52 try {
17.68/7.52 listIterator(index).add(element);
17.68/7.52 } catch (NoSuchElementException exc) {
17.68/7.52 throw new IndexOutOfBoundsException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Removes the element at the specified position in this list (optional
17.68/7.52 * operation). Shifts any subsequent elements to the left (subtracts one
17.68/7.52 * from their indices). Returns the element that was removed from the
17.68/7.52 * list.
17.68/7.52 *
17.68/7.52 *
This implementation first gets a list iterator pointing to the
17.68/7.52 * indexed element (with listIterator(index)). Then, it removes
17.68/7.52 * the element with ListIterator.remove.
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException if the list iterator does not
17.68/7.52 * implement the remove operation.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public E remove(int index) {
17.68/7.52 try {
17.68/7.52 ListIterator e = listIterator(index);
17.68/7.52 E outCast = e.next();
17.68/7.52 e.remove();
17.68/7.52 return outCast;
17.68/7.52 } catch (NoSuchElementException exc) {
17.68/7.52 throw new IndexOutOfBoundsException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 // Bulk Operations
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Inserts all of the elements in the specified collection into this
17.68/7.52 * list at the specified position (optional operation). Shifts the
17.68/7.52 * element currently at that position (if any) and any subsequent
17.68/7.52 * elements to the right (increases their indices). The new elements
17.68/7.52 * will appear in this list in the order that they are returned by the
17.68/7.52 * specified collection's iterator. The behavior of this operation is
17.68/7.52 * undefined if the specified collection is modified while the
17.68/7.52 * operation is in progress. (Note that this will occur if the specified
17.68/7.52 * collection is this list, and it's nonempty.)
17.68/7.52 *
17.68/7.52 * This implementation gets an iterator over the specified collection and
17.68/7.52 * a list iterator over this list pointing to the indexed element (with
17.68/7.52 * listIterator(index)). Then, it iterates over the specified
17.68/7.52 * collection, inserting the elements obtained from the iterator into this
17.68/7.52 * list, one at a time, using ListIterator.add followed by
17.68/7.52 * ListIterator.next (to skip over the added element).
17.68/7.52 *
17.68/7.52 *
Note that this implementation will throw an
17.68/7.52 * UnsupportedOperationException if the list iterator returned by
17.68/7.52 * the listIterator method does not implement the add
17.68/7.52 * operation.
17.68/7.52 *
17.68/7.52 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.52 * @throws ClassCastException {@inheritDoc}
17.68/7.52 * @throws NullPointerException {@inheritDoc}
17.68/7.52 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public boolean addAll(int index, Collection extends E> c) {
17.68/7.52 try {
17.68/7.52 boolean modified = false;
17.68/7.52 ListIterator e1 = listIterator(index);
17.68/7.52 Iterator extends E> e2 = c.iterator();
17.68/7.52 while (e2.hasNext()) {
17.68/7.52 e1.add(e2.next());
17.68/7.52 modified = true;
17.68/7.52 }
17.68/7.52 return modified;
17.68/7.52 } catch (NoSuchElementException exc) {
17.68/7.52 throw new IndexOutOfBoundsException();
17.68/7.52 }
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 // Iterators
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Returns an iterator over the elements in this list (in proper
17.68/7.52 * sequence).
17.68/7.52 *
17.68/7.52 * This implementation merely returns a list iterator over the list.
17.68/7.52 *
17.68/7.52 * @return an iterator over the elements in this list (in proper sequence)
17.68/7.52 */
17.68/7.52 public Iterator iterator() {
17.68/7.52 return listIterator();
17.68/7.52 }
17.68/7.52
17.68/7.52 /**
17.68/7.52 * Returns a list iterator over the elements in this list (in proper
17.68/7.52 * sequence).
17.68/7.52 *
17.68/7.52 * @param index index of first element to be returned from the list
17.68/7.52 * iterator (by a call to the next
method)
17.68/7.52 * @return a list iterator over the elements in this list (in proper
17.68/7.52 * sequence)
17.68/7.52 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.52 */
17.68/7.52 public abstract ListIterator listIterator(int index);
17.68/7.52 }
17.68/7.52
17.68/7.52
17.68/7.52 /*
17.68/7.52 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.52 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.52 *
17.68/7.52 * This code is free software; you can redistribute it and/or modify it
17.68/7.52 * under the terms of the GNU General Public License version 2 only, as
17.68/7.52 * published by the Free Software Foundation. Sun designates this
17.68/7.52 * particular file as subject to the "Classpath" exception as provided
17.68/7.52 * by Sun in the LICENSE file that accompanied this code.
17.68/7.52 *
17.68/7.52 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.53 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.53 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.53 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.53 * accompanied this code).
17.68/7.53 *
17.68/7.53 * You should have received a copy of the GNU General Public License version
17.68/7.53 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.53 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.53 *
17.68/7.53 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.53 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.53 * have any questions.
17.68/7.53 */
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * The root interface in the collection hierarchy. A collection
17.68/7.53 * represents a group of objects, known as its elements. Some
17.68/7.53 * collections allow duplicate elements and others do not. Some are ordered
17.68/7.53 * and others unordered. The JDK does not provide any direct
17.68/7.53 * implementations of this interface: it provides implementations of more
17.68/7.53 * specific subinterfaces like Set and List. This interface
17.68/7.53 * is typically used to pass collections around and manipulate them where
17.68/7.53 * maximum generality is desired.
17.68/7.53 *
17.68/7.53 * Bags or multisets (unordered collections that may contain
17.68/7.53 * duplicate elements) should implement this interface directly.
17.68/7.53 *
17.68/7.53 *
All general-purpose Collection implementation classes (which
17.68/7.53 * typically implement Collection indirectly through one of its
17.68/7.53 * subinterfaces) should provide two "standard" constructors: a void (no
17.68/7.53 * arguments) constructor, which creates an empty collection, and a
17.68/7.53 * constructor with a single argument of type Collection, which
17.68/7.53 * creates a new collection with the same elements as its argument. In
17.68/7.53 * effect, the latter constructor allows the user to copy any collection,
17.68/7.53 * producing an equivalent collection of the desired implementation type.
17.68/7.53 * There is no way to enforce this convention (as interfaces cannot contain
17.68/7.53 * constructors) but all of the general-purpose Collection
17.68/7.53 * implementations in the Java platform libraries comply.
17.68/7.53 *
17.68/7.53 *
The "destructive" methods contained in this interface, that is, the
17.68/7.53 * methods that modify the collection on which they operate, are specified to
17.68/7.53 * throw UnsupportedOperationException if this collection does not
17.68/7.53 * support the operation. If this is the case, these methods may, but are not
17.68/7.53 * required to, throw an UnsupportedOperationException if the
17.68/7.53 * invocation would have no effect on the collection. For example, invoking
17.68/7.53 * the {@link #addAll(Collection)} method on an unmodifiable collection may,
17.68/7.53 * but is not required to, throw the exception if the collection to be added
17.68/7.53 * is empty.
17.68/7.53 *
17.68/7.53 *
Some collection implementations have restrictions on the elements that
17.68/7.53 * they may contain. For example, some implementations prohibit null elements,
17.68/7.53 * and some have restrictions on the types of their elements. Attempting to
17.68/7.53 * add an ineligible element throws an unchecked exception, typically
17.68/7.53 * NullPointerException or ClassCastException. Attempting
17.68/7.53 * to query the presence of an ineligible element may throw an exception,
17.68/7.53 * or it may simply return false; some implementations will exhibit the former
17.68/7.53 * behavior and some will exhibit the latter. More generally, attempting an
17.68/7.53 * operation on an ineligible element whose completion would not result in
17.68/7.53 * the insertion of an ineligible element into the collection may throw an
17.68/7.53 * exception or it may succeed, at the option of the implementation.
17.68/7.53 * Such exceptions are marked as "optional" in the specification for this
17.68/7.53 * interface.
17.68/7.53 *
17.68/7.53 *
It is up to each collection to determine its own synchronization
17.68/7.53 * policy. In the absence of a stronger guarantee by the
17.68/7.53 * implementation, undefined behavior may result from the invocation
17.68/7.53 * of any method on a collection that is being mutated by another
17.68/7.53 * thread; this includes direct invocations, passing the collection to
17.68/7.53 * a method that might perform invocations, and using an existing
17.68/7.53 * iterator to examine the collection.
17.68/7.53 *
17.68/7.53 *
Many methods in Collections Framework interfaces are defined in
17.68/7.53 * terms of the {@link Object#equals(Object) equals} method. For example,
17.68/7.53 * the specification for the {@link #contains(Object) contains(Object o)}
17.68/7.53 * method says: "returns true if and only if this collection
17.68/7.53 * contains at least one element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e))." This specification should
17.68/7.53 * not be construed to imply that invoking Collection.contains
17.68/7.53 * with a non-null argument o will cause o.equals(e) to be
17.68/7.53 * invoked for any element e. Implementations are free to implement
17.68/7.53 * optimizations whereby the equals invocation is avoided, for
17.68/7.53 * example, by first comparing the hash codes of the two elements. (The
17.68/7.53 * {@link Object#hashCode()} specification guarantees that two objects with
17.68/7.53 * unequal hash codes cannot be equal.) More generally, implementations of
17.68/7.53 * the various Collections Framework interfaces are free to take advantage of
17.68/7.53 * the specified behavior of underlying {@link Object} methods wherever the
17.68/7.53 * implementor deems it appropriate.
17.68/7.53 *
17.68/7.53 *
This interface is a member of the
17.68/7.53 *
17.68/7.53 * Java Collections Framework.
17.68/7.53 *
17.68/7.53 * @author Josh Bloch
17.68/7.53 * @author Neal Gafter
17.68/7.53 * @see Set
17.68/7.53 * @see List
17.68/7.53 * @see Map
17.68/7.53 * @see SortedSet
17.68/7.53 * @see SortedMap
17.68/7.53 * @see HashSet
17.68/7.53 * @see TreeSet
17.68/7.53 * @see ArrayList
17.68/7.53 * @see LinkedList
17.68/7.53 * @see Vector
17.68/7.53 * @see Collections
17.68/7.53 * @see Arrays
17.68/7.53 * @see AbstractCollection
17.68/7.53 * @since 1.2
17.68/7.53 */
17.68/7.53
17.68/7.53 public interface Collection {
17.68/7.53 // Query Operations
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the number of elements in this collection. If this collection
17.68/7.53 * contains more than Integer.MAX_VALUE elements, returns
17.68/7.53 * Integer.MAX_VALUE.
17.68/7.53 *
17.68/7.53 * @return the number of elements in this collection
17.68/7.53 */
17.68/7.53 int size();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns true if this collection contains no elements.
17.68/7.53 *
17.68/7.53 * @return true if this collection contains no elements
17.68/7.53 */
17.68/7.53 boolean isEmpty();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns true if this collection contains the specified element.
17.68/7.53 * More formally, returns true if and only if this collection
17.68/7.53 * contains at least one element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e)).
17.68/7.53 *
17.68/7.53 * @param o element whose presence in this collection is to be tested
17.68/7.53 * @return true if this collection contains the specified
17.68/7.53 * element
17.68/7.53 * @throws ClassCastException if the type of the specified element
17.68/7.53 * is incompatible with this collection (optional)
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * collection does not permit null elements (optional)
17.68/7.53 */
17.68/7.53 boolean contains(Object o);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns an iterator over the elements in this collection. There are no
17.68/7.53 * guarantees concerning the order in which the elements are returned
17.68/7.53 * (unless this collection is an instance of some class that provides a
17.68/7.53 * guarantee).
17.68/7.53 *
17.68/7.53 * @return an Iterator over the elements in this collection
17.68/7.53 */
17.68/7.53 Iterator iterator();
17.68/7.53
17.68/7.53 // Modification Operations
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Ensures that this collection contains the specified element (optional
17.68/7.53 * operation). Returns true if this collection changed as a
17.68/7.53 * result of the call. (Returns false if this collection does
17.68/7.53 * not permit duplicates and already contains the specified element.)
17.68/7.53 *
17.68/7.53 * Collections that support this operation may place limitations on what
17.68/7.53 * elements may be added to this collection. In particular, some
17.68/7.53 * collections will refuse to add null elements, and others will
17.68/7.53 * impose restrictions on the type of elements that may be added.
17.68/7.53 * Collection classes should clearly specify in their documentation any
17.68/7.53 * restrictions on what elements may be added.
17.68/7.53 *
17.68/7.53 * If a collection refuses to add a particular element for any reason
17.68/7.53 * other than that it already contains the element, it must throw
17.68/7.53 * an exception (rather than returning false). This preserves
17.68/7.53 * the invariant that a collection always contains the specified element
17.68/7.53 * after this call returns.
17.68/7.53 *
17.68/7.53 * @param e element whose presence in this collection is to be ensured
17.68/7.53 * @return true if this collection changed as a result of the
17.68/7.53 * call
17.68/7.53 * @throws UnsupportedOperationException if the add operation
17.68/7.53 * is not supported by this collection
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this collection
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * collection does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the element
17.68/7.53 * prevents it from being added to this collection
17.68/7.53 * @throws IllegalStateException if the element cannot be added at this
17.68/7.53 * time due to insertion restrictions
17.68/7.53 */
17.68/7.53 boolean add(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes a single instance of the specified element from this
17.68/7.53 * collection, if it is present (optional operation). More formally,
17.68/7.53 * removes an element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e)), if
17.68/7.53 * this collection contains one or more such elements. Returns
17.68/7.53 * true if this collection contained the specified element (or
17.68/7.53 * equivalently, if this collection changed as a result of the call).
17.68/7.53 *
17.68/7.53 * @param o element to be removed from this collection, if present
17.68/7.53 * @return true if an element was removed as a result of this call
17.68/7.53 * @throws ClassCastException if the type of the specified element
17.68/7.53 * is incompatible with this collection (optional)
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * collection does not permit null elements (optional)
17.68/7.53 * @throws UnsupportedOperationException if the remove operation
17.68/7.53 * is not supported by this collection
17.68/7.53 */
17.68/7.53 boolean remove(Object o);
17.68/7.53
17.68/7.53
17.68/7.53 // Bulk Operations
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns true if this collection contains all of the elements
17.68/7.53 * in the specified collection.
17.68/7.53 *
17.68/7.53 * @param c collection to be checked for containment in this collection
17.68/7.53 * @return true if this collection contains all of the elements
17.68/7.53 * in the specified collection
17.68/7.53 * @throws ClassCastException if the types of one or more elements
17.68/7.53 * in the specified collection are incompatible with this
17.68/7.53 * collection (optional)
17.68/7.53 * @throws NullPointerException if the specified collection contains one
17.68/7.53 * or more null elements and this collection does not permit null
17.68/7.53 * elements (optional), or if the specified collection is null
17.68/7.53 * @see #contains(Object)
17.68/7.53 */
17.68/7.53 boolean containsAll(Collection> c);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Adds all of the elements in the specified collection to this collection
17.68/7.53 * (optional operation). The behavior of this operation is undefined if
17.68/7.53 * the specified collection is modified while the operation is in progress.
17.68/7.53 * (This implies that the behavior of this call is undefined if the
17.68/7.53 * specified collection is this collection, and this collection is
17.68/7.53 * nonempty.)
17.68/7.53 *
17.68/7.53 * @param c collection containing elements to be added to this collection
17.68/7.53 * @return true if this collection changed as a result of the call
17.68/7.53 * @throws UnsupportedOperationException if the addAll operation
17.68/7.53 * is not supported by this collection
17.68/7.53 * @throws ClassCastException if the class of an element of the specified
17.68/7.53 * collection prevents it from being added to this collection
17.68/7.53 * @throws NullPointerException if the specified collection contains a
17.68/7.53 * null element and this collection does not permit null elements,
17.68/7.53 * or if the specified collection is null
17.68/7.53 * @throws IllegalArgumentException if some property of an element of the
17.68/7.53 * specified collection prevents it from being added to this
17.68/7.53 * collection
17.68/7.53 * @throws IllegalStateException if not all the elements can be added at
17.68/7.53 * this time due to insertion restrictions
17.68/7.53 * @see #add(Object)
17.68/7.53 */
17.68/7.53 boolean addAll(Collection extends E> c);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes all of this collection's elements that are also contained in the
17.68/7.53 * specified collection (optional operation). After this call returns,
17.68/7.53 * this collection will contain no elements in common with the specified
17.68/7.53 * collection.
17.68/7.53 *
17.68/7.53 * @param c collection containing elements to be removed from this collection
17.68/7.53 * @return true if this collection changed as a result of the
17.68/7.53 * call
17.68/7.53 * @throws UnsupportedOperationException if the removeAll method
17.68/7.53 * is not supported by this collection
17.68/7.53 * @throws ClassCastException if the types of one or more elements
17.68/7.53 * in this collection are incompatible with the specified
17.68/7.53 * collection (optional)
17.68/7.53 * @throws NullPointerException if this collection contains one or more
17.68/7.53 * null elements and the specified collection does not support
17.68/7.53 * null elements (optional), or if the specified collection is null
17.68/7.53 * @see #remove(Object)
17.68/7.53 * @see #contains(Object)
17.68/7.53 */
17.68/7.53 boolean removeAll(Collection> c);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retains only the elements in this collection that are contained in the
17.68/7.53 * specified collection (optional operation). In other words, removes from
17.68/7.53 * this collection all of its elements that are not contained in the
17.68/7.53 * specified collection.
17.68/7.53 *
17.68/7.53 * @param c collection containing elements to be retained in this collection
17.68/7.53 * @return true if this collection changed as a result of the call
17.68/7.53 * @throws UnsupportedOperationException if the retainAll operation
17.68/7.53 * is not supported by this collection
17.68/7.53 * @throws ClassCastException if the types of one or more elements
17.68/7.53 * in this collection are incompatible with the specified
17.68/7.53 * collection (optional)
17.68/7.53 * @throws NullPointerException if this collection contains one or more
17.68/7.53 * null elements and the specified collection does not permit null
17.68/7.53 * elements (optional), or if the specified collection is null
17.68/7.53 * @see #remove(Object)
17.68/7.53 * @see #contains(Object)
17.68/7.53 */
17.68/7.53 boolean retainAll(Collection> c);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes all of the elements from this collection (optional operation).
17.68/7.53 * The collection will be empty after this method returns.
17.68/7.53 *
17.68/7.53 * @throws UnsupportedOperationException if the clear operation
17.68/7.53 * is not supported by this collection
17.68/7.53 */
17.68/7.53 void clear();
17.68/7.53
17.68/7.53
17.68/7.53 // Comparison and hashing
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Compares the specified object with this collection for equality.
17.68/7.53 *
17.68/7.53 * While the Collection interface adds no stipulations to the
17.68/7.53 * general contract for the Object.equals, programmers who
17.68/7.53 * implement the Collection interface "directly" (in other words,
17.68/7.53 * create a class that is a Collection but is not a Set
17.68/7.53 * or a List) must exercise care if they choose to override the
17.68/7.53 * Object.equals. It is not necessary to do so, and the simplest
17.68/7.53 * course of action is to rely on Object's implementation, but
17.68/7.53 * the implementor may wish to implement a "value comparison" in place of
17.68/7.53 * the default "reference comparison." (The List and
17.68/7.53 * Set interfaces mandate such value comparisons.)
17.68/7.53 *
17.68/7.53 * The general contract for the Object.equals method states that
17.68/7.53 * equals must be symmetric (in other words, a.equals(b) if and
17.68/7.53 * only if b.equals(a)). The contracts for List.equals
17.68/7.53 * and Set.equals state that lists are only equal to other lists,
17.68/7.53 * and sets to other sets. Thus, a custom equals method for a
17.68/7.53 * collection class that implements neither the List nor
17.68/7.53 * Set interface must return false when this collection
17.68/7.53 * is compared to any list or set. (By the same logic, it is not possible
17.68/7.53 * to write a class that correctly implements both the Set and
17.68/7.53 * List interfaces.)
17.68/7.53 *
17.68/7.53 * @param o object to be compared for equality with this collection
17.68/7.53 * @return true if the specified object is equal to this
17.68/7.53 * collection
17.68/7.53 *
17.68/7.53 * @see Object#equals(Object)
17.68/7.53 * @see Set#equals(Object)
17.68/7.53 * @see List#equals(Object)
17.68/7.53 */
17.68/7.53 boolean equals(Object o);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the hash code value for this collection. While the
17.68/7.53 * Collection interface adds no stipulations to the general
17.68/7.53 * contract for the Object.hashCode method, programmers should
17.68/7.53 * take note that any class that overrides the Object.equals
17.68/7.53 * method must also override the Object.hashCode method in order
17.68/7.53 * to satisfy the general contract for the Object.hashCodemethod.
17.68/7.53 * In particular, c1.equals(c2) implies that
17.68/7.53 * c1.hashCode()==c2.hashCode().
17.68/7.53 *
17.68/7.53 * @return the hash code value for this collection
17.68/7.53 *
17.68/7.53 * @see Object#hashCode()
17.68/7.53 * @see Object#equals(Object)
17.68/7.53 */
17.68/7.53 int hashCode();
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 /*
17.68/7.53 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.53 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.53 *
17.68/7.53 * This code is free software; you can redistribute it and/or modify it
17.68/7.53 * under the terms of the GNU General Public License version 2 only, as
17.68/7.53 * published by the Free Software Foundation. Sun designates this
17.68/7.53 * particular file as subject to the "Classpath" exception as provided
17.68/7.53 * by Sun in the LICENSE file that accompanied this code.
17.68/7.53 *
17.68/7.53 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.53 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.53 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.53 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.53 * accompanied this code).
17.68/7.53 *
17.68/7.53 * You should have received a copy of the GNU General Public License version
17.68/7.53 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.53 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.53 *
17.68/7.53 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.53 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.53 * have any questions.
17.68/7.53 */
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * This exception may be thrown by methods that have detected concurrent
17.68/7.53 * modification of an object when such modification is not permissible.
17.68/7.53 *
17.68/7.53 * For example, it is not generally permissible for one thread to modify a Collection
17.68/7.53 * while another thread is iterating over it. In general, the results of the
17.68/7.53 * iteration are undefined under these circumstances. Some Iterator
17.68/7.53 * implementations (including those of all the general purpose collection implementations
17.68/7.53 * provided by the JRE) may choose to throw this exception if this behavior is
17.68/7.53 * detected. Iterators that do this are known as fail-fast iterators,
17.68/7.53 * as they fail quickly and cleanly, rather that risking arbitrary,
17.68/7.53 * non-deterministic behavior at an undetermined time in the future.
17.68/7.53 *
17.68/7.53 * Note that this exception does not always indicate that an object has
17.68/7.53 * been concurrently modified by a different thread. If a single
17.68/7.53 * thread issues a sequence of method invocations that violates the
17.68/7.53 * contract of an object, the object may throw this exception. For
17.68/7.53 * example, if a thread modifies a collection directly while it is
17.68/7.53 * iterating over the collection with a fail-fast iterator, the iterator
17.68/7.53 * will throw this exception.
17.68/7.53 *
17.68/7.53 *
Note that fail-fast behavior cannot be guaranteed as it is, generally
17.68/7.53 * speaking, impossible to make any hard guarantees in the presence of
17.68/7.53 * unsynchronized concurrent modification. Fail-fast operations
17.68/7.53 * throw ConcurrentModificationException on a best-effort basis.
17.68/7.53 * Therefore, it would be wrong to write a program that depended on this
17.68/7.53 * exception for its correctness: ConcurrentModificationException
17.68/7.53 * should be used only to detect bugs.
17.68/7.53 *
17.68/7.53 * @author Josh Bloch
17.68/7.53 * @see Collection
17.68/7.53 * @see Iterator
17.68/7.53 * @see ListIterator
17.68/7.53 * @see Vector
17.68/7.53 * @see LinkedList
17.68/7.53 * @see HashSet
17.68/7.53 * @see Hashtable
17.68/7.53 * @see TreeMap
17.68/7.53 * @see AbstractList
17.68/7.53 * @since 1.2
17.68/7.53 */
17.68/7.53 public class ConcurrentModificationException extends RuntimeException {
17.68/7.53 /**
17.68/7.53 * Constructs a ConcurrentModificationException with no
17.68/7.53 * detail message.
17.68/7.53 */
17.68/7.53 public ConcurrentModificationException() {
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs a ConcurrentModificationException with the
17.68/7.53 * specified detail message.
17.68/7.53 *
17.68/7.53 * @param message the detail message pertaining to this exception.
17.68/7.53 */
17.68/7.53 public ConcurrentModificationException(String message) {
17.68/7.53 super(message);
17.68/7.53 }
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 /*
17.68/7.53 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.53 *
17.68/7.53 * This code is free software; you can redistribute it and/or modify it
17.68/7.53 * under the terms of the GNU General Public License version 2 only, as
17.68/7.53 * published by the Free Software Foundation. Sun designates this
17.68/7.53 * particular file as subject to the "Classpath" exception as provided
17.68/7.53 * by Sun in the LICENSE file that accompanied this code.
17.68/7.53 *
17.68/7.53 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.53 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.53 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.53 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.53 * accompanied this code).
17.68/7.53 *
17.68/7.53 * You should have received a copy of the GNU General Public License version
17.68/7.53 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.53 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.53 *
17.68/7.53 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.53 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.53 * have any questions.
17.68/7.53 */
17.68/7.53
17.68/7.53 /*
17.68/7.53 * This file is available under and governed by the GNU General Public
17.68/7.53 * License version 2 only, as published by the Free Software Foundation.
17.68/7.53 * However, the following notice accompanied the original version of this
17.68/7.53 * file:
17.68/7.53 *
17.68/7.53 * Written by Doug Lea and Josh Bloch with assistance from members of
17.68/7.53 * JCP JSR-166 Expert Group and released to the public domain, as explained
17.68/7.53 * at http://creativecommons.org/licenses/publicdomain
17.68/7.53 */
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * A linear collection that supports element insertion and removal at
17.68/7.53 * both ends. The name deque is short for "double ended queue"
17.68/7.53 * and is usually pronounced "deck". Most Deque
17.68/7.53 * implementations place no fixed limits on the number of elements
17.68/7.53 * they may contain, but this interface supports capacity-restricted
17.68/7.53 * deques as well as those with no fixed size limit.
17.68/7.53 *
17.68/7.53 *
This interface defines methods to access the elements at both
17.68/7.53 * ends of the deque. Methods are provided to insert, remove, and
17.68/7.53 * examine the element. Each of these methods exists in two forms:
17.68/7.53 * one throws an exception if the operation fails, the other returns a
17.68/7.53 * special value (either null or false, depending on
17.68/7.53 * the operation). The latter form of the insert operation is
17.68/7.53 * designed specifically for use with capacity-restricted
17.68/7.53 * Deque implementations; in most implementations, insert
17.68/7.53 * operations cannot fail.
17.68/7.53 *
17.68/7.53 *
The twelve methods described above are summarized in the
17.68/7.53 * following table:
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * |
17.68/7.53 * First Element (Head) |
17.68/7.53 * Last Element (Tail) |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * |
17.68/7.53 * Throws exception |
17.68/7.53 * Special value |
17.68/7.53 * Throws exception |
17.68/7.53 * Special value |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * Insert |
17.68/7.53 * {@link #addFirst addFirst(e)} |
17.68/7.53 * {@link #offerFirst offerFirst(e)} |
17.68/7.53 * {@link #addLast addLast(e)} |
17.68/7.53 * {@link #offerLast offerLast(e)} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * Remove |
17.68/7.53 * {@link #removeFirst removeFirst()} |
17.68/7.53 * {@link #pollFirst pollFirst()} |
17.68/7.53 * {@link #removeLast removeLast()} |
17.68/7.53 * {@link #pollLast pollLast()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * Examine |
17.68/7.53 * {@link #getFirst getFirst()} |
17.68/7.53 * {@link #peekFirst peekFirst()} |
17.68/7.53 * {@link #getLast getLast()} |
17.68/7.53 * {@link #peekLast peekLast()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * This interface extends the {@link Queue} interface. When a deque is
17.68/7.53 * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
17.68/7.53 * added at the end of the deque and removed from the beginning. The methods
17.68/7.53 * inherited from the Queue interface are precisely equivalent to
17.68/7.53 * Deque methods as indicated in the following table:
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * Queue Method |
17.68/7.53 * Equivalent Deque Method |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link java.util.Queue#add add(e)} |
17.68/7.53 * {@link #addLast addLast(e)} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link java.util.Queue#offer offer(e)} |
17.68/7.53 * {@link #offerLast offerLast(e)} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link java.util.Queue#remove remove()} |
17.68/7.53 * {@link #removeFirst removeFirst()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link java.util.Queue#poll poll()} |
17.68/7.53 * {@link #pollFirst pollFirst()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link java.util.Queue#element element()} |
17.68/7.53 * {@link #getFirst getFirst()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link java.util.Queue#peek peek()} |
17.68/7.53 * {@link #peek peekFirst()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * Deques can also be used as LIFO (Last-In-First-Out) stacks. This
17.68/7.53 * interface should be used in preference to the legacy {@link Stack} class.
17.68/7.53 * When a deque is used as a stack, elements are pushed and popped from the
17.68/7.53 * beginning of the deque. Stack methods are precisely equivalent to
17.68/7.53 * Deque methods as indicated in the table below:
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * Stack Method |
17.68/7.53 * Equivalent Deque Method |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link #push push(e)} |
17.68/7.53 * {@link #addFirst addFirst(e)} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link #pop pop()} |
17.68/7.53 * {@link #removeFirst removeFirst()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * {@link #peek peek()} |
17.68/7.53 * {@link #peekFirst peekFirst()} |
17.68/7.53 *
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * Note that the {@link #peek peek} method works equally well when
17.68/7.53 * a deque is used as a queue or a stack; in either case, elements are
17.68/7.53 * drawn from the beginning of the deque.
17.68/7.53 *
17.68/7.53 *
This interface provides two methods to remove interior
17.68/7.53 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
17.68/7.53 * {@link #removeLastOccurrence removeLastOccurrence}.
17.68/7.53 *
17.68/7.53 *
Unlike the {@link List} interface, this interface does not
17.68/7.53 * provide support for indexed access to elements.
17.68/7.53 *
17.68/7.53 *
While Deque implementations are not strictly required
17.68/7.53 * to prohibit the insertion of null elements, they are strongly
17.68/7.53 * encouraged to do so. Users of any Deque implementations
17.68/7.53 * that do allow null elements are strongly encouraged not to
17.68/7.53 * take advantage of the ability to insert nulls. This is so because
17.68/7.53 * null is used as a special return value by various methods
17.68/7.53 * to indicated that the deque is empty.
17.68/7.53 *
17.68/7.53 *
Deque implementations generally do not define
17.68/7.53 * element-based versions of the equals and hashCode
17.68/7.53 * methods, but instead inherit the identity-based versions from class
17.68/7.53 * Object.
17.68/7.53 *
17.68/7.53 *
This interface is a member of the Java Collections
17.68/7.53 * Framework.
17.68/7.53 *
17.68/7.53 * @author Doug Lea
17.68/7.53 * @author Josh Bloch
17.68/7.53 * @since 1.6
17.68/7.53 * @param the type of elements held in this collection
17.68/7.53 */
17.68/7.53
17.68/7.53 public interface Deque extends Queue {
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the front of this deque if it is
17.68/7.53 * possible to do so immediately without violating capacity restrictions.
17.68/7.53 * When using a capacity-restricted deque, it is generally preferable to
17.68/7.53 * use method {@link #offerFirst}.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 * @throws IllegalStateException if the element cannot be added at this
17.68/7.53 * time due to capacity restrictions
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this deque
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the specified
17.68/7.53 * element prevents it from being added to this deque
17.68/7.53 */
17.68/7.53 void addFirst(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the end of this deque if it is
17.68/7.53 * possible to do so immediately without violating capacity restrictions.
17.68/7.53 * When using a capacity-restricted deque, it is generally preferable to
17.68/7.53 * use method {@link #offerLast}.
17.68/7.53 *
17.68/7.53 * This method is equivalent to {@link #add}.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 * @throws IllegalStateException if the element cannot be added at this
17.68/7.53 * time due to capacity restrictions
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this deque
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the specified
17.68/7.53 * element prevents it from being added to this deque
17.68/7.53 */
17.68/7.53 void addLast(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the front of this deque unless it would
17.68/7.53 * violate capacity restrictions. When using a capacity-restricted deque,
17.68/7.53 * this method is generally preferable to the {@link #addFirst} method,
17.68/7.53 * which can fail to insert an element only by throwing an exception.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 * @return true if the element was added to this deque, else
17.68/7.53 * false
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this deque
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the specified
17.68/7.53 * element prevents it from being added to this deque
17.68/7.53 */
17.68/7.53 boolean offerFirst(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the end of this deque unless it would
17.68/7.53 * violate capacity restrictions. When using a capacity-restricted deque,
17.68/7.53 * this method is generally preferable to the {@link #addLast} method,
17.68/7.53 * which can fail to insert an element only by throwing an exception.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 * @return true if the element was added to this deque, else
17.68/7.53 * false
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this deque
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the specified
17.68/7.53 * element prevents it from being added to this deque
17.68/7.53 */
17.68/7.53 boolean offerLast(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the first element of this deque. This method
17.68/7.53 * differs from {@link #pollFirst pollFirst} only in that it throws an
17.68/7.53 * exception if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the head of this deque
17.68/7.53 * @throws NoSuchElementException if this deque is empty
17.68/7.53 */
17.68/7.53 E removeFirst();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the last element of this deque. This method
17.68/7.53 * differs from {@link #pollLast pollLast} only in that it throws an
17.68/7.53 * exception if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the tail of this deque
17.68/7.53 * @throws NoSuchElementException if this deque is empty
17.68/7.53 */
17.68/7.53 E removeLast();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the first element of this deque,
17.68/7.53 * or returns null if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the head of this deque, or null if this deque is empty
17.68/7.53 */
17.68/7.53 E pollFirst();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the last element of this deque,
17.68/7.53 * or returns null if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the tail of this deque, or null if this deque is empty
17.68/7.53 */
17.68/7.53 E pollLast();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the first element of this deque.
17.68/7.53 *
17.68/7.53 * This method differs from {@link #peekFirst peekFirst} only in that it
17.68/7.53 * throws an exception if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the head of this deque
17.68/7.53 * @throws NoSuchElementException if this deque is empty
17.68/7.53 */
17.68/7.53 E getFirst();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the last element of this deque.
17.68/7.53 * This method differs from {@link #peekLast peekLast} only in that it
17.68/7.53 * throws an exception if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the tail of this deque
17.68/7.53 * @throws NoSuchElementException if this deque is empty
17.68/7.53 */
17.68/7.53 E getLast();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the first element of this deque,
17.68/7.53 * or returns null if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the head of this deque, or null if this deque is empty
17.68/7.53 */
17.68/7.53 E peekFirst();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the last element of this deque,
17.68/7.53 * or returns null if this deque is empty.
17.68/7.53 *
17.68/7.53 * @return the tail of this deque, or null if this deque is empty
17.68/7.53 */
17.68/7.53 E peekLast();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes the first occurrence of the specified element from this deque.
17.68/7.53 * If the deque does not contain the element, it is unchanged.
17.68/7.53 * More formally, removes the first element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e))
17.68/7.53 * (if such an element exists).
17.68/7.53 * Returns true if this deque contained the specified element
17.68/7.53 * (or equivalently, if this deque changed as a result of the call).
17.68/7.53 *
17.68/7.53 * @param o element to be removed from this deque, if present
17.68/7.53 * @return true if an element was removed as a result of this call
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * is incompatible with this deque (optional)
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements (optional)
17.68/7.53 */
17.68/7.53 boolean removeFirstOccurrence(Object o);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes the last occurrence of the specified element from this deque.
17.68/7.53 * If the deque does not contain the element, it is unchanged.
17.68/7.53 * More formally, removes the last element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e))
17.68/7.53 * (if such an element exists).
17.68/7.53 * Returns true if this deque contained the specified element
17.68/7.53 * (or equivalently, if this deque changed as a result of the call).
17.68/7.53 *
17.68/7.53 * @param o element to be removed from this deque, if present
17.68/7.53 * @return true if an element was removed as a result of this call
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * is incompatible with this deque (optional)
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements (optional)
17.68/7.53 */
17.68/7.53 boolean removeLastOccurrence(Object o);
17.68/7.53
17.68/7.53 // *** Queue methods ***
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element into the queue represented by this deque
17.68/7.53 * (in other words, at the tail of this deque) if it is possible to do so
17.68/7.53 * immediately without violating capacity restrictions, returning
17.68/7.53 * true upon success and throwing an
17.68/7.53 * IllegalStateException if no space is currently available.
17.68/7.53 * When using a capacity-restricted deque, it is generally preferable to
17.68/7.53 * use {@link #offer(Object) offer}.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #addLast}.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 * @return true (as specified by {@link Collection#add})
17.68/7.53 * @throws IllegalStateException if the element cannot be added at this
17.68/7.53 * time due to capacity restrictions
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this deque
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the specified
17.68/7.53 * element prevents it from being added to this deque
17.68/7.53 */
17.68/7.53 boolean add(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element into the queue represented by this deque
17.68/7.53 * (in other words, at the tail of this deque) if it is possible to do so
17.68/7.53 * immediately without violating capacity restrictions, returning
17.68/7.53 * true upon success and false if no space is currently
17.68/7.53 * available. When using a capacity-restricted deque, this method is
17.68/7.53 * generally preferable to the {@link #add} method, which can fail to
17.68/7.53 * insert an element only by throwing an exception.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #offerLast}.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 * @return true if the element was added to this deque, else
17.68/7.53 * false
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this deque
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the specified
17.68/7.53 * element prevents it from being added to this deque
17.68/7.53 */
17.68/7.53 boolean offer(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the head of the queue represented by this deque
17.68/7.53 * (in other words, the first element of this deque).
17.68/7.53 * This method differs from {@link #poll poll} only in that it throws an
17.68/7.53 * exception if this deque is empty.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #removeFirst()}.
17.68/7.53 *
17.68/7.53 * @return the head of the queue represented by this deque
17.68/7.53 * @throws NoSuchElementException if this deque is empty
17.68/7.53 */
17.68/7.53 E remove();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the head of the queue represented by this deque
17.68/7.53 * (in other words, the first element of this deque), or returns
17.68/7.53 * null if this deque is empty.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #pollFirst()}.
17.68/7.53 *
17.68/7.53 * @return the first element of this deque, or null if
17.68/7.53 * this deque is empty
17.68/7.53 */
17.68/7.53 E poll();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the head of the queue represented by
17.68/7.53 * this deque (in other words, the first element of this deque).
17.68/7.53 * This method differs from {@link #peek peek} only in that it throws an
17.68/7.53 * exception if this deque is empty.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #getFirst()}.
17.68/7.53 *
17.68/7.53 * @return the head of the queue represented by this deque
17.68/7.53 * @throws NoSuchElementException if this deque is empty
17.68/7.53 */
17.68/7.53 E element();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the head of the queue represented by
17.68/7.53 * this deque (in other words, the first element of this deque), or
17.68/7.53 * returns null if this deque is empty.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #peekFirst()}.
17.68/7.53 *
17.68/7.53 * @return the head of the queue represented by this deque, or
17.68/7.53 * null if this deque is empty
17.68/7.53 */
17.68/7.53 E peek();
17.68/7.53
17.68/7.53
17.68/7.53 // *** Stack methods ***
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Pushes an element onto the stack represented by this deque (in other
17.68/7.53 * words, at the head of this deque) if it is possible to do so
17.68/7.53 * immediately without violating capacity restrictions, returning
17.68/7.53 * true upon success and throwing an
17.68/7.53 * IllegalStateException if no space is currently available.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #addFirst}.
17.68/7.53 *
17.68/7.53 * @param e the element to push
17.68/7.53 * @throws IllegalStateException if the element cannot be added at this
17.68/7.53 * time due to capacity restrictions
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * prevents it from being added to this deque
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements
17.68/7.53 * @throws IllegalArgumentException if some property of the specified
17.68/7.53 * element prevents it from being added to this deque
17.68/7.53 */
17.68/7.53 void push(E e);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Pops an element from the stack represented by this deque. In other
17.68/7.53 * words, removes and returns the first element of this deque.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #removeFirst()}.
17.68/7.53 *
17.68/7.53 * @return the element at the front of this deque (which is the top
17.68/7.53 * of the stack represented by this deque)
17.68/7.53 * @throws NoSuchElementException if this deque is empty
17.68/7.53 */
17.68/7.53 E pop();
17.68/7.53
17.68/7.53
17.68/7.53 // *** Collection methods ***
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes the first occurrence of the specified element from this deque.
17.68/7.53 * If the deque does not contain the element, it is unchanged.
17.68/7.53 * More formally, removes the first element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e))
17.68/7.53 * (if such an element exists).
17.68/7.53 * Returns true if this deque contained the specified element
17.68/7.53 * (or equivalently, if this deque changed as a result of the call).
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #removeFirstOccurrence}.
17.68/7.53 *
17.68/7.53 * @param o element to be removed from this deque, if present
17.68/7.53 * @return true if an element was removed as a result of this call
17.68/7.53 * @throws ClassCastException if the class of the specified element
17.68/7.53 * is incompatible with this deque (optional)
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements (optional)
17.68/7.53 */
17.68/7.53 boolean remove(Object o);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns true if this deque contains the specified element.
17.68/7.53 * More formally, returns true if and only if this deque contains
17.68/7.53 * at least one element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e)).
17.68/7.53 *
17.68/7.53 * @param o element whose presence in this deque is to be tested
17.68/7.53 * @return true if this deque contains the specified element
17.68/7.53 * @throws ClassCastException if the type of the specified element
17.68/7.53 * is incompatible with this deque (optional)
17.68/7.53 * @throws NullPointerException if the specified element is null and this
17.68/7.53 * deque does not permit null elements (optional)
17.68/7.53 */
17.68/7.53 boolean contains(Object o);
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the number of elements in this deque.
17.68/7.53 *
17.68/7.53 * @return the number of elements in this deque
17.68/7.53 */
17.68/7.53 public int size();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns an iterator over the elements in this deque in proper sequence.
17.68/7.53 * The elements will be returned in order from first (head) to last (tail).
17.68/7.53 *
17.68/7.53 * @return an iterator over the elements in this deque in proper sequence
17.68/7.53 */
17.68/7.53 Iterator iterator();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns an iterator over the elements in this deque in reverse
17.68/7.53 * sequential order. The elements will be returned in order from
17.68/7.53 * last (tail) to first (head).
17.68/7.53 *
17.68/7.53 * @return an iterator over the elements in this deque in reverse
17.68/7.53 * sequence
17.68/7.53 */
17.68/7.53 Iterator descendingIterator();
17.68/7.53
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 /*
17.68/7.53 * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.53 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.53 *
17.68/7.53 * This code is free software; you can redistribute it and/or modify it
17.68/7.53 * under the terms of the GNU General Public License version 2 only, as
17.68/7.53 * published by the Free Software Foundation. Sun designates this
17.68/7.53 * particular file as subject to the "Classpath" exception as provided
17.68/7.53 * by Sun in the LICENSE file that accompanied this code.
17.68/7.53 *
17.68/7.53 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.53 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.53 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.53 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.53 * accompanied this code).
17.68/7.53 *
17.68/7.53 * You should have received a copy of the GNU General Public License version
17.68/7.53 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.53 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.53 *
17.68/7.53 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.53 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.53 * have any questions.
17.68/7.53 */
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Thrown to indicate that a method has been passed an illegal or
17.68/7.53 * inappropriate argument.
17.68/7.53 *
17.68/7.53 * @author unascribed
17.68/7.53 * @see java.lang.Thread#setPriority(int)
17.68/7.53 * @since JDK1.0
17.68/7.53 */
17.68/7.53 public
17.68/7.53 class IllegalArgumentException extends RuntimeException {
17.68/7.53 /**
17.68/7.53 * Constructs an IllegalArgumentException
with no
17.68/7.53 * detail message.
17.68/7.53 */
17.68/7.53 public IllegalArgumentException() {
17.68/7.53 super();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs an IllegalArgumentException
with the
17.68/7.53 * specified detail message.
17.68/7.53 *
17.68/7.53 * @param s the detail message.
17.68/7.53 */
17.68/7.53 public IllegalArgumentException(String s) {
17.68/7.53 super(s);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs a new exception with the specified detail message and
17.68/7.53 * cause.
17.68/7.53 *
17.68/7.53 * Note that the detail message associated with cause
is
17.68/7.53 * not automatically incorporated in this exception's detail
17.68/7.53 * message.
17.68/7.53 *
17.68/7.53 * @param message the detail message (which is saved for later retrieval
17.68/7.53 * by the {@link Throwable#getMessage()} method).
17.68/7.53 * @param cause the cause (which is saved for later retrieval by the
17.68/7.53 * {@link Throwable#getCause()} method). (A null value
17.68/7.53 * is permitted, and indicates that the cause is nonexistent or
17.68/7.53 * unknown.)
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public IllegalArgumentException(String message, Throwable cause) {
17.68/7.53 super(message, cause);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs a new exception with the specified cause and a detail
17.68/7.53 * message of (cause==null ? null : cause.toString()) (which
17.68/7.53 * typically contains the class and detail message of cause).
17.68/7.53 * This constructor is useful for exceptions that are little more than
17.68/7.53 * wrappers for other throwables (for example, {@link
17.68/7.53 * java.security.PrivilegedActionException}).
17.68/7.53 *
17.68/7.53 * @param cause the cause (which is saved for later retrieval by the
17.68/7.53 * {@link Throwable#getCause()} method). (A null value is
17.68/7.53 * permitted, and indicates that the cause is nonexistent or
17.68/7.53 * unknown.)
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public IllegalArgumentException(Throwable cause) {
17.68/7.53 super(cause);
17.68/7.53 }
17.68/7.53
17.68/7.53 private static final long serialVersionUID = -5365630128856068164L;
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 /*
17.68/7.53 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.53 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.53 *
17.68/7.53 * This code is free software; you can redistribute it and/or modify it
17.68/7.53 * under the terms of the GNU General Public License version 2 only, as
17.68/7.53 * published by the Free Software Foundation. Sun designates this
17.68/7.53 * particular file as subject to the "Classpath" exception as provided
17.68/7.53 * by Sun in the LICENSE file that accompanied this code.
17.68/7.53 *
17.68/7.53 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.53 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.53 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.53 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.53 * accompanied this code).
17.68/7.53 *
17.68/7.53 * You should have received a copy of the GNU General Public License version
17.68/7.53 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.53 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.53 *
17.68/7.53 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.53 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.53 * have any questions.
17.68/7.53 */
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Signals that a method has been invoked at an illegal or
17.68/7.53 * inappropriate time. In other words, the Java environment or
17.68/7.53 * Java application is not in an appropriate state for the requested
17.68/7.53 * operation.
17.68/7.53 *
17.68/7.53 * @author Jonni Kanerva
17.68/7.53 * @since JDK1.1
17.68/7.53 */
17.68/7.53 public
17.68/7.53 class IllegalStateException extends RuntimeException {
17.68/7.53 /**
17.68/7.53 * Constructs an IllegalStateException with no detail message.
17.68/7.53 * A detail message is a String that describes this particular exception.
17.68/7.53 */
17.68/7.53 public IllegalStateException() {
17.68/7.53 super();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs an IllegalStateException with the specified detail
17.68/7.53 * message. A detail message is a String that describes this particular
17.68/7.53 * exception.
17.68/7.53 *
17.68/7.53 * @param s the String that contains a detailed message
17.68/7.53 */
17.68/7.53 public IllegalStateException(String s) {
17.68/7.53 super(s);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs a new exception with the specified detail message and
17.68/7.53 * cause.
17.68/7.53 *
17.68/7.53 *
Note that the detail message associated with cause
is
17.68/7.53 * not automatically incorporated in this exception's detail
17.68/7.53 * message.
17.68/7.53 *
17.68/7.53 * @param message the detail message (which is saved for later retrieval
17.68/7.53 * by the {@link Throwable#getMessage()} method).
17.68/7.53 * @param cause the cause (which is saved for later retrieval by the
17.68/7.53 * {@link Throwable#getCause()} method). (A null value
17.68/7.53 * is permitted, and indicates that the cause is nonexistent or
17.68/7.53 * unknown.)
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public IllegalStateException(String message, Throwable cause) {
17.68/7.53 super(message, cause);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs a new exception with the specified cause and a detail
17.68/7.53 * message of (cause==null ? null : cause.toString()) (which
17.68/7.53 * typically contains the class and detail message of cause).
17.68/7.53 * This constructor is useful for exceptions that are little more than
17.68/7.53 * wrappers for other throwables (for example, {@link
17.68/7.53 * java.security.PrivilegedActionException}).
17.68/7.53 *
17.68/7.53 * @param cause the cause (which is saved for later retrieval by the
17.68/7.53 * {@link Throwable#getCause()} method). (A null value is
17.68/7.53 * permitted, and indicates that the cause is nonexistent or
17.68/7.53 * unknown.)
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public IllegalStateException(Throwable cause) {
17.68/7.53 super(cause);
17.68/7.53 }
17.68/7.53
17.68/7.53 static final long serialVersionUID = -1848914673093119416L;
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 /*
17.68/7.53 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.53 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.53 *
17.68/7.53 * This code is free software; you can redistribute it and/or modify it
17.68/7.53 * under the terms of the GNU General Public License version 2 only, as
17.68/7.53 * published by the Free Software Foundation. Sun designates this
17.68/7.53 * particular file as subject to the "Classpath" exception as provided
17.68/7.53 * by Sun in the LICENSE file that accompanied this code.
17.68/7.53 *
17.68/7.53 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.53 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.53 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.53 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.53 * accompanied this code).
17.68/7.53 *
17.68/7.53 * You should have received a copy of the GNU General Public License version
17.68/7.53 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.53 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.53 *
17.68/7.53 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.53 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.53 * have any questions.
17.68/7.53 */
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * An iterator over a collection. {@code Iterator} takes the place of
17.68/7.53 * {@link Enumeration} in the Java Collections Framework. Iterators
17.68/7.53 * differ from enumerations in two ways:
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * - Iterators allow the caller to remove elements from the
17.68/7.53 * underlying collection during the iteration with well-defined
17.68/7.53 * semantics.
17.68/7.53 *
- Method names have been improved.
17.68/7.53 *
17.68/7.53 *
17.68/7.53 * This interface is a member of the
17.68/7.53 *
17.68/7.53 * Java Collections Framework.
17.68/7.53 *
17.68/7.53 * @author Josh Bloch
17.68/7.53 * @see Collection
17.68/7.53 * @see ListIterator
17.68/7.53 * @see Iterable
17.68/7.53 * @since 1.2
17.68/7.53 */
17.68/7.53 public interface Iterator {
17.68/7.53 /**
17.68/7.53 * Returns {@code true} if the iteration has more elements.
17.68/7.53 * (In other words, returns {@code true} if {@link #next} would
17.68/7.53 * return an element rather than throwing an exception.)
17.68/7.53 *
17.68/7.53 * @return {@code true} if the iteration has more elements
17.68/7.53 */
17.68/7.53 boolean hasNext();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the next element in the iteration.
17.68/7.53 *
17.68/7.53 * @return the next element in the iteration
17.68/7.53 * @throws NoSuchElementException if the iteration has no more elements
17.68/7.53 */
17.68/7.53 E next();
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes from the underlying collection the last element returned
17.68/7.53 * by this iterator (optional operation). This method can be called
17.68/7.53 * only once per call to {@link #next}. The behavior of an iterator
17.68/7.53 * is unspecified if the underlying collection is modified while the
17.68/7.53 * iteration is in progress in any way other than by calling this
17.68/7.53 * method.
17.68/7.53 *
17.68/7.53 * @throws UnsupportedOperationException if the {@code remove}
17.68/7.53 * operation is not supported by this iterator
17.68/7.53 *
17.68/7.53 * @throws IllegalStateException if the {@code next} method has not
17.68/7.53 * yet been called, or the {@code remove} method has already
17.68/7.53 * been called after the last call to the {@code next}
17.68/7.53 * method
17.68/7.53 */
17.68/7.53 void remove();
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 public class juLinkedListCreatePeekLast {
17.68/7.53 public static void main(String[] args) {
17.68/7.53 Random.args = args;
17.68/7.53
17.68/7.53 LinkedList l = createList(Random.random());
17.68/7.53 l.peekLast();
17.68/7.53 }
17.68/7.53
17.68/7.53 public static LinkedList createList(int n) {
17.68/7.53 LinkedList l = new LinkedList();
17.68/7.53 while (n > 0) {
17.68/7.53 l.addLast(new Content(Random.random()));
17.68/7.53 n--;
17.68/7.53 }
17.68/7.53 return l;
17.68/7.53 }
17.68/7.53 }
17.68/7.53
17.68/7.53 final class Content {
17.68/7.53 int val;
17.68/7.53
17.68/7.53 public Content(int v) {
17.68/7.53 this.val = v;
17.68/7.53 }
17.68/7.53
17.68/7.53 public int hashCode() {
17.68/7.53 return val^31;
17.68/7.53 }
17.68/7.53
17.68/7.53 public boolean equals(Object o) {
17.68/7.53 if (o instanceof Content) {
17.68/7.53 return this.val == ((Content) o).val;
17.68/7.53 }
17.68/7.53 return false;
17.68/7.53 }
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 /*
17.68/7.53 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.53 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.53 *
17.68/7.53 * This code is free software; you can redistribute it and/or modify it
17.68/7.53 * under the terms of the GNU General Public License version 2 only, as
17.68/7.53 * published by the Free Software Foundation. Sun designates this
17.68/7.53 * particular file as subject to the "Classpath" exception as provided
17.68/7.53 * by Sun in the LICENSE file that accompanied this code.
17.68/7.53 *
17.68/7.53 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.53 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.53 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.53 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.53 * accompanied this code).
17.68/7.53 *
17.68/7.53 * You should have received a copy of the GNU General Public License version
17.68/7.53 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.53 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.53 *
17.68/7.53 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.53 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.53 * have any questions.
17.68/7.53 */
17.68/7.53
17.68/7.53 package javaUtilEx;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Linked list implementation of the List interface. Implements all
17.68/7.53 * optional list operations, and permits all elements (including
17.68/7.53 * null). In addition to implementing the List interface,
17.68/7.53 * the LinkedList class provides uniformly named methods to
17.68/7.53 * get, remove and insert an element at the
17.68/7.53 * beginning and end of the list. These operations allow linked lists to be
17.68/7.53 * used as a stack, {@linkplain Queue queue}, or {@linkplain Deque
17.68/7.53 * double-ended queue}.
17.68/7.53 *
17.68/7.53 * The class implements the Deque interface, providing
17.68/7.53 * first-in-first-out queue operations for add,
17.68/7.53 * poll, along with other stack and deque operations.
17.68/7.53 *
17.68/7.53 * All of the operations perform as could be expected for a doubly-linked
17.68/7.53 * list. Operations that index into the list will traverse the list from
17.68/7.53 * the beginning or the end, whichever is closer to the specified index.
17.68/7.53 *
17.68/7.53 *
Note that this implementation is not synchronized.
17.68/7.53 * If multiple threads access a linked list concurrently, and at least
17.68/7.53 * one of the threads modifies the list structurally, it must be
17.68/7.53 * synchronized externally. (A structural modification is any operation
17.68/7.53 * that adds or deletes one or more elements; merely setting the value of
17.68/7.53 * an element is not a structural modification.) This is typically
17.68/7.53 * accomplished by synchronizing on some object that naturally
17.68/7.53 * encapsulates the list.
17.68/7.53 *
17.68/7.53 * If no such object exists, the list should be "wrapped" using the
17.68/7.53 * {@link Collections#synchronizedList Collections.synchronizedList}
17.68/7.53 * method. This is best done at creation time, to prevent accidental
17.68/7.53 * unsynchronized access to the list:
17.68/7.53 * List list = Collections.synchronizedList(new LinkedList(...));
17.68/7.53 *
17.68/7.53 * The iterators returned by this class's iterator and
17.68/7.53 * listIterator methods are fail-fast: if the list is
17.68/7.53 * structurally modified at any time after the iterator is created, in
17.68/7.53 * any way except through the Iterator's own remove or
17.68/7.53 * add methods, the iterator will throw a {@link
17.68/7.53 * ConcurrentModificationException}. Thus, in the face of concurrent
17.68/7.53 * modification, the iterator fails quickly and cleanly, rather than
17.68/7.53 * risking arbitrary, non-deterministic behavior at an undetermined
17.68/7.53 * time in the future.
17.68/7.53 *
17.68/7.53 *
Note that the fail-fast behavior of an iterator cannot be guaranteed
17.68/7.53 * as it is, generally speaking, impossible to make any hard guarantees in the
17.68/7.53 * presence of unsynchronized concurrent modification. Fail-fast iterators
17.68/7.53 * throw ConcurrentModificationException on a best-effort basis.
17.68/7.53 * Therefore, it would be wrong to write a program that depended on this
17.68/7.53 * exception for its correctness: the fail-fast behavior of iterators
17.68/7.53 * should be used only to detect bugs.
17.68/7.53 *
17.68/7.53 *
This class is a member of the
17.68/7.53 *
17.68/7.53 * Java Collections Framework.
17.68/7.53 *
17.68/7.53 * @author Josh Bloch
17.68/7.53 * @see List
17.68/7.53 * @see ArrayList
17.68/7.53 * @see Vector
17.68/7.53 * @since 1.2
17.68/7.53 * @param the type of elements held in this collection
17.68/7.53 */
17.68/7.53
17.68/7.53 public class LinkedList
17.68/7.53 extends AbstractSequentialList
17.68/7.53 implements List, Deque
17.68/7.53 {
17.68/7.53 private transient Entry header = new Entry(null, null, null);
17.68/7.53 private transient int size = 0;
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs an empty list.
17.68/7.53 */
17.68/7.53 public LinkedList() {
17.68/7.53 header.next = header.previous = header;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Constructs a list containing the elements of the specified
17.68/7.53 * collection, in the order they are returned by the collection's
17.68/7.53 * iterator.
17.68/7.53 *
17.68/7.53 * @param c the collection whose elements are to be placed into this list
17.68/7.53 * @throws NullPointerException if the specified collection is null
17.68/7.53 */
17.68/7.53 public LinkedList(Collection extends E> c) {
17.68/7.53 this();
17.68/7.53 addAll(c);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the first element in this list.
17.68/7.53 *
17.68/7.53 * @return the first element in this list
17.68/7.53 * @throws NoSuchElementException if this list is empty
17.68/7.53 */
17.68/7.53 public E getFirst() {
17.68/7.53 if (size==0)
17.68/7.53 throw new NoSuchElementException();
17.68/7.53
17.68/7.53 return header.next.element;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the last element in this list.
17.68/7.53 *
17.68/7.53 * @return the last element in this list
17.68/7.53 * @throws NoSuchElementException if this list is empty
17.68/7.53 */
17.68/7.53 public E getLast() {
17.68/7.53 if (size==0)
17.68/7.53 throw new NoSuchElementException();
17.68/7.53
17.68/7.53 return header.previous.element;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes and returns the first element from this list.
17.68/7.53 *
17.68/7.53 * @return the first element from this list
17.68/7.53 * @throws NoSuchElementException if this list is empty
17.68/7.53 */
17.68/7.53 public E removeFirst() {
17.68/7.53 return remove(header.next);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes and returns the last element from this list.
17.68/7.53 *
17.68/7.53 * @return the last element from this list
17.68/7.53 * @throws NoSuchElementException if this list is empty
17.68/7.53 */
17.68/7.53 public E removeLast() {
17.68/7.53 return remove(header.previous);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the beginning of this list.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 */
17.68/7.53 public void addFirst(E e) {
17.68/7.53 addBefore(e, header.next);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Appends the specified element to the end of this list.
17.68/7.53 *
17.68/7.53 * This method is equivalent to {@link #add}.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 */
17.68/7.53 public void addLast(E e) {
17.68/7.53 addBefore(e, header);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns true if this list contains the specified element.
17.68/7.53 * More formally, returns true if and only if this list contains
17.68/7.53 * at least one element e such that
17.68/7.53 * (o==null ? e==null : o.equals(e)).
17.68/7.53 *
17.68/7.53 * @param o element whose presence in this list is to be tested
17.68/7.53 * @return true if this list contains the specified element
17.68/7.53 */
17.68/7.53 public boolean contains(Object o) {
17.68/7.53 return indexOf(o) != -1;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the number of elements in this list.
17.68/7.53 *
17.68/7.53 * @return the number of elements in this list
17.68/7.53 */
17.68/7.53 public int size() {
17.68/7.53 return size;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Appends the specified element to the end of this list.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #addLast}.
17.68/7.53 *
17.68/7.53 * @param e element to be appended to this list
17.68/7.53 * @return true (as specified by {@link Collection#add})
17.68/7.53 */
17.68/7.53 public boolean add(E e) {
17.68/7.53 addBefore(e, header);
17.68/7.53 return true;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes the first occurrence of the specified element from this list,
17.68/7.53 * if it is present. If this list does not contain the element, it is
17.68/7.53 * unchanged. More formally, removes the element with the lowest index
17.68/7.53 * i such that
17.68/7.53 * (o==null ? get(i)==null : o.equals(get(i)))
17.68/7.53 * (if such an element exists). Returns true if this list
17.68/7.53 * contained the specified element (or equivalently, if this list
17.68/7.53 * changed as a result of the call).
17.68/7.53 *
17.68/7.53 * @param o element to be removed from this list, if present
17.68/7.53 * @return true if this list contained the specified element
17.68/7.53 */
17.68/7.53 public boolean remove(Object o) {
17.68/7.53 if (o==null) {
17.68/7.53 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.53 if (e.element==null) {
17.68/7.53 remove(e);
17.68/7.53 return true;
17.68/7.53 }
17.68/7.53 }
17.68/7.53 } else {
17.68/7.53 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.53 if (o.equals(e.element)) {
17.68/7.53 remove(e);
17.68/7.53 return true;
17.68/7.53 }
17.68/7.53 }
17.68/7.53 }
17.68/7.53 return false;
17.68/7.53 }
17.68/7.53 /**
17.68/7.53 * Removes all of the elements from this list.
17.68/7.53 */
17.68/7.53 public void clear() {
17.68/7.53 Entry e = header.next;
17.68/7.53 while (e != header) {
17.68/7.53 Entry next = e.next;
17.68/7.53 e.next = e.previous = null;
17.68/7.53 e.element = null;
17.68/7.53 e = next;
17.68/7.53 }
17.68/7.53 header.next = header.previous = header;
17.68/7.53 size = 0;
17.68/7.53 modCount++;
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 // Positional Access Operations
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the element at the specified position in this list.
17.68/7.53 *
17.68/7.53 * @param index index of the element to return
17.68/7.53 * @return the element at the specified position in this list
17.68/7.53 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.53 */
17.68/7.53 public E get(int index) {
17.68/7.53 return entry(index).element;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Replaces the element at the specified position in this list with the
17.68/7.53 * specified element.
17.68/7.53 *
17.68/7.53 * @param index index of the element to replace
17.68/7.53 * @param element element to be stored at the specified position
17.68/7.53 * @return the element previously at the specified position
17.68/7.53 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.53 */
17.68/7.53 public E set(int index, E element) {
17.68/7.53 Entry e = entry(index);
17.68/7.53 E oldVal = e.element;
17.68/7.53 e.element = element;
17.68/7.53 return oldVal;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the specified position in this list.
17.68/7.53 * Shifts the element currently at that position (if any) and any
17.68/7.53 * subsequent elements to the right (adds one to their indices).
17.68/7.53 *
17.68/7.53 * @param index index at which the specified element is to be inserted
17.68/7.53 * @param element element to be inserted
17.68/7.53 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.53 */
17.68/7.53 public void add(int index, E element) {
17.68/7.53 addBefore(element, (index==size ? header : entry(index)));
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes the element at the specified position in this list. Shifts any
17.68/7.53 * subsequent elements to the left (subtracts one from their indices).
17.68/7.53 * Returns the element that was removed from the list.
17.68/7.53 *
17.68/7.53 * @param index the index of the element to be removed
17.68/7.53 * @return the element previously at the specified position
17.68/7.53 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.53 */
17.68/7.53 public E remove(int index) {
17.68/7.53 return remove(entry(index));
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the indexed entry.
17.68/7.53 */
17.68/7.53 private Entry entry(int index) {
17.68/7.53 if (index < 0 || index >= size)
17.68/7.53 throw new IndexOutOfBoundsException();
17.68/7.53 Entry e = header;
17.68/7.53 if (index < (size >> 1)) {
17.68/7.53 for (int i = 0; i <= index; i++)
17.68/7.53 e = e.next;
17.68/7.53 } else {
17.68/7.53 for (int i = size; i > index; i--)
17.68/7.53 e = e.previous;
17.68/7.53 }
17.68/7.53 return e;
17.68/7.53 }
17.68/7.53
17.68/7.53
17.68/7.53 // Search Operations
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the index of the first occurrence of the specified element
17.68/7.53 * in this list, or -1 if this list does not contain the element.
17.68/7.53 * More formally, returns the lowest index i such that
17.68/7.53 * (o==null ? get(i)==null : o.equals(get(i))),
17.68/7.53 * or -1 if there is no such index.
17.68/7.53 *
17.68/7.53 * @param o element to search for
17.68/7.53 * @return the index of the first occurrence of the specified element in
17.68/7.53 * this list, or -1 if this list does not contain the element
17.68/7.53 */
17.68/7.53 public int indexOf(Object o) {
17.68/7.53 int index = 0;
17.68/7.53 if (o==null) {
17.68/7.53 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.53 if (e.element==null)
17.68/7.53 return index;
17.68/7.53 index++;
17.68/7.53 }
17.68/7.53 } else {
17.68/7.53 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.53 if (o.equals(e.element))
17.68/7.53 return index;
17.68/7.53 index++;
17.68/7.53 }
17.68/7.53 }
17.68/7.53 return -1;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns the index of the last occurrence of the specified element
17.68/7.53 * in this list, or -1 if this list does not contain the element.
17.68/7.53 * More formally, returns the highest index i such that
17.68/7.53 * (o==null ? get(i)==null : o.equals(get(i))),
17.68/7.53 * or -1 if there is no such index.
17.68/7.53 *
17.68/7.53 * @param o element to search for
17.68/7.53 * @return the index of the last occurrence of the specified element in
17.68/7.53 * this list, or -1 if this list does not contain the element
17.68/7.53 */
17.68/7.53 public int lastIndexOf(Object o) {
17.68/7.53 int index = size;
17.68/7.53 if (o==null) {
17.68/7.53 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.53 index--;
17.68/7.53 if (e.element==null)
17.68/7.53 return index;
17.68/7.53 }
17.68/7.53 } else {
17.68/7.53 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.53 index--;
17.68/7.53 if (o.equals(e.element))
17.68/7.53 return index;
17.68/7.53 }
17.68/7.53 }
17.68/7.53 return -1;
17.68/7.53 }
17.68/7.53
17.68/7.53 // Queue operations.
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the head (first element) of this list.
17.68/7.53 * @return the head of this list, or null if this list is empty
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public E peek() {
17.68/7.53 if (size==0)
17.68/7.53 return null;
17.68/7.53 return getFirst();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the head (first element) of this list.
17.68/7.53 * @return the head of this list
17.68/7.53 * @throws NoSuchElementException if this list is empty
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public E element() {
17.68/7.53 return getFirst();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the head (first element) of this list
17.68/7.53 * @return the head of this list, or null if this list is empty
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public E poll() {
17.68/7.53 if (size==0)
17.68/7.53 return null;
17.68/7.53 return removeFirst();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the head (first element) of this list.
17.68/7.53 *
17.68/7.53 * @return the head of this list
17.68/7.53 * @throws NoSuchElementException if this list is empty
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public E remove() {
17.68/7.53 return removeFirst();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Adds the specified element as the tail (last element) of this list.
17.68/7.53 *
17.68/7.53 * @param e the element to add
17.68/7.53 * @return true (as specified by {@link Queue#offer})
17.68/7.53 * @since 1.5
17.68/7.53 */
17.68/7.53 public boolean offer(E e) {
17.68/7.53 return add(e);
17.68/7.53 }
17.68/7.53
17.68/7.53 // Deque operations
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the front of this list.
17.68/7.53 *
17.68/7.53 * @param e the element to insert
17.68/7.53 * @return true (as specified by {@link Deque#offerFirst})
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public boolean offerFirst(E e) {
17.68/7.53 addFirst(e);
17.68/7.53 return true;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Inserts the specified element at the end of this list.
17.68/7.53 *
17.68/7.53 * @param e the element to insert
17.68/7.53 * @return true (as specified by {@link Deque#offerLast})
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public boolean offerLast(E e) {
17.68/7.53 addLast(e);
17.68/7.53 return true;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the first element of this list,
17.68/7.53 * or returns null if this list is empty.
17.68/7.53 *
17.68/7.53 * @return the first element of this list, or null
17.68/7.53 * if this list is empty
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public E peekFirst() {
17.68/7.53 if (size==0)
17.68/7.53 return null;
17.68/7.53 return getFirst();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves, but does not remove, the last element of this list,
17.68/7.53 * or returns null if this list is empty.
17.68/7.53 *
17.68/7.53 * @return the last element of this list, or null
17.68/7.53 * if this list is empty
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public E peekLast() {
17.68/7.53 if (size==0)
17.68/7.53 return null;
17.68/7.53 return getLast();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the first element of this list,
17.68/7.53 * or returns null if this list is empty.
17.68/7.53 *
17.68/7.53 * @return the first element of this list, or null if
17.68/7.53 * this list is empty
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public E pollFirst() {
17.68/7.53 if (size==0)
17.68/7.53 return null;
17.68/7.53 return removeFirst();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Retrieves and removes the last element of this list,
17.68/7.53 * or returns null if this list is empty.
17.68/7.53 *
17.68/7.53 * @return the last element of this list, or null if
17.68/7.53 * this list is empty
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public E pollLast() {
17.68/7.53 if (size==0)
17.68/7.53 return null;
17.68/7.53 return removeLast();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Pushes an element onto the stack represented by this list. In other
17.68/7.53 * words, inserts the element at the front of this list.
17.68/7.53 *
17.68/7.53 * This method is equivalent to {@link #addFirst}.
17.68/7.53 *
17.68/7.53 * @param e the element to push
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public void push(E e) {
17.68/7.53 addFirst(e);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Pops an element from the stack represented by this list. In other
17.68/7.53 * words, removes and returns the first element of this list.
17.68/7.53 *
17.68/7.53 *
This method is equivalent to {@link #removeFirst()}.
17.68/7.53 *
17.68/7.53 * @return the element at the front of this list (which is the top
17.68/7.53 * of the stack represented by this list)
17.68/7.53 * @throws NoSuchElementException if this list is empty
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public E pop() {
17.68/7.53 return removeFirst();
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes the first occurrence of the specified element in this
17.68/7.53 * list (when traversing the list from head to tail). If the list
17.68/7.53 * does not contain the element, it is unchanged.
17.68/7.53 *
17.68/7.53 * @param o element to be removed from this list, if present
17.68/7.53 * @return true if the list contained the specified element
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public boolean removeFirstOccurrence(Object o) {
17.68/7.53 return remove(o);
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Removes the last occurrence of the specified element in this
17.68/7.53 * list (when traversing the list from head to tail). If the list
17.68/7.53 * does not contain the element, it is unchanged.
17.68/7.53 *
17.68/7.53 * @param o element to be removed from this list, if present
17.68/7.53 * @return true if the list contained the specified element
17.68/7.53 * @since 1.6
17.68/7.53 */
17.68/7.53 public boolean removeLastOccurrence(Object o) {
17.68/7.53 if (o==null) {
17.68/7.53 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.53 if (e.element==null) {
17.68/7.53 remove(e);
17.68/7.53 return true;
17.68/7.53 }
17.68/7.53 }
17.68/7.53 } else {
17.68/7.53 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.53 if (o.equals(e.element)) {
17.68/7.53 remove(e);
17.68/7.53 return true;
17.68/7.53 }
17.68/7.53 }
17.68/7.53 }
17.68/7.53 return false;
17.68/7.53 }
17.68/7.53
17.68/7.53 /**
17.68/7.53 * Returns a list-iterator of the elements in this list (in proper
17.68/7.53 * sequence), starting at the specified position in the list.
17.68/7.53 * Obeys the general contract of List.listIterator(int).
17.68/7.53 *
17.68/7.53 * The list-iterator is fail-fast: if the list is structurally
17.68/7.53 * modified at any time after the Iterator is created, in any way except
17.68/7.53 * through the list-iterator's own remove or add
17.68/7.53 * methods, the list-iterator will throw a
17.68/7.53 * ConcurrentModificationException. Thus, in the face of
17.68/7.53 * concurrent modification, the iterator fails quickly and cleanly, rather
17.68/7.53 * than risking arbitrary, non-deterministic behavior at an undetermined
17.68/7.53 * time in the future.
17.68/7.54 *
17.68/7.54 * @param index index of the first element to be returned from the
17.68/7.54 * list-iterator (by a call to next)
17.68/7.54 * @return a ListIterator of the elements in this list (in proper
17.68/7.54 * sequence), starting at the specified position in the list
17.68/7.54 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.54 * @see List#listIterator(int)
17.68/7.54 */
17.68/7.54 public ListIterator listIterator(int index) {
17.68/7.54 return new ListItr(index);
17.68/7.54 }
17.68/7.54
17.68/7.54 private class ListItr implements ListIterator {
17.68/7.54 private Entry lastReturned = header;
17.68/7.54 private Entry next;
17.68/7.54 private int nextIndex;
17.68/7.54 private int expectedModCount = modCount;
17.68/7.54
17.68/7.54 ListItr(int index) {
17.68/7.54 if (index < 0 || index > size)
17.68/7.54 throw new IndexOutOfBoundsException();
17.68/7.54 if (index < (size >> 1)) {
17.68/7.54 next = header.next;
17.68/7.54 for (nextIndex=0; nextIndexindex; nextIndex--)
17.68/7.54 next = next.previous;
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54 public boolean hasNext() {
17.68/7.54 return nextIndex != size;
17.68/7.54 }
17.68/7.54
17.68/7.54 public E next() {
17.68/7.54 checkForComodification();
17.68/7.54 if (nextIndex == size)
17.68/7.54 throw new NoSuchElementException();
17.68/7.54
17.68/7.54 lastReturned = next;
17.68/7.54 next = next.next;
17.68/7.54 nextIndex++;
17.68/7.54 return lastReturned.element;
17.68/7.54 }
17.68/7.54
17.68/7.54 public boolean hasPrevious() {
17.68/7.54 return nextIndex != 0;
17.68/7.54 }
17.68/7.54
17.68/7.54 public E previous() {
17.68/7.54 if (nextIndex == 0)
17.68/7.54 throw new NoSuchElementException();
17.68/7.54
17.68/7.54 lastReturned = next = next.previous;
17.68/7.54 nextIndex--;
17.68/7.54 checkForComodification();
17.68/7.54 return lastReturned.element;
17.68/7.54 }
17.68/7.54
17.68/7.54 public int nextIndex() {
17.68/7.54 return nextIndex;
17.68/7.54 }
17.68/7.54
17.68/7.54 public int previousIndex() {
17.68/7.54 return nextIndex-1;
17.68/7.54 }
17.68/7.54
17.68/7.54 public void remove() {
17.68/7.54 checkForComodification();
17.68/7.54 Entry lastNext = lastReturned.next;
17.68/7.54 try {
17.68/7.54 LinkedList.this.remove(lastReturned);
17.68/7.54 } catch (NoSuchElementException e) {
17.68/7.54 throw new IllegalStateException();
17.68/7.54 }
17.68/7.54 if (next==lastReturned)
17.68/7.54 next = lastNext;
17.68/7.54 else
17.68/7.54 nextIndex--;
17.68/7.54 lastReturned = header;
17.68/7.54 expectedModCount++;
17.68/7.54 }
17.68/7.54
17.68/7.54 public void set(E e) {
17.68/7.54 if (lastReturned == header)
17.68/7.54 throw new IllegalStateException();
17.68/7.54 checkForComodification();
17.68/7.54 lastReturned.element = e;
17.68/7.54 }
17.68/7.54
17.68/7.54 public void add(E e) {
17.68/7.54 checkForComodification();
17.68/7.54 lastReturned = header;
17.68/7.54 addBefore(e, next);
17.68/7.54 nextIndex++;
17.68/7.54 expectedModCount++;
17.68/7.54 }
17.68/7.54
17.68/7.54 final void checkForComodification() {
17.68/7.54 if (modCount != expectedModCount)
17.68/7.54 throw new ConcurrentModificationException();
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54 private static class Entry {
17.68/7.54 E element;
17.68/7.54 Entry next;
17.68/7.54 Entry previous;
17.68/7.54
17.68/7.54 Entry(E element, Entry next, Entry previous) {
17.68/7.54 this.element = element;
17.68/7.54 this.next = next;
17.68/7.54 this.previous = previous;
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54 private Entry addBefore(E e, Entry entry) {
17.68/7.54 Entry newEntry = new Entry(e, entry, entry.previous);
17.68/7.54 newEntry.previous.next = newEntry;
17.68/7.54 newEntry.next.previous = newEntry;
17.68/7.54 size++;
17.68/7.54 modCount++;
17.68/7.54 return newEntry;
17.68/7.54 }
17.68/7.54
17.68/7.54 private E remove(Entry e) {
17.68/7.54 if (e == header)
17.68/7.54 throw new NoSuchElementException();
17.68/7.54
17.68/7.54 E result = e.element;
17.68/7.54 e.previous.next = e.next;
17.68/7.54 e.next.previous = e.previous;
17.68/7.54 e.next = e.previous = null;
17.68/7.54 e.element = null;
17.68/7.54 size--;
17.68/7.54 modCount++;
17.68/7.54 return result;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * @since 1.6
17.68/7.54 */
17.68/7.54 public Iterator descendingIterator() {
17.68/7.54 return new DescendingIterator();
17.68/7.54 }
17.68/7.54
17.68/7.54 /** Adapter to provide descending iterators via ListItr.previous */
17.68/7.54 private class DescendingIterator implements Iterator {
17.68/7.54 final ListItr itr = new ListItr(size());
17.68/7.54 public boolean hasNext() {
17.68/7.54 return itr.hasPrevious();
17.68/7.54 }
17.68/7.54 public E next() {
17.68/7.54 return itr.previous();
17.68/7.54 }
17.68/7.54 public void remove() {
17.68/7.54 itr.remove();
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns an array containing all of the elements in this list
17.68/7.54 * in proper sequence (from first to last element).
17.68/7.54 *
17.68/7.54 * The returned array will be "safe" in that no references to it are
17.68/7.54 * maintained by this list. (In other words, this method must allocate
17.68/7.54 * a new array). The caller is thus free to modify the returned array.
17.68/7.54 *
17.68/7.54 *
This method acts as bridge between array-based and collection-based
17.68/7.54 * APIs.
17.68/7.54 *
17.68/7.54 * @return an array containing all of the elements in this list
17.68/7.54 * in proper sequence
17.68/7.54 */
17.68/7.54 public Object[] toArray() {
17.68/7.54 Object[] result = new Object[size];
17.68/7.54 int i = 0;
17.68/7.54 for (Entry e = header.next; e != header; e = e.next)
17.68/7.54 result[i++] = e.element;
17.68/7.54 return result;
17.68/7.54 }
17.68/7.54
17.68/7.54 private static final long serialVersionUID = 876323262645176354L;
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 /*
17.68/7.54 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * An iterator for lists that allows the programmer
17.68/7.54 * to traverse the list in either direction, modify
17.68/7.54 * the list during iteration, and obtain the iterator's
17.68/7.54 * current position in the list. A {@code ListIterator}
17.68/7.54 * has no current element; its cursor position always
17.68/7.54 * lies between the element that would be returned by a call
17.68/7.54 * to {@code previous()} and the element that would be
17.68/7.54 * returned by a call to {@code next()}.
17.68/7.54 * An iterator for a list of length {@code n} has {@code n+1} possible
17.68/7.54 * cursor positions, as illustrated by the carets ({@code ^}) below:
17.68/7.54 *
17.68/7.54 * Element(0) Element(1) Element(2) ... Element(n-1)
17.68/7.54 * cursor positions: ^ ^ ^ ^ ^
17.68/7.54 *
17.68/7.54 * Note that the {@link #remove} and {@link #set(Object)} methods are
17.68/7.54 * not defined in terms of the cursor position; they are defined to
17.68/7.54 * operate on the last element returned by a call to {@link #next} or
17.68/7.54 * {@link #previous()}.
17.68/7.54 *
17.68/7.54 * This interface is a member of the
17.68/7.54 *
17.68/7.54 * Java Collections Framework.
17.68/7.54 *
17.68/7.54 * @author Josh Bloch
17.68/7.54 * @see Collection
17.68/7.54 * @see List
17.68/7.54 * @see Iterator
17.68/7.54 * @see Enumeration
17.68/7.54 * @see List#listIterator()
17.68/7.54 * @since 1.2
17.68/7.54 */
17.68/7.54 public interface ListIterator extends Iterator {
17.68/7.54 // Query Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns {@code true} if this list iterator has more elements when
17.68/7.54 * traversing the list in the forward direction. (In other words,
17.68/7.54 * returns {@code true} if {@link #next} would return an element rather
17.68/7.54 * than throwing an exception.)
17.68/7.54 *
17.68/7.54 * @return {@code true} if the list iterator has more elements when
17.68/7.54 * traversing the list in the forward direction
17.68/7.54 */
17.68/7.54 boolean hasNext();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the next element in the list and advances the cursor position.
17.68/7.54 * This method may be called repeatedly to iterate through the list,
17.68/7.54 * or intermixed with calls to {@link #previous} to go back and forth.
17.68/7.54 * (Note that alternating calls to {@code next} and {@code previous}
17.68/7.54 * will return the same element repeatedly.)
17.68/7.54 *
17.68/7.54 * @return the next element in the list
17.68/7.54 * @throws NoSuchElementException if the iteration has no next element
17.68/7.54 */
17.68/7.54 E next();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns {@code true} if this list iterator has more elements when
17.68/7.54 * traversing the list in the reverse direction. (In other words,
17.68/7.54 * returns {@code true} if {@link #previous} would return an element
17.68/7.54 * rather than throwing an exception.)
17.68/7.54 *
17.68/7.54 * @return {@code true} if the list iterator has more elements when
17.68/7.54 * traversing the list in the reverse direction
17.68/7.54 */
17.68/7.54 boolean hasPrevious();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the previous element in the list and moves the cursor
17.68/7.54 * position backwards. This method may be called repeatedly to
17.68/7.54 * iterate through the list backwards, or intermixed with calls to
17.68/7.54 * {@link #next} to go back and forth. (Note that alternating calls
17.68/7.54 * to {@code next} and {@code previous} will return the same
17.68/7.54 * element repeatedly.)
17.68/7.54 *
17.68/7.54 * @return the previous element in the list
17.68/7.54 * @throws NoSuchElementException if the iteration has no previous
17.68/7.54 * element
17.68/7.54 */
17.68/7.54 E previous();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the index of the element that would be returned by a
17.68/7.54 * subsequent call to {@link #next}. (Returns list size if the list
17.68/7.54 * iterator is at the end of the list.)
17.68/7.54 *
17.68/7.54 * @return the index of the element that would be returned by a
17.68/7.54 * subsequent call to {@code next}, or list size if the list
17.68/7.54 * iterator is at the end of the list
17.68/7.54 */
17.68/7.54 int nextIndex();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the index of the element that would be returned by a
17.68/7.54 * subsequent call to {@link #previous}. (Returns -1 if the list
17.68/7.54 * iterator is at the beginning of the list.)
17.68/7.54 *
17.68/7.54 * @return the index of the element that would be returned by a
17.68/7.54 * subsequent call to {@code previous}, or -1 if the list
17.68/7.54 * iterator is at the beginning of the list
17.68/7.54 */
17.68/7.54 int previousIndex();
17.68/7.54
17.68/7.54
17.68/7.54 // Modification Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Removes from the list the last element that was returned by {@link
17.68/7.54 * #next} or {@link #previous} (optional operation). This call can
17.68/7.54 * only be made once per call to {@code next} or {@code previous}.
17.68/7.54 * It can be made only if {@link #add} has not been
17.68/7.54 * called after the last call to {@code next} or {@code previous}.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException if the {@code remove}
17.68/7.54 * operation is not supported by this list iterator
17.68/7.54 * @throws IllegalStateException if neither {@code next} nor
17.68/7.54 * {@code previous} have been called, or {@code remove} or
17.68/7.54 * {@code add} have been called after the last call to
17.68/7.54 * {@code next} or {@code previous}
17.68/7.54 */
17.68/7.54 void remove();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Replaces the last element returned by {@link #next} or
17.68/7.54 * {@link #previous} with the specified element (optional operation).
17.68/7.54 * This call can be made only if neither {@link #remove} nor {@link
17.68/7.54 * #add} have been called after the last call to {@code next} or
17.68/7.54 * {@code previous}.
17.68/7.54 *
17.68/7.54 * @param e the element with which to replace the last element returned by
17.68/7.54 * {@code next} or {@code previous}
17.68/7.54 * @throws UnsupportedOperationException if the {@code set} operation
17.68/7.54 * is not supported by this list iterator
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 * @throws IllegalArgumentException if some aspect of the specified
17.68/7.54 * element prevents it from being added to this list
17.68/7.54 * @throws IllegalStateException if neither {@code next} nor
17.68/7.54 * {@code previous} have been called, or {@code remove} or
17.68/7.54 * {@code add} have been called after the last call to
17.68/7.54 * {@code next} or {@code previous}
17.68/7.54 */
17.68/7.54 void set(E e);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Inserts the specified element into the list (optional operation).
17.68/7.54 * The element is inserted immediately before the next element that
17.68/7.54 * would be returned by {@link #next}, if any, and after the next
17.68/7.54 * element that would be returned by {@link #previous}, if any. (If the
17.68/7.54 * list contains no elements, the new element becomes the sole element
17.68/7.54 * on the list.) The new element is inserted before the implicit
17.68/7.54 * cursor: a subsequent call to {@code next} would be unaffected, and a
17.68/7.54 * subsequent call to {@code previous} would return the new element.
17.68/7.54 * (This call increases by one the value that would be returned by a
17.68/7.54 * call to {@code nextIndex} or {@code previousIndex}.)
17.68/7.54 *
17.68/7.54 * @param e the element to insert
17.68/7.54 * @throws UnsupportedOperationException if the {@code add} method is
17.68/7.54 * not supported by this list iterator
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 * @throws IllegalArgumentException if some aspect of this element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 */
17.68/7.54 void add(E e);
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 /*
17.68/7.54 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * An ordered collection (also known as a sequence). The user of this
17.68/7.54 * interface has precise control over where in the list each element is
17.68/7.54 * inserted. The user can access elements by their integer index (position in
17.68/7.54 * the list), and search for elements in the list.
17.68/7.54 *
17.68/7.54 * Unlike sets, lists typically allow duplicate elements. More formally,
17.68/7.54 * lists typically allow pairs of elements e1 and e2
17.68/7.54 * such that e1.equals(e2), and they typically allow multiple
17.68/7.54 * null elements if they allow null elements at all. It is not inconceivable
17.68/7.54 * that someone might wish to implement a list that prohibits duplicates, by
17.68/7.54 * throwing runtime exceptions when the user attempts to insert them, but we
17.68/7.54 * expect this usage to be rare.
17.68/7.54 *
17.68/7.54 * The List interface places additional stipulations, beyond those
17.68/7.54 * specified in the Collection interface, on the contracts of the
17.68/7.54 * iterator, add, remove, equals, and
17.68/7.54 * hashCode methods. Declarations for other inherited methods are
17.68/7.54 * also included here for convenience.
17.68/7.54 *
17.68/7.54 * The List interface provides four methods for positional (indexed)
17.68/7.54 * access to list elements. Lists (like Java arrays) are zero based. Note
17.68/7.54 * that these operations may execute in time proportional to the index value
17.68/7.54 * for some implementations (the LinkedList class, for
17.68/7.54 * example). Thus, iterating over the elements in a list is typically
17.68/7.54 * preferable to indexing through it if the caller does not know the
17.68/7.54 * implementation.
17.68/7.54 *
17.68/7.54 * The List interface provides a special iterator, called a
17.68/7.54 * ListIterator, that allows element insertion and replacement, and
17.68/7.54 * bidirectional access in addition to the normal operations that the
17.68/7.54 * Iterator interface provides. A method is provided to obtain a
17.68/7.54 * list iterator that starts at a specified position in the list.
17.68/7.54 *
17.68/7.54 * The List interface provides two methods to search for a specified
17.68/7.54 * object. From a performance standpoint, these methods should be used with
17.68/7.54 * caution. In many implementations they will perform costly linear
17.68/7.54 * searches.
17.68/7.54 *
17.68/7.54 * The List interface provides two methods to efficiently insert and
17.68/7.54 * remove multiple elements at an arbitrary point in the list.
17.68/7.54 *
17.68/7.54 * Note: While it is permissible for lists to contain themselves as elements,
17.68/7.54 * extreme caution is advised: the equals and hashCode
17.68/7.54 * methods are no longer well defined on such a list.
17.68/7.54 *
17.68/7.54 *
Some list implementations have restrictions on the elements that
17.68/7.54 * they may contain. For example, some implementations prohibit null elements,
17.68/7.54 * and some have restrictions on the types of their elements. Attempting to
17.68/7.54 * add an ineligible element throws an unchecked exception, typically
17.68/7.54 * NullPointerException or ClassCastException. Attempting
17.68/7.54 * to query the presence of an ineligible element may throw an exception,
17.68/7.54 * or it may simply return false; some implementations will exhibit the former
17.68/7.54 * behavior and some will exhibit the latter. More generally, attempting an
17.68/7.54 * operation on an ineligible element whose completion would not result in
17.68/7.54 * the insertion of an ineligible element into the list may throw an
17.68/7.54 * exception or it may succeed, at the option of the implementation.
17.68/7.54 * Such exceptions are marked as "optional" in the specification for this
17.68/7.54 * interface.
17.68/7.54 *
17.68/7.54 *
This interface is a member of the
17.68/7.54 *
17.68/7.54 * Java Collections Framework.
17.68/7.54 *
17.68/7.54 * @author Josh Bloch
17.68/7.54 * @author Neal Gafter
17.68/7.54 * @see Collection
17.68/7.54 * @see Set
17.68/7.54 * @see ArrayList
17.68/7.54 * @see LinkedList
17.68/7.54 * @see Vector
17.68/7.54 * @see Arrays#asList(Object[])
17.68/7.54 * @see Collections#nCopies(int, Object)
17.68/7.54 * @see Collections#EMPTY_LIST
17.68/7.54 * @see AbstractList
17.68/7.54 * @see AbstractSequentialList
17.68/7.54 * @since 1.2
17.68/7.54 */
17.68/7.54
17.68/7.54 public interface List extends Collection {
17.68/7.54 // Query Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the number of elements in this list. If this list contains
17.68/7.54 * more than Integer.MAX_VALUE elements, returns
17.68/7.54 * Integer.MAX_VALUE.
17.68/7.54 *
17.68/7.54 * @return the number of elements in this list
17.68/7.54 */
17.68/7.54 int size();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns true if this list contains no elements.
17.68/7.54 *
17.68/7.54 * @return true if this list contains no elements
17.68/7.54 */
17.68/7.54 boolean isEmpty();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns true if this list contains the specified element.
17.68/7.54 * More formally, returns true if and only if this list contains
17.68/7.54 * at least one element e such that
17.68/7.54 * (o==null ? e==null : o.equals(e)).
17.68/7.54 *
17.68/7.54 * @param o element whose presence in this list is to be tested
17.68/7.54 * @return true if this list contains the specified element
17.68/7.54 * @throws ClassCastException if the type of the specified element
17.68/7.54 * is incompatible with this list (optional)
17.68/7.54 * @throws NullPointerException if the specified element is null and this
17.68/7.54 * list does not permit null elements (optional)
17.68/7.54 */
17.68/7.54 boolean contains(Object o);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns an iterator over the elements in this list in proper sequence.
17.68/7.54 *
17.68/7.54 * @return an iterator over the elements in this list in proper sequence
17.68/7.54 */
17.68/7.54 Iterator iterator();
17.68/7.54
17.68/7.54 // Modification Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Appends the specified element to the end of this list (optional
17.68/7.54 * operation).
17.68/7.54 *
17.68/7.54 * Lists that support this operation may place limitations on what
17.68/7.54 * elements may be added to this list. In particular, some
17.68/7.54 * lists will refuse to add null elements, and others will impose
17.68/7.54 * restrictions on the type of elements that may be added. List
17.68/7.54 * classes should clearly specify in their documentation any restrictions
17.68/7.54 * on what elements may be added.
17.68/7.54 *
17.68/7.54 * @param e element to be appended to this list
17.68/7.54 * @return true (as specified by {@link Collection#add})
17.68/7.54 * @throws UnsupportedOperationException if the add operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 * @throws NullPointerException if the specified element is null and this
17.68/7.54 * list does not permit null elements
17.68/7.54 * @throws IllegalArgumentException if some property of this element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 */
17.68/7.54 boolean add(E e);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Removes the first occurrence of the specified element from this list,
17.68/7.54 * if it is present (optional operation). If this list does not contain
17.68/7.54 * the element, it is unchanged. More formally, removes the element with
17.68/7.54 * the lowest index i such that
17.68/7.54 * (o==null ? get(i)==null : o.equals(get(i)))
17.68/7.54 * (if such an element exists). Returns true if this list
17.68/7.54 * contained the specified element (or equivalently, if this list changed
17.68/7.54 * as a result of the call).
17.68/7.54 *
17.68/7.54 * @param o element to be removed from this list, if present
17.68/7.54 * @return true if this list contained the specified element
17.68/7.54 * @throws ClassCastException if the type of the specified element
17.68/7.54 * is incompatible with this list (optional)
17.68/7.54 * @throws NullPointerException if the specified element is null and this
17.68/7.54 * list does not permit null elements (optional)
17.68/7.54 * @throws UnsupportedOperationException if the remove operation
17.68/7.54 * is not supported by this list
17.68/7.54 */
17.68/7.54 boolean remove(Object o);
17.68/7.54
17.68/7.54
17.68/7.54 // Bulk Modification Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns true if this list contains all of the elements of the
17.68/7.54 * specified collection.
17.68/7.54 *
17.68/7.54 * @param c collection to be checked for containment in this list
17.68/7.54 * @return true if this list contains all of the elements of the
17.68/7.54 * specified collection
17.68/7.54 * @throws ClassCastException if the types of one or more elements
17.68/7.54 * in the specified collection are incompatible with this
17.68/7.54 * list (optional)
17.68/7.54 * @throws NullPointerException if the specified collection contains one
17.68/7.54 * or more null elements and this list does not permit null
17.68/7.54 * elements (optional), or if the specified collection is null
17.68/7.54 * @see #contains(Object)
17.68/7.54 */
17.68/7.54 boolean containsAll(Collection> c);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Appends all of the elements in the specified collection to the end of
17.68/7.54 * this list, in the order that they are returned by the specified
17.68/7.54 * collection's iterator (optional operation). The behavior of this
17.68/7.54 * operation is undefined if the specified collection is modified while
17.68/7.54 * the operation is in progress. (Note that this will occur if the
17.68/7.54 * specified collection is this list, and it's nonempty.)
17.68/7.54 *
17.68/7.54 * @param c collection containing elements to be added to this list
17.68/7.54 * @return true if this list changed as a result of the call
17.68/7.54 * @throws UnsupportedOperationException if the addAll operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of an element of the specified
17.68/7.54 * collection prevents it from being added to this list
17.68/7.54 * @throws NullPointerException if the specified collection contains one
17.68/7.54 * or more null elements and this list does not permit null
17.68/7.54 * elements, or if the specified collection is null
17.68/7.54 * @throws IllegalArgumentException if some property of an element of the
17.68/7.54 * specified collection prevents it from being added to this list
17.68/7.54 * @see #add(Object)
17.68/7.54 */
17.68/7.54 boolean addAll(Collection extends E> c);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Inserts all of the elements in the specified collection into this
17.68/7.54 * list at the specified position (optional operation). Shifts the
17.68/7.54 * element currently at that position (if any) and any subsequent
17.68/7.54 * elements to the right (increases their indices). The new elements
17.68/7.54 * will appear in this list in the order that they are returned by the
17.68/7.54 * specified collection's iterator. The behavior of this operation is
17.68/7.54 * undefined if the specified collection is modified while the
17.68/7.54 * operation is in progress. (Note that this will occur if the specified
17.68/7.54 * collection is this list, and it's nonempty.)
17.68/7.54 *
17.68/7.54 * @param index index at which to insert the first element from the
17.68/7.54 * specified collection
17.68/7.54 * @param c collection containing elements to be added to this list
17.68/7.54 * @return true if this list changed as a result of the call
17.68/7.54 * @throws UnsupportedOperationException if the addAll operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of an element of the specified
17.68/7.54 * collection prevents it from being added to this list
17.68/7.54 * @throws NullPointerException if the specified collection contains one
17.68/7.54 * or more null elements and this list does not permit null
17.68/7.54 * elements, or if the specified collection is null
17.68/7.54 * @throws IllegalArgumentException if some property of an element of the
17.68/7.54 * specified collection prevents it from being added to this list
17.68/7.54 * @throws IndexOutOfBoundsException if the index is out of range
17.68/7.54 * (index < 0 || index > size())
17.68/7.54 */
17.68/7.54 boolean addAll(int index, Collection extends E> c);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Removes from this list all of its elements that are contained in the
17.68/7.54 * specified collection (optional operation).
17.68/7.54 *
17.68/7.54 * @param c collection containing elements to be removed from this list
17.68/7.54 * @return true if this list changed as a result of the call
17.68/7.54 * @throws UnsupportedOperationException if the removeAll operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of an element of this list
17.68/7.54 * is incompatible with the specified collection (optional)
17.68/7.54 * @throws NullPointerException if this list contains a null element and the
17.68/7.54 * specified collection does not permit null elements (optional),
17.68/7.54 * or if the specified collection is null
17.68/7.54 * @see #remove(Object)
17.68/7.54 * @see #contains(Object)
17.68/7.54 */
17.68/7.54 boolean removeAll(Collection> c);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Retains only the elements in this list that are contained in the
17.68/7.54 * specified collection (optional operation). In other words, removes
17.68/7.54 * from this list all of its elements that are not contained in the
17.68/7.54 * specified collection.
17.68/7.54 *
17.68/7.54 * @param c collection containing elements to be retained in this list
17.68/7.54 * @return true if this list changed as a result of the call
17.68/7.54 * @throws UnsupportedOperationException if the retainAll operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of an element of this list
17.68/7.54 * is incompatible with the specified collection (optional)
17.68/7.54 * @throws NullPointerException if this list contains a null element and the
17.68/7.54 * specified collection does not permit null elements (optional),
17.68/7.54 * or if the specified collection is null
17.68/7.54 * @see #remove(Object)
17.68/7.54 * @see #contains(Object)
17.68/7.54 */
17.68/7.54 boolean retainAll(Collection> c);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Removes all of the elements from this list (optional operation).
17.68/7.54 * The list will be empty after this call returns.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException if the clear operation
17.68/7.54 * is not supported by this list
17.68/7.54 */
17.68/7.54 void clear();
17.68/7.54
17.68/7.54
17.68/7.54 // Comparison and hashing
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Compares the specified object with this list for equality. Returns
17.68/7.54 * true if and only if the specified object is also a list, both
17.68/7.54 * lists have the same size, and all corresponding pairs of elements in
17.68/7.54 * the two lists are equal. (Two elements e1 and
17.68/7.54 * e2 are equal if (e1==null ? e2==null :
17.68/7.54 * e1.equals(e2)).) In other words, two lists are defined to be
17.68/7.54 * equal if they contain the same elements in the same order. This
17.68/7.54 * definition ensures that the equals method works properly across
17.68/7.54 * different implementations of the List interface.
17.68/7.54 *
17.68/7.54 * @param o the object to be compared for equality with this list
17.68/7.54 * @return true if the specified object is equal to this list
17.68/7.54 */
17.68/7.54 boolean equals(Object o);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the hash code value for this list. The hash code of a list
17.68/7.54 * is defined to be the result of the following calculation:
17.68/7.54 *
17.68/7.54 * int hashCode = 1;
17.68/7.54 * for (E e : list)
17.68/7.54 * hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
17.68/7.54 *
17.68/7.54 * This ensures that list1.equals(list2) implies that
17.68/7.54 * list1.hashCode()==list2.hashCode() for any two lists,
17.68/7.54 * list1 and list2, as required by the general
17.68/7.54 * contract of {@link Object#hashCode}.
17.68/7.54 *
17.68/7.54 * @return the hash code value for this list
17.68/7.54 * @see Object#equals(Object)
17.68/7.54 * @see #equals(Object)
17.68/7.54 */
17.68/7.54 int hashCode();
17.68/7.54
17.68/7.54
17.68/7.54 // Positional Access Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the element at the specified position in this list.
17.68/7.54 *
17.68/7.54 * @param index index of the element to return
17.68/7.54 * @return the element at the specified position in this list
17.68/7.54 * @throws IndexOutOfBoundsException if the index is out of range
17.68/7.54 * (index < 0 || index >= size())
17.68/7.54 */
17.68/7.54 E get(int index);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Replaces the element at the specified position in this list with the
17.68/7.54 * specified element (optional operation).
17.68/7.54 *
17.68/7.54 * @param index index of the element to replace
17.68/7.54 * @param element element to be stored at the specified position
17.68/7.54 * @return the element previously at the specified position
17.68/7.54 * @throws UnsupportedOperationException if the set operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 * @throws NullPointerException if the specified element is null and
17.68/7.54 * this list does not permit null elements
17.68/7.54 * @throws IllegalArgumentException if some property of the specified
17.68/7.54 * element prevents it from being added to this list
17.68/7.54 * @throws IndexOutOfBoundsException if the index is out of range
17.68/7.54 * (index < 0 || index >= size())
17.68/7.54 */
17.68/7.54 E set(int index, E element);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Inserts the specified element at the specified position in this list
17.68/7.54 * (optional operation). Shifts the element currently at that position
17.68/7.54 * (if any) and any subsequent elements to the right (adds one to their
17.68/7.54 * indices).
17.68/7.54 *
17.68/7.54 * @param index index at which the specified element is to be inserted
17.68/7.54 * @param element element to be inserted
17.68/7.54 * @throws UnsupportedOperationException if the add operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 * @throws NullPointerException if the specified element is null and
17.68/7.54 * this list does not permit null elements
17.68/7.54 * @throws IllegalArgumentException if some property of the specified
17.68/7.54 * element prevents it from being added to this list
17.68/7.54 * @throws IndexOutOfBoundsException if the index is out of range
17.68/7.54 * (index < 0 || index > size())
17.68/7.54 */
17.68/7.54 void add(int index, E element);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Removes the element at the specified position in this list (optional
17.68/7.54 * operation). Shifts any subsequent elements to the left (subtracts one
17.68/7.54 * from their indices). Returns the element that was removed from the
17.68/7.54 * list.
17.68/7.54 *
17.68/7.54 * @param index the index of the element to be removed
17.68/7.54 * @return the element previously at the specified position
17.68/7.54 * @throws UnsupportedOperationException if the remove operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws IndexOutOfBoundsException if the index is out of range
17.68/7.54 * (index < 0 || index >= size())
17.68/7.54 */
17.68/7.54 E remove(int index);
17.68/7.54
17.68/7.54
17.68/7.54 // Search Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the index of the first occurrence of the specified element
17.68/7.54 * in this list, or -1 if this list does not contain the element.
17.68/7.54 * More formally, returns the lowest index i such that
17.68/7.54 * (o==null ? get(i)==null : o.equals(get(i))),
17.68/7.54 * or -1 if there is no such index.
17.68/7.54 *
17.68/7.54 * @param o element to search for
17.68/7.54 * @return the index of the first occurrence of the specified element in
17.68/7.54 * this list, or -1 if this list does not contain the element
17.68/7.54 * @throws ClassCastException if the type of the specified element
17.68/7.54 * is incompatible with this list (optional)
17.68/7.54 * @throws NullPointerException if the specified element is null and this
17.68/7.54 * list does not permit null elements (optional)
17.68/7.54 */
17.68/7.54 int indexOf(Object o);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns the index of the last occurrence of the specified element
17.68/7.54 * in this list, or -1 if this list does not contain the element.
17.68/7.54 * More formally, returns the highest index i such that
17.68/7.54 * (o==null ? get(i)==null : o.equals(get(i))),
17.68/7.54 * or -1 if there is no such index.
17.68/7.54 *
17.68/7.54 * @param o element to search for
17.68/7.54 * @return the index of the last occurrence of the specified element in
17.68/7.54 * this list, or -1 if this list does not contain the element
17.68/7.54 * @throws ClassCastException if the type of the specified element
17.68/7.54 * is incompatible with this list (optional)
17.68/7.54 * @throws NullPointerException if the specified element is null and this
17.68/7.54 * list does not permit null elements (optional)
17.68/7.54 */
17.68/7.54 int lastIndexOf(Object o);
17.68/7.54
17.68/7.54
17.68/7.54 // List Iterators
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns a list iterator over the elements in this list (in proper
17.68/7.54 * sequence).
17.68/7.54 *
17.68/7.54 * @return a list iterator over the elements in this list (in proper
17.68/7.54 * sequence)
17.68/7.54 */
17.68/7.54 ListIterator listIterator();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns a list iterator over the elements in this list (in proper
17.68/7.54 * sequence), starting at the specified position in the list.
17.68/7.54 * The specified index indicates the first element that would be
17.68/7.54 * returned by an initial call to {@link ListIterator#next next}.
17.68/7.54 * An initial call to {@link ListIterator#previous previous} would
17.68/7.54 * return the element with the specified index minus one.
17.68/7.54 *
17.68/7.54 * @param index index of the first element to be returned from the
17.68/7.54 * list iterator (by a call to {@link ListIterator#next next})
17.68/7.54 * @return a list iterator over the elements in this list (in proper
17.68/7.54 * sequence), starting at the specified position in the list
17.68/7.54 * @throws IndexOutOfBoundsException if the index is out of range
17.68/7.54 * ({@code index < 0 || index > size()})
17.68/7.54 */
17.68/7.54 ListIterator listIterator(int index);
17.68/7.54
17.68/7.54 // View
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns a view of the portion of this list between the specified
17.68/7.54 * fromIndex, inclusive, and toIndex, exclusive. (If
17.68/7.54 * fromIndex and toIndex are equal, the returned list is
17.68/7.54 * empty.) The returned list is backed by this list, so non-structural
17.68/7.54 * changes in the returned list are reflected in this list, and vice-versa.
17.68/7.54 * The returned list supports all of the optional list operations supported
17.68/7.54 * by this list.
17.68/7.54 *
17.68/7.54 * This method eliminates the need for explicit range operations (of
17.68/7.54 * the sort that commonly exist for arrays). Any operation that expects
17.68/7.54 * a list can be used as a range operation by passing a subList view
17.68/7.54 * instead of a whole list. For example, the following idiom
17.68/7.54 * removes a range of elements from a list:
17.68/7.54 *
17.68/7.54 * list.subList(from, to).clear();
17.68/7.54 *
17.68/7.54 * Similar idioms may be constructed for indexOf and
17.68/7.54 * lastIndexOf, and all of the algorithms in the
17.68/7.54 * Collections class can be applied to a subList.
17.68/7.54 *
17.68/7.54 * The semantics of the list returned by this method become undefined if
17.68/7.54 * the backing list (i.e., this list) is structurally modified in
17.68/7.54 * any way other than via the returned list. (Structural modifications are
17.68/7.54 * those that change the size of this list, or otherwise perturb it in such
17.68/7.54 * a fashion that iterations in progress may yield incorrect results.)
17.68/7.54 *
17.68/7.54 * @param fromIndex low endpoint (inclusive) of the subList
17.68/7.54 * @param toIndex high endpoint (exclusive) of the subList
17.68/7.54 * @return a view of the specified range within this list
17.68/7.54 * @throws IndexOutOfBoundsException for an illegal endpoint index value
17.68/7.54 * (fromIndex < 0 || toIndex > size ||
17.68/7.54 * fromIndex > toIndex)
17.68/7.54 */
17.68/7.54 List subList(int fromIndex, int toIndex);
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 /*
17.68/7.54 * Copyright 1994-1998 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Thrown by the nextElement
method of an
17.68/7.54 * Enumeration
to indicate that there are no more
17.68/7.54 * elements in the enumeration.
17.68/7.54 *
17.68/7.54 * @author unascribed
17.68/7.54 * @see java.util.Enumeration
17.68/7.54 * @see java.util.Enumeration#nextElement()
17.68/7.54 * @since JDK1.0
17.68/7.54 */
17.68/7.54 public
17.68/7.54 class NoSuchElementException extends RuntimeException {
17.68/7.54 /**
17.68/7.54 * Constructs a NoSuchElementException
with null
17.68/7.54 * as its error message string.
17.68/7.54 */
17.68/7.54 public NoSuchElementException() {
17.68/7.54 super();
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Constructs a NoSuchElementException
, saving a reference
17.68/7.54 * to the error message string s for later retrieval by the
17.68/7.54 * getMessage method.
17.68/7.54 *
17.68/7.54 * @param s the detail message.
17.68/7.54 */
17.68/7.54 public NoSuchElementException(String s) {
17.68/7.54 super(s);
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 /*
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 /*
17.68/7.54 * This file is available under and governed by the GNU General Public
17.68/7.54 * License version 2 only, as published by the Free Software Foundation.
17.68/7.54 * However, the following notice accompanied the original version of this
17.68/7.54 * file:
17.68/7.54 *
17.68/7.54 * Written by Doug Lea with assistance from members of JCP JSR-166
17.68/7.54 * Expert Group and released to the public domain, as explained at
17.68/7.54 * http://creativecommons.org/licenses/publicdomain
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * A collection designed for holding elements prior to processing.
17.68/7.54 * Besides basic {@link java.util.Collection Collection} operations,
17.68/7.54 * queues provide additional insertion, extraction, and inspection
17.68/7.54 * operations. Each of these methods exists in two forms: one throws
17.68/7.54 * an exception if the operation fails, the other returns a special
17.68/7.54 * value (either null or false, depending on the
17.68/7.54 * operation). The latter form of the insert operation is designed
17.68/7.54 * specifically for use with capacity-restricted Queue
17.68/7.54 * implementations; in most implementations, insert operations cannot
17.68/7.54 * fail.
17.68/7.54 *
17.68/7.54 *
17.68/7.54 *
17.68/7.54 *
17.68/7.54 * |
17.68/7.54 * Throws exception |
17.68/7.54 * Returns special value |
17.68/7.54 *
17.68/7.54 *
17.68/7.54 * Insert |
17.68/7.54 * {@link #add add(e)} |
17.68/7.54 * {@link #offer offer(e)} |
17.68/7.54 *
17.68/7.54 *
17.68/7.54 * Remove |
17.68/7.54 * {@link #remove remove()} |
17.68/7.54 * {@link #poll poll()} |
17.68/7.54 *
17.68/7.54 *
17.68/7.54 * Examine |
17.68/7.54 * {@link #element element()} |
17.68/7.54 * {@link #peek peek()} |
17.68/7.54 *
17.68/7.54 *
17.68/7.54 *
17.68/7.54 * Queues typically, but do not necessarily, order elements in a
17.68/7.54 * FIFO (first-in-first-out) manner. Among the exceptions are
17.68/7.54 * priority queues, which order elements according to a supplied
17.68/7.54 * comparator, or the elements' natural ordering, and LIFO queues (or
17.68/7.54 * stacks) which order the elements LIFO (last-in-first-out).
17.68/7.54 * Whatever the ordering used, the head of the queue is that
17.68/7.54 * element which would be removed by a call to {@link #remove() } or
17.68/7.54 * {@link #poll()}. In a FIFO queue, all new elements are inserted at
17.68/7.54 * the tail of the queue. Other kinds of queues may use
17.68/7.54 * different placement rules. Every Queue implementation
17.68/7.54 * must specify its ordering properties.
17.68/7.54 *
17.68/7.54 *
The {@link #offer offer} method inserts an element if possible,
17.68/7.54 * otherwise returning false. This differs from the {@link
17.68/7.54 * java.util.Collection#add Collection.add} method, which can fail to
17.68/7.54 * add an element only by throwing an unchecked exception. The
17.68/7.54 * offer method is designed for use when failure is a normal,
17.68/7.54 * rather than exceptional occurrence, for example, in fixed-capacity
17.68/7.54 * (or "bounded") queues.
17.68/7.54 *
17.68/7.54 *
The {@link #remove()} and {@link #poll()} methods remove and
17.68/7.54 * return the head of the queue.
17.68/7.54 * Exactly which element is removed from the queue is a
17.68/7.54 * function of the queue's ordering policy, which differs from
17.68/7.54 * implementation to implementation. The remove() and
17.68/7.54 * poll() methods differ only in their behavior when the
17.68/7.54 * queue is empty: the remove() method throws an exception,
17.68/7.54 * while the poll() method returns null.
17.68/7.54 *
17.68/7.54 *
The {@link #element()} and {@link #peek()} methods return, but do
17.68/7.54 * not remove, the head of the queue.
17.68/7.54 *
17.68/7.54 *
The Queue interface does not define the blocking queue
17.68/7.54 * methods, which are common in concurrent programming. These methods,
17.68/7.54 * which wait for elements to appear or for space to become available, are
17.68/7.54 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
17.68/7.54 * extends this interface.
17.68/7.54 *
17.68/7.54 *
Queue implementations generally do not allow insertion
17.68/7.54 * of null elements, although some implementations, such as
17.68/7.54 * {@link LinkedList}, do not prohibit insertion of null.
17.68/7.54 * Even in the implementations that permit it, null should
17.68/7.54 * not be inserted into a Queue, as null is also
17.68/7.54 * used as a special return value by the poll method to
17.68/7.54 * indicate that the queue contains no elements.
17.68/7.54 *
17.68/7.54 *
Queue implementations generally do not define
17.68/7.54 * element-based versions of methods equals and
17.68/7.54 * hashCode but instead inherit the identity based versions
17.68/7.54 * from class Object, because element-based equality is not
17.68/7.54 * always well-defined for queues with the same elements but different
17.68/7.54 * ordering properties.
17.68/7.54 *
17.68/7.54 *
17.68/7.54 *
This interface is a member of the
17.68/7.54 *
17.68/7.54 * Java Collections Framework.
17.68/7.54 *
17.68/7.54 * @see java.util.Collection
17.68/7.54 * @see LinkedList
17.68/7.54 * @see PriorityQueue
17.68/7.54 * @see java.util.concurrent.LinkedBlockingQueue
17.68/7.54 * @see java.util.concurrent.BlockingQueue
17.68/7.54 * @see java.util.concurrent.ArrayBlockingQueue
17.68/7.54 * @see java.util.concurrent.LinkedBlockingQueue
17.68/7.54 * @see java.util.concurrent.PriorityBlockingQueue
17.68/7.54 * @since 1.5
17.68/7.54 * @author Doug Lea
17.68/7.54 * @param the type of elements held in this collection
17.68/7.54 */
17.68/7.54 public interface Queue extends Collection {
17.68/7.54 /**
17.68/7.54 * Inserts the specified element into this queue if it is possible to do so
17.68/7.54 * immediately without violating capacity restrictions, returning
17.68/7.54 * true upon success and throwing an IllegalStateException
17.68/7.54 * if no space is currently available.
17.68/7.54 *
17.68/7.54 * @param e the element to add
17.68/7.54 * @return true (as specified by {@link Collection#add})
17.68/7.54 * @throws IllegalStateException if the element cannot be added at this
17.68/7.54 * time due to capacity restrictions
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this queue
17.68/7.54 * @throws NullPointerException if the specified element is null and
17.68/7.54 * this queue does not permit null elements
17.68/7.54 * @throws IllegalArgumentException if some property of this element
17.68/7.54 * prevents it from being added to this queue
17.68/7.54 */
17.68/7.54 boolean add(E e);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Inserts the specified element into this queue if it is possible to do
17.68/7.54 * so immediately without violating capacity restrictions.
17.68/7.54 * When using a capacity-restricted queue, this method is generally
17.68/7.54 * preferable to {@link #add}, which can fail to insert an element only
17.68/7.54 * by throwing an exception.
17.68/7.54 *
17.68/7.54 * @param e the element to add
17.68/7.54 * @return true if the element was added to this queue, else
17.68/7.54 * false
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this queue
17.68/7.54 * @throws NullPointerException if the specified element is null and
17.68/7.54 * this queue does not permit null elements
17.68/7.54 * @throws IllegalArgumentException if some property of this element
17.68/7.54 * prevents it from being added to this queue
17.68/7.54 */
17.68/7.54 boolean offer(E e);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Retrieves and removes the head of this queue. This method differs
17.68/7.54 * from {@link #poll poll} only in that it throws an exception if this
17.68/7.54 * queue is empty.
17.68/7.54 *
17.68/7.54 * @return the head of this queue
17.68/7.54 * @throws NoSuchElementException if this queue is empty
17.68/7.54 */
17.68/7.54 E remove();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Retrieves and removes the head of this queue,
17.68/7.54 * or returns null if this queue is empty.
17.68/7.54 *
17.68/7.54 * @return the head of this queue, or null if this queue is empty
17.68/7.54 */
17.68/7.54 E poll();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Retrieves, but does not remove, the head of this queue. This method
17.68/7.54 * differs from {@link #peek peek} only in that it throws an exception
17.68/7.54 * if this queue is empty.
17.68/7.54 *
17.68/7.54 * @return the head of this queue
17.68/7.54 * @throws NoSuchElementException if this queue is empty
17.68/7.54 */
17.68/7.54 E element();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Retrieves, but does not remove, the head of this queue,
17.68/7.54 * or returns null if this queue is empty.
17.68/7.54 *
17.68/7.54 * @return the head of this queue, or null if this queue is empty
17.68/7.54 */
17.68/7.54 E peek();
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 /*
17.68/7.54 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Marker interface used by List implementations to indicate that
17.68/7.54 * they support fast (generally constant time) random access. The primary
17.68/7.54 * purpose of this interface is to allow generic algorithms to alter their
17.68/7.54 * behavior to provide good performance when applied to either random or
17.68/7.54 * sequential access lists.
17.68/7.54 *
17.68/7.54 * The best algorithms for manipulating random access lists (such as
17.68/7.54 * ArrayList) can produce quadratic behavior when applied to
17.68/7.54 * sequential access lists (such as LinkedList). Generic list
17.68/7.54 * algorithms are encouraged to check whether the given list is an
17.68/7.54 * instanceof this interface before applying an algorithm that would
17.68/7.54 * provide poor performance if it were applied to a sequential access list,
17.68/7.54 * and to alter their behavior if necessary to guarantee acceptable
17.68/7.54 * performance.
17.68/7.54 *
17.68/7.54 *
It is recognized that the distinction between random and sequential
17.68/7.54 * access is often fuzzy. For example, some List implementations
17.68/7.54 * provide asymptotically linear access times if they get huge, but constant
17.68/7.54 * access times in practice. Such a List implementation
17.68/7.54 * should generally implement this interface. As a rule of thumb, a
17.68/7.54 * List implementation should implement this interface if,
17.68/7.54 * for typical instances of the class, this loop:
17.68/7.54 *
17.68/7.54 * for (int i=0, n=list.size(); i < n; i++)
17.68/7.54 * list.get(i);
17.68/7.54 *
17.68/7.54 * runs faster than this loop:
17.68/7.54 *
17.68/7.54 * for (Iterator i=list.iterator(); i.hasNext(); )
17.68/7.54 * i.next();
17.68/7.54 *
17.68/7.54 *
17.68/7.54 * This interface is a member of the
17.68/7.54 *
17.68/7.54 * Java Collections Framework.
17.68/7.54 *
17.68/7.54 * @since 1.4
17.68/7.54 */
17.68/7.54 public interface RandomAccess {
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 public class Random {
17.68/7.54 static String[] args;
17.68/7.54 static int index = 0;
17.68/7.54
17.68/7.54 public static int random() {
17.68/7.54 String string = args[index];
17.68/7.54 index++;
17.68/7.54 return string.length();
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 /*
17.68/7.54 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Thrown to indicate that the requested operation is not supported.
17.68/7.54 *
17.68/7.54 * This class is a member of the
17.68/7.54 *
17.68/7.54 * Java Collections Framework.
17.68/7.54 *
17.68/7.54 * @author Josh Bloch
17.68/7.54 * @since 1.2
17.68/7.54 */
17.68/7.54 public class UnsupportedOperationException extends RuntimeException {
17.68/7.54 /**
17.68/7.54 * Constructs an UnsupportedOperationException with no detail message.
17.68/7.54 */
17.68/7.54 public UnsupportedOperationException() {
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Constructs an UnsupportedOperationException with the specified
17.68/7.54 * detail message.
17.68/7.54 *
17.68/7.54 * @param message the detail message
17.68/7.54 */
17.68/7.54 public UnsupportedOperationException(String message) {
17.68/7.54 super(message);
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Constructs a new exception with the specified detail message and
17.68/7.54 * cause.
17.68/7.54 *
17.68/7.54 *
Note that the detail message associated with cause
is
17.68/7.54 * not automatically incorporated in this exception's detail
17.68/7.54 * message.
17.68/7.54 *
17.68/7.54 * @param message the detail message (which is saved for later retrieval
17.68/7.54 * by the {@link Throwable#getMessage()} method).
17.68/7.54 * @param cause the cause (which is saved for later retrieval by the
17.68/7.54 * {@link Throwable#getCause()} method). (A null value
17.68/7.54 * is permitted, and indicates that the cause is nonexistent or
17.68/7.54 * unknown.)
17.68/7.54 * @since 1.5
17.68/7.54 */
17.68/7.54 public UnsupportedOperationException(String message, Throwable cause) {
17.68/7.54 super(message, cause);
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Constructs a new exception with the specified cause and a detail
17.68/7.54 * message of (cause==null ? null : cause.toString()) (which
17.68/7.54 * typically contains the class and detail message of cause).
17.68/7.54 * This constructor is useful for exceptions that are little more than
17.68/7.54 * wrappers for other throwables (for example, {@link
17.68/7.54 * java.security.PrivilegedActionException}).
17.68/7.54 *
17.68/7.54 * @param cause the cause (which is saved for later retrieval by the
17.68/7.54 * {@link Throwable#getCause()} method). (A null value is
17.68/7.54 * permitted, and indicates that the cause is nonexistent or
17.68/7.54 * unknown.)
17.68/7.54 * @since 1.5
17.68/7.54 */
17.68/7.54 public UnsupportedOperationException(Throwable cause) {
17.68/7.54 super(cause);
17.68/7.54 }
17.68/7.54
17.68/7.54 static final long serialVersionUID = -1242599979055084673L;
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54
17.68/7.54 ----------------------------------------
17.68/7.54
17.68/7.54 (1) BareJBCToJBCProof (EQUIVALENT)
17.68/7.54 initialized classpath
17.68/7.54 ----------------------------------------
17.68/7.54
17.68/7.54 (2)
17.68/7.54 Obligation:
17.68/7.54 need to prove termination of the following program:
17.68/7.54 /*
17.68/7.54 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * This class provides a skeletal implementation of the Collection
17.68/7.54 * interface, to minimize the effort required to implement this interface.
17.68/7.54 *
17.68/7.54 * To implement an unmodifiable collection, the programmer needs only to
17.68/7.54 * extend this class and provide implementations for the iterator and
17.68/7.54 * size methods. (The iterator returned by the iterator
17.68/7.54 * method must implement hasNext and next.)
17.68/7.54 *
17.68/7.54 * To implement a modifiable collection, the programmer must additionally
17.68/7.54 * override this class's add method (which otherwise throws an
17.68/7.54 * UnsupportedOperationException), and the iterator returned by the
17.68/7.54 * iterator method must additionally implement its remove
17.68/7.54 * method.
17.68/7.54 *
17.68/7.54 * The programmer should generally provide a void (no argument) and
17.68/7.54 * Collection constructor, as per the recommendation in the
17.68/7.54 * Collection interface specification.
17.68/7.54 *
17.68/7.54 * The documentation for each non-abstract method in this class describes its
17.68/7.54 * implementation in detail. Each of these methods may be overridden if
17.68/7.54 * the collection being implemented admits a more efficient implementation.
17.68/7.54 *
17.68/7.54 * This class is a member of the
17.68/7.54 *
17.68/7.54 * Java Collections Framework.
17.68/7.54 *
17.68/7.54 * @author Josh Bloch
17.68/7.54 * @author Neal Gafter
17.68/7.54 * @see Collection
17.68/7.54 * @since 1.2
17.68/7.54 */
17.68/7.54
17.68/7.54 public abstract class AbstractCollection implements Collection {
17.68/7.54 /**
17.68/7.54 * Sole constructor. (For invocation by subclass constructors, typically
17.68/7.54 * implicit.)
17.68/7.54 */
17.68/7.54 protected AbstractCollection() {
17.68/7.54 }
17.68/7.54
17.68/7.54 // Query Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns an iterator over the elements contained in this collection.
17.68/7.54 *
17.68/7.54 * @return an iterator over the elements contained in this collection
17.68/7.54 */
17.68/7.54 public abstract Iterator iterator();
17.68/7.54
17.68/7.54 public abstract int size();
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * This implementation returns size() == 0.
17.68/7.54 */
17.68/7.54 public boolean isEmpty() {
17.68/7.54 return size() == 0;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation iterates over the elements in the collection,
17.68/7.54 * checking each element in turn for equality with the specified element.
17.68/7.54 *
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public boolean contains(Object o) {
17.68/7.54 Iterator e = iterator();
17.68/7.54 if (o==null) {
17.68/7.54 while (e.hasNext())
17.68/7.54 if (e.next()==null)
17.68/7.54 return true;
17.68/7.54 } else {
17.68/7.54 while (e.hasNext())
17.68/7.54 if (o.equals(e.next()))
17.68/7.54 return true;
17.68/7.54 }
17.68/7.54 return false;
17.68/7.54 }
17.68/7.54
17.68/7.54 // Modification Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * This implementation always throws an
17.68/7.54 * UnsupportedOperationException.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.54 * @throws IllegalStateException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public boolean add(E e) {
17.68/7.54 throw new UnsupportedOperationException();
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation iterates over the collection looking for the
17.68/7.54 * specified element. If it finds the element, it removes the element
17.68/7.54 * from the collection using the iterator's remove method.
17.68/7.54 *
17.68/7.54 *
Note that this implementation throws an
17.68/7.54 * UnsupportedOperationException if the iterator returned by this
17.68/7.54 * collection's iterator method does not implement the remove
17.68/7.54 * method and this collection contains the specified object.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public boolean remove(Object o) {
17.68/7.54 Iterator e = iterator();
17.68/7.54 if (o==null) {
17.68/7.54 while (e.hasNext()) {
17.68/7.54 if (e.next()==null) {
17.68/7.54 e.remove();
17.68/7.54 return true;
17.68/7.54 }
17.68/7.54 }
17.68/7.54 } else {
17.68/7.54 while (e.hasNext()) {
17.68/7.54 if (o.equals(e.next())) {
17.68/7.54 e.remove();
17.68/7.54 return true;
17.68/7.54 }
17.68/7.54 }
17.68/7.54 }
17.68/7.54 return false;
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 // Bulk Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * This implementation iterates over the specified collection,
17.68/7.54 * checking each element returned by the iterator in turn to see
17.68/7.54 * if it's contained in this collection. If all elements are so
17.68/7.54 * contained true is returned, otherwise false.
17.68/7.54 *
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 * @see #contains(Object)
17.68/7.54 */
17.68/7.54 public boolean containsAll(Collection> c) {
17.68/7.54 Iterator> e = c.iterator();
17.68/7.54 while (e.hasNext())
17.68/7.54 if (!contains(e.next()))
17.68/7.54 return false;
17.68/7.54 return true;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation iterates over the specified collection, and adds
17.68/7.54 * each object returned by the iterator to this collection, in turn.
17.68/7.54 *
17.68/7.54 *
Note that this implementation will throw an
17.68/7.54 * UnsupportedOperationException unless add is
17.68/7.54 * overridden (assuming the specified collection is non-empty).
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.54 * @throws IllegalStateException {@inheritDoc}
17.68/7.54 *
17.68/7.54 * @see #add(Object)
17.68/7.54 */
17.68/7.54 public boolean addAll(Collection extends E> c) {
17.68/7.54 boolean modified = false;
17.68/7.54 Iterator extends E> e = c.iterator();
17.68/7.54 while (e.hasNext()) {
17.68/7.54 if (add(e.next()))
17.68/7.54 modified = true;
17.68/7.54 }
17.68/7.54 return modified;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation iterates over this collection, checking each
17.68/7.54 * element returned by the iterator in turn to see if it's contained
17.68/7.54 * in the specified collection. If it's so contained, it's removed from
17.68/7.54 * this collection with the iterator's remove method.
17.68/7.54 *
17.68/7.54 *
Note that this implementation will throw an
17.68/7.54 * UnsupportedOperationException if the iterator returned by the
17.68/7.54 * iterator method does not implement the remove method
17.68/7.54 * and this collection contains one or more elements in common with the
17.68/7.54 * specified collection.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 *
17.68/7.54 * @see #remove(Object)
17.68/7.54 * @see #contains(Object)
17.68/7.54 */
17.68/7.54 public boolean removeAll(Collection> c) {
17.68/7.54 boolean modified = false;
17.68/7.54 Iterator> e = iterator();
17.68/7.54 while (e.hasNext()) {
17.68/7.54 if (c.contains(e.next())) {
17.68/7.54 e.remove();
17.68/7.54 modified = true;
17.68/7.54 }
17.68/7.54 }
17.68/7.54 return modified;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation iterates over this collection, checking each
17.68/7.54 * element returned by the iterator in turn to see if it's contained
17.68/7.54 * in the specified collection. If it's not so contained, it's removed
17.68/7.54 * from this collection with the iterator's remove method.
17.68/7.54 *
17.68/7.54 *
Note that this implementation will throw an
17.68/7.54 * UnsupportedOperationException if the iterator returned by the
17.68/7.54 * iterator method does not implement the remove method
17.68/7.54 * and this collection contains one or more elements not present in the
17.68/7.54 * specified collection.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 *
17.68/7.54 * @see #remove(Object)
17.68/7.54 * @see #contains(Object)
17.68/7.54 */
17.68/7.54 public boolean retainAll(Collection> c) {
17.68/7.54 boolean modified = false;
17.68/7.54 Iterator e = iterator();
17.68/7.54 while (e.hasNext()) {
17.68/7.54 if (!c.contains(e.next())) {
17.68/7.54 e.remove();
17.68/7.54 modified = true;
17.68/7.54 }
17.68/7.54 }
17.68/7.54 return modified;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * This implementation iterates over this collection, removing each
17.68/7.54 * element using the Iterator.remove operation. Most
17.68/7.54 * implementations will probably choose to override this method for
17.68/7.54 * efficiency.
17.68/7.54 *
17.68/7.54 *
Note that this implementation will throw an
17.68/7.54 * UnsupportedOperationException if the iterator returned by this
17.68/7.54 * collection's iterator method does not implement the
17.68/7.54 * remove method and this collection is non-empty.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public void clear() {
17.68/7.54 Iterator e = iterator();
17.68/7.54 while (e.hasNext()) {
17.68/7.54 e.next();
17.68/7.54 e.remove();
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 // String conversion
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns a string representation of this collection. The string
17.68/7.54 * representation consists of a list of the collection's elements in the
17.68/7.54 * order they are returned by its iterator, enclosed in square brackets
17.68/7.54 * ("[]"). Adjacent elements are separated by the characters
17.68/7.54 * ", " (comma and space). Elements are converted to strings as
17.68/7.54 * by {@link String#valueOf(Object)}.
17.68/7.54 *
17.68/7.54 * @return a string representation of this collection
17.68/7.54 */
17.68/7.54 public String toString() {
17.68/7.54 Iterator i = iterator();
17.68/7.54 if (! i.hasNext())
17.68/7.54 return "[]";
17.68/7.54
17.68/7.54 String sb = "";
17.68/7.54 sb = sb + "[";
17.68/7.54 for (;;) {
17.68/7.54 E e = i.next();
17.68/7.54 sb = sb + (e == this ? "(this Collection)" : e);
17.68/7.54 if (! i.hasNext()) {
17.68/7.54 sb = sb + "]";
17.68/7.54 return sb;
17.68/7.54 }
17.68/7.54 sb = sb + ", ";
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 /*
17.68/7.54 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.54 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.54 *
17.68/7.54 * This code is free software; you can redistribute it and/or modify it
17.68/7.54 * under the terms of the GNU General Public License version 2 only, as
17.68/7.54 * published by the Free Software Foundation. Sun designates this
17.68/7.54 * particular file as subject to the "Classpath" exception as provided
17.68/7.54 * by Sun in the LICENSE file that accompanied this code.
17.68/7.54 *
17.68/7.54 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.54 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.54 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.54 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.54 * accompanied this code).
17.68/7.54 *
17.68/7.54 * You should have received a copy of the GNU General Public License version
17.68/7.54 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.54 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.54 *
17.68/7.54 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.54 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.54 * have any questions.
17.68/7.54 */
17.68/7.54
17.68/7.54 package javaUtilEx;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * This class provides a skeletal implementation of the {@link List}
17.68/7.54 * interface to minimize the effort required to implement this interface
17.68/7.54 * backed by a "random access" data store (such as an array). For sequential
17.68/7.54 * access data (such as a linked list), {@link AbstractSequentialList} should
17.68/7.54 * be used in preference to this class.
17.68/7.54 *
17.68/7.54 * To implement an unmodifiable list, the programmer needs only to extend
17.68/7.54 * this class and provide implementations for the {@link #get(int)} and
17.68/7.54 * {@link List#size() size()} methods.
17.68/7.54 *
17.68/7.54 *
To implement a modifiable list, the programmer must additionally
17.68/7.54 * override the {@link #set(int, Object) set(int, E)} method (which otherwise
17.68/7.54 * throws an {@code UnsupportedOperationException}). If the list is
17.68/7.54 * variable-size the programmer must additionally override the
17.68/7.54 * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
17.68/7.54 *
17.68/7.54 *
The programmer should generally provide a void (no argument) and collection
17.68/7.54 * constructor, as per the recommendation in the {@link Collection} interface
17.68/7.54 * specification.
17.68/7.54 *
17.68/7.54 *
Unlike the other abstract collection implementations, the programmer does
17.68/7.54 * not have to provide an iterator implementation; the iterator and
17.68/7.54 * list iterator are implemented by this class, on top of the "random access"
17.68/7.54 * methods:
17.68/7.54 * {@link #get(int)},
17.68/7.54 * {@link #set(int, Object) set(int, E)},
17.68/7.54 * {@link #add(int, Object) add(int, E)} and
17.68/7.54 * {@link #remove(int)}.
17.68/7.54 *
17.68/7.54 *
The documentation for each non-abstract method in this class describes its
17.68/7.54 * implementation in detail. Each of these methods may be overridden if the
17.68/7.54 * collection being implemented admits a more efficient implementation.
17.68/7.54 *
17.68/7.54 *
This class is a member of the
17.68/7.54 *
17.68/7.54 * Java Collections Framework.
17.68/7.54 *
17.68/7.54 * @author Josh Bloch
17.68/7.54 * @author Neal Gafter
17.68/7.54 * @since 1.2
17.68/7.54 */
17.68/7.54
17.68/7.54 public abstract class AbstractList extends AbstractCollection implements List {
17.68/7.54 /**
17.68/7.54 * Sole constructor. (For invocation by subclass constructors, typically
17.68/7.54 * implicit.)
17.68/7.54 */
17.68/7.54 protected AbstractList() {
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Appends the specified element to the end of this list (optional
17.68/7.54 * operation).
17.68/7.54 *
17.68/7.54 * Lists that support this operation may place limitations on what
17.68/7.54 * elements may be added to this list. In particular, some
17.68/7.54 * lists will refuse to add null elements, and others will impose
17.68/7.54 * restrictions on the type of elements that may be added. List
17.68/7.54 * classes should clearly specify in their documentation any restrictions
17.68/7.54 * on what elements may be added.
17.68/7.54 *
17.68/7.54 *
This implementation calls {@code add(size(), e)}.
17.68/7.54 *
17.68/7.54 *
Note that this implementation throws an
17.68/7.54 * {@code UnsupportedOperationException} unless
17.68/7.54 * {@link #add(int, Object) add(int, E)} is overridden.
17.68/7.54 *
17.68/7.54 * @param e element to be appended to this list
17.68/7.54 * @return {@code true} (as specified by {@link Collection#add})
17.68/7.54 * @throws UnsupportedOperationException if the {@code add} operation
17.68/7.54 * is not supported by this list
17.68/7.54 * @throws ClassCastException if the class of the specified element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 * @throws NullPointerException if the specified element is null and this
17.68/7.54 * list does not permit null elements
17.68/7.54 * @throws IllegalArgumentException if some property of this element
17.68/7.54 * prevents it from being added to this list
17.68/7.54 */
17.68/7.54 public boolean add(E e) {
17.68/7.54 add(size(), e);
17.68/7.54 return true;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.54 */
17.68/7.54 abstract public E get(int index);
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation always throws an
17.68/7.54 * {@code UnsupportedOperationException}.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.54 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public E set(int index, E element) {
17.68/7.54 throw new UnsupportedOperationException();
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation always throws an
17.68/7.54 * {@code UnsupportedOperationException}.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.54 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public void add(int index, E element) {
17.68/7.54 throw new UnsupportedOperationException();
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation always throws an
17.68/7.54 * {@code UnsupportedOperationException}.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public E remove(int index) {
17.68/7.54 throw new UnsupportedOperationException();
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 // Search Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation first gets a list iterator (with
17.68/7.54 * {@code listIterator()}). Then, it iterates over the list until the
17.68/7.54 * specified element is found or the end of the list is reached.
17.68/7.54 *
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public int indexOf(Object o) {
17.68/7.54 ListIterator e = listIterator();
17.68/7.54 if (o==null) {
17.68/7.54 while (e.hasNext())
17.68/7.54 if (e.next()==null)
17.68/7.54 return e.previousIndex();
17.68/7.54 } else {
17.68/7.54 while (e.hasNext())
17.68/7.54 if (o.equals(e.next()))
17.68/7.54 return e.previousIndex();
17.68/7.54 }
17.68/7.54 return -1;
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * This implementation first gets a list iterator that points to the end
17.68/7.54 * of the list (with {@code listIterator(size())}). Then, it iterates
17.68/7.54 * backwards over the list until the specified element is found, or the
17.68/7.54 * beginning of the list is reached.
17.68/7.54 *
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public int lastIndexOf(Object o) {
17.68/7.54 ListIterator e = listIterator(size());
17.68/7.54 if (o==null) {
17.68/7.54 while (e.hasPrevious())
17.68/7.54 if (e.previous()==null)
17.68/7.54 return e.nextIndex();
17.68/7.54 } else {
17.68/7.54 while (e.hasPrevious())
17.68/7.54 if (o.equals(e.previous()))
17.68/7.54 return e.nextIndex();
17.68/7.54 }
17.68/7.54 return -1;
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 // Bulk Operations
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Removes all of the elements from this list (optional operation).
17.68/7.54 * The list will be empty after this call returns.
17.68/7.54 *
17.68/7.54 * This implementation calls {@code removeRange(0, size())}.
17.68/7.54 *
17.68/7.54 *
Note that this implementation throws an
17.68/7.54 * {@code UnsupportedOperationException} unless {@code remove(int
17.68/7.54 * index)} or {@code removeRange(int fromIndex, int toIndex)} is
17.68/7.54 * overridden.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException if the {@code clear} operation
17.68/7.54 * is not supported by this list
17.68/7.54 */
17.68/7.54 public void clear() {
17.68/7.54 removeRange(0, size());
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 *
This implementation gets an iterator over the specified collection
17.68/7.54 * and iterates over it, inserting the elements obtained from the
17.68/7.54 * iterator into this list at the appropriate position, one at a time,
17.68/7.54 * using {@code add(int, E)}.
17.68/7.54 * Many implementations will override this method for efficiency.
17.68/7.54 *
17.68/7.54 *
Note that this implementation throws an
17.68/7.54 * {@code UnsupportedOperationException} unless
17.68/7.54 * {@link #add(int, Object) add(int, E)} is overridden.
17.68/7.54 *
17.68/7.54 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.54 * @throws ClassCastException {@inheritDoc}
17.68/7.54 * @throws NullPointerException {@inheritDoc}
17.68/7.54 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.54 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public boolean addAll(int index, Collection extends E> c) {
17.68/7.54 rangeCheckForAdd(index);
17.68/7.54 boolean modified = false;
17.68/7.54 Iterator extends E> e = c.iterator();
17.68/7.54 while (e.hasNext()) {
17.68/7.54 add(index++, e.next());
17.68/7.54 modified = true;
17.68/7.54 }
17.68/7.54 return modified;
17.68/7.54 }
17.68/7.54
17.68/7.54
17.68/7.54 // Iterators
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Returns an iterator over the elements in this list in proper sequence.
17.68/7.54 *
17.68/7.54 *
This implementation returns a straightforward implementation of the
17.68/7.54 * iterator interface, relying on the backing list's {@code size()},
17.68/7.54 * {@code get(int)}, and {@code remove(int)} methods.
17.68/7.54 *
17.68/7.54 *
Note that the iterator returned by this method will throw an
17.68/7.54 * {@link UnsupportedOperationException} in response to its
17.68/7.54 * {@code remove} method unless the list's {@code remove(int)} method is
17.68/7.54 * overridden.
17.68/7.54 *
17.68/7.54 *
This implementation can be made to throw runtime exceptions in the
17.68/7.54 * face of concurrent modification, as described in the specification
17.68/7.54 * for the (protected) {@link #modCount} field.
17.68/7.54 *
17.68/7.54 * @return an iterator over the elements in this list in proper sequence
17.68/7.54 */
17.68/7.54 public Iterator iterator() {
17.68/7.54 return new Itr();
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * This implementation returns {@code listIterator(0)}.
17.68/7.54 *
17.68/7.54 * @see #listIterator(int)
17.68/7.54 */
17.68/7.54 public ListIterator listIterator() {
17.68/7.54 return listIterator(0);
17.68/7.54 }
17.68/7.54
17.68/7.54 /**
17.68/7.54 * {@inheritDoc}
17.68/7.54 *
17.68/7.54 * This implementation returns a straightforward implementation of the
17.68/7.54 * {@code ListIterator} interface that extends the implementation of the
17.68/7.54 * {@code Iterator} interface returned by the {@code iterator()} method.
17.68/7.54 * The {@code ListIterator} implementation relies on the backing list's
17.68/7.54 * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
17.68/7.54 * and {@code remove(int)} methods.
17.68/7.54 *
17.68/7.54 *
Note that the list iterator returned by this implementation will
17.68/7.54 * throw an {@link UnsupportedOperationException} in response to its
17.68/7.54 * {@code remove}, {@code set} and {@code add} methods unless the
17.68/7.54 * list's {@code remove(int)}, {@code set(int, E)}, and
17.68/7.54 * {@code add(int, E)} methods are overridden.
17.68/7.54 *
17.68/7.54 *
This implementation can be made to throw runtime exceptions in the
17.68/7.54 * face of concurrent modification, as described in the specification for
17.68/7.54 * the (protected) {@link #modCount} field.
17.68/7.54 *
17.68/7.54 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.54 */
17.68/7.54 public ListIterator listIterator(final int index) {
17.68/7.54 rangeCheckForAdd(index);
17.68/7.54
17.68/7.54 return new ListItr(index);
17.68/7.54 }
17.68/7.54
17.68/7.54 private class Itr implements Iterator {
17.68/7.54 /**
17.68/7.54 * Index of element to be returned by subsequent call to next.
17.68/7.54 */
17.68/7.54 int cursor = 0;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * Index of element returned by most recent call to next or
17.68/7.54 * previous. Reset to -1 if this element is deleted by a call
17.68/7.54 * to remove.
17.68/7.54 */
17.68/7.54 int lastRet = -1;
17.68/7.54
17.68/7.54 /**
17.68/7.54 * The modCount value that the iterator believes that the backing
17.68/7.54 * List should have. If this expectation is violated, the iterator
17.68/7.54 * has detected concurrent modification.
17.68/7.54 */
17.68/7.54 int expectedModCount = modCount;
17.68/7.54
17.68/7.54 public boolean hasNext() {
17.68/7.54 return cursor != size();
17.68/7.54 }
17.68/7.54
17.68/7.54 public E next() {
17.68/7.54 checkForComodification();
17.68/7.54 try {
17.68/7.54 int i = cursor;
17.68/7.54 E next = get(i);
17.68/7.54 lastRet = i;
17.68/7.54 cursor = i + 1;
17.68/7.54 return next;
17.68/7.54 } catch (IndexOutOfBoundsException e) {
17.68/7.54 checkForComodification();
17.68/7.54 throw new NoSuchElementException();
17.68/7.54 }
17.68/7.54 }
17.68/7.54
17.68/7.54 public void remove() {
17.68/7.54 if (lastRet < 0)
17.68/7.54 throw new IllegalStateException();
17.68/7.54 checkForComodification();
17.68/7.54
17.68/7.54 try {
17.68/7.54 AbstractList.this.remove(lastRet);
17.68/7.55 if (lastRet < cursor)
17.68/7.55 cursor--;
17.68/7.55 lastRet = -1;
17.68/7.55 expectedModCount = modCount;
17.68/7.55 } catch (IndexOutOfBoundsException e) {
17.68/7.55 throw new ConcurrentModificationException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 final void checkForComodification() {
17.68/7.55 if (modCount != expectedModCount)
17.68/7.55 throw new ConcurrentModificationException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 private class ListItr extends Itr implements ListIterator {
17.68/7.55 ListItr(int index) {
17.68/7.55 cursor = index;
17.68/7.55 }
17.68/7.55
17.68/7.55 public boolean hasPrevious() {
17.68/7.55 return cursor != 0;
17.68/7.55 }
17.68/7.55
17.68/7.55 public E previous() {
17.68/7.55 checkForComodification();
17.68/7.55 try {
17.68/7.55 int i = cursor - 1;
17.68/7.55 E previous = get(i);
17.68/7.55 lastRet = cursor = i;
17.68/7.55 return previous;
17.68/7.55 } catch (IndexOutOfBoundsException e) {
17.68/7.55 checkForComodification();
17.68/7.55 throw new NoSuchElementException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 public int nextIndex() {
17.68/7.55 return cursor;
17.68/7.55 }
17.68/7.55
17.68/7.55 public int previousIndex() {
17.68/7.55 return cursor-1;
17.68/7.55 }
17.68/7.55
17.68/7.55 public void set(E e) {
17.68/7.55 if (lastRet < 0)
17.68/7.55 throw new IllegalStateException();
17.68/7.55 checkForComodification();
17.68/7.55
17.68/7.55 try {
17.68/7.55 AbstractList.this.set(lastRet, e);
17.68/7.55 expectedModCount = modCount;
17.68/7.55 } catch (IndexOutOfBoundsException ex) {
17.68/7.55 throw new ConcurrentModificationException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 public void add(E e) {
17.68/7.55 checkForComodification();
17.68/7.55
17.68/7.55 try {
17.68/7.55 int i = cursor;
17.68/7.55 AbstractList.this.add(i, e);
17.68/7.55 lastRet = -1;
17.68/7.55 cursor = i + 1;
17.68/7.55 expectedModCount = modCount;
17.68/7.55 } catch (IndexOutOfBoundsException ex) {
17.68/7.55 throw new ConcurrentModificationException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * {@inheritDoc}
17.68/7.55 *
17.68/7.55 * This implementation returns a list that subclasses
17.68/7.55 * {@code AbstractList}. The subclass stores, in private fields, the
17.68/7.55 * offset of the subList within the backing list, the size of the subList
17.68/7.55 * (which can change over its lifetime), and the expected
17.68/7.55 * {@code modCount} value of the backing list. There are two variants
17.68/7.55 * of the subclass, one of which implements {@code RandomAccess}.
17.68/7.55 * If this list implements {@code RandomAccess} the returned list will
17.68/7.55 * be an instance of the subclass that implements {@code RandomAccess}.
17.68/7.55 *
17.68/7.55 *
The subclass's {@code set(int, E)}, {@code get(int)},
17.68/7.55 * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
17.68/7.55 * Collection)} and {@code removeRange(int, int)} methods all
17.68/7.55 * delegate to the corresponding methods on the backing abstract list,
17.68/7.55 * after bounds-checking the index and adjusting for the offset. The
17.68/7.55 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
17.68/7.55 * c)}.
17.68/7.55 *
17.68/7.55 *
The {@code listIterator(int)} method returns a "wrapper object"
17.68/7.55 * over a list iterator on the backing list, which is created with the
17.68/7.55 * corresponding method on the backing list. The {@code iterator} method
17.68/7.55 * merely returns {@code listIterator()}, and the {@code size} method
17.68/7.55 * merely returns the subclass's {@code size} field.
17.68/7.55 *
17.68/7.55 *
All methods first check to see if the actual {@code modCount} of
17.68/7.55 * the backing list is equal to its expected value, and throw a
17.68/7.55 * {@code ConcurrentModificationException} if it is not.
17.68/7.55 *
17.68/7.55 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
17.68/7.55 * {@code (fromIndex < 0 || toIndex > size)}
17.68/7.55 * @throws IllegalArgumentException if the endpoint indices are out of order
17.68/7.55 * {@code (fromIndex > toIndex)}
17.68/7.55 */
17.68/7.55 public List subList(int fromIndex, int toIndex) {
17.68/7.55 return (this instanceof RandomAccess ?
17.68/7.55 new RandomAccessSubList(this, fromIndex, toIndex) :
17.68/7.55 new SubList(this, fromIndex, toIndex));
17.68/7.55 }
17.68/7.55
17.68/7.55 // Comparison and hashing
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Compares the specified object with this list for equality. Returns
17.68/7.55 * {@code true} if and only if the specified object is also a list, both
17.68/7.55 * lists have the same size, and all corresponding pairs of elements in
17.68/7.55 * the two lists are equal. (Two elements {@code e1} and
17.68/7.55 * {@code e2} are equal if {@code (e1==null ? e2==null :
17.68/7.55 * e1.equals(e2))}.) In other words, two lists are defined to be
17.68/7.55 * equal if they contain the same elements in the same order.
17.68/7.55 *
17.68/7.55 * This implementation first checks if the specified object is this
17.68/7.55 * list. If so, it returns {@code true}; if not, it checks if the
17.68/7.55 * specified object is a list. If not, it returns {@code false}; if so,
17.68/7.55 * it iterates over both lists, comparing corresponding pairs of elements.
17.68/7.55 * If any comparison returns {@code false}, this method returns
17.68/7.55 * {@code false}. If either iterator runs out of elements before the
17.68/7.55 * other it returns {@code false} (as the lists are of unequal length);
17.68/7.55 * otherwise it returns {@code true} when the iterations complete.
17.68/7.55 *
17.68/7.55 * @param o the object to be compared for equality with this list
17.68/7.55 * @return {@code true} if the specified object is equal to this list
17.68/7.55 */
17.68/7.55 public boolean equals(Object o) {
17.68/7.55 if (o == this)
17.68/7.55 return true;
17.68/7.55 if (!(o instanceof List))
17.68/7.55 return false;
17.68/7.55
17.68/7.55 ListIterator e1 = listIterator();
17.68/7.55 ListIterator e2 = ((List) o).listIterator();
17.68/7.55 while(e1.hasNext() && e2.hasNext()) {
17.68/7.55 E o1 = e1.next();
17.68/7.55 Object o2 = e2.next();
17.68/7.55 if (!(o1==null ? o2==null : o1.equals(o2)))
17.68/7.55 return false;
17.68/7.55 }
17.68/7.55 return !(e1.hasNext() || e2.hasNext());
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns the hash code value for this list.
17.68/7.55 *
17.68/7.55 * This implementation uses exactly the code that is used to define the
17.68/7.55 * list hash function in the documentation for the {@link List#hashCode}
17.68/7.55 * method.
17.68/7.55 *
17.68/7.55 * @return the hash code value for this list
17.68/7.55 */
17.68/7.55 public int hashCode() {
17.68/7.55 int hashCode = 1;
17.68/7.55 Iterator it = this.iterator();
17.68/7.55 while (it.hasNext()) {
17.68/7.55 E e = it.next();
17.68/7.55 hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
17.68/7.55 }
17.68/7.55 return hashCode;
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes from this list all of the elements whose index is between
17.68/7.55 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
17.68/7.55 * Shifts any succeeding elements to the left (reduces their index).
17.68/7.55 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
17.68/7.55 * (If {@code toIndex==fromIndex}, this operation has no effect.)
17.68/7.55 *
17.68/7.55 * This method is called by the {@code clear} operation on this list
17.68/7.55 * and its subLists. Overriding this method to take advantage of
17.68/7.55 * the internals of the list implementation can substantially
17.68/7.55 * improve the performance of the {@code clear} operation on this list
17.68/7.55 * and its subLists.
17.68/7.55 *
17.68/7.55 *
This implementation gets a list iterator positioned before
17.68/7.55 * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
17.68/7.55 * followed by {@code ListIterator.remove} until the entire range has
17.68/7.55 * been removed. Note: if {@code ListIterator.remove} requires linear
17.68/7.55 * time, this implementation requires quadratic time.
17.68/7.55 *
17.68/7.55 * @param fromIndex index of first element to be removed
17.68/7.55 * @param toIndex index after last element to be removed
17.68/7.55 */
17.68/7.55 protected void removeRange(int fromIndex, int toIndex) {
17.68/7.55 ListIterator it = listIterator(fromIndex);
17.68/7.55 for (int i=0, n=toIndex-fromIndex; istructurally modified.
17.68/7.55 * Structural modifications are those that change the size of the
17.68/7.55 * list, or otherwise perturb it in such a fashion that iterations in
17.68/7.55 * progress may yield incorrect results.
17.68/7.55 *
17.68/7.55 * This field is used by the iterator and list iterator implementation
17.68/7.55 * returned by the {@code iterator} and {@code listIterator} methods.
17.68/7.55 * If the value of this field changes unexpectedly, the iterator (or list
17.68/7.55 * iterator) will throw a {@code ConcurrentModificationException} in
17.68/7.55 * response to the {@code next}, {@code remove}, {@code previous},
17.68/7.55 * {@code set} or {@code add} operations. This provides
17.68/7.55 * fail-fast behavior, rather than non-deterministic behavior in
17.68/7.55 * the face of concurrent modification during iteration.
17.68/7.55 *
17.68/7.55 *
Use of this field by subclasses is optional. If a subclass
17.68/7.55 * wishes to provide fail-fast iterators (and list iterators), then it
17.68/7.55 * merely has to increment this field in its {@code add(int, E)} and
17.68/7.55 * {@code remove(int)} methods (and any other methods that it overrides
17.68/7.55 * that result in structural modifications to the list). A single call to
17.68/7.55 * {@code add(int, E)} or {@code remove(int)} must add no more than
17.68/7.55 * one to this field, or the iterators (and list iterators) will throw
17.68/7.55 * bogus {@code ConcurrentModificationExceptions}. If an implementation
17.68/7.55 * does not wish to provide fail-fast iterators, this field may be
17.68/7.55 * ignored.
17.68/7.55 */
17.68/7.55 protected transient int modCount = 0;
17.68/7.55
17.68/7.55 private void rangeCheckForAdd(int index) {
17.68/7.55 if (index < 0 || index > size())
17.68/7.55 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
17.68/7.55 }
17.68/7.55
17.68/7.55 private String outOfBoundsMsg(int index) {
17.68/7.55 return "";
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 class SubList extends AbstractList {
17.68/7.55 private final AbstractList l;
17.68/7.55 private final int offset;
17.68/7.55 private int size;
17.68/7.55
17.68/7.55 SubList(AbstractList list, int fromIndex, int toIndex) {
17.68/7.55 if (fromIndex < 0)
17.68/7.55 throw new IndexOutOfBoundsException();
17.68/7.55 if (toIndex > list.size())
17.68/7.55 throw new IndexOutOfBoundsException();
17.68/7.55 if (fromIndex > toIndex)
17.68/7.55 throw new IllegalArgumentException();
17.68/7.55 l = list;
17.68/7.55 offset = fromIndex;
17.68/7.55 size = toIndex - fromIndex;
17.68/7.55 this.modCount = l.modCount;
17.68/7.55 }
17.68/7.55
17.68/7.55 public E set(int index, E element) {
17.68/7.55 rangeCheck(index);
17.68/7.55 checkForComodification();
17.68/7.55 return l.set(index+offset, element);
17.68/7.55 }
17.68/7.55
17.68/7.55 public E get(int index) {
17.68/7.55 rangeCheck(index);
17.68/7.55 checkForComodification();
17.68/7.55 return l.get(index+offset);
17.68/7.55 }
17.68/7.55
17.68/7.55 public int size() {
17.68/7.55 checkForComodification();
17.68/7.55 return size;
17.68/7.55 }
17.68/7.55
17.68/7.55 public void add(int index, E element) {
17.68/7.55 rangeCheckForAdd(index);
17.68/7.55 checkForComodification();
17.68/7.55 l.add(index+offset, element);
17.68/7.55 this.modCount = l.modCount;
17.68/7.55 size++;
17.68/7.55 }
17.68/7.55
17.68/7.55 public E remove(int index) {
17.68/7.55 rangeCheck(index);
17.68/7.55 checkForComodification();
17.68/7.55 E result = l.remove(index+offset);
17.68/7.55 this.modCount = l.modCount;
17.68/7.55 size--;
17.68/7.55 return result;
17.68/7.55 }
17.68/7.55
17.68/7.55 protected void removeRange(int fromIndex, int toIndex) {
17.68/7.55 checkForComodification();
17.68/7.55 l.removeRange(fromIndex+offset, toIndex+offset);
17.68/7.55 this.modCount = l.modCount;
17.68/7.55 size -= (toIndex-fromIndex);
17.68/7.55 }
17.68/7.55
17.68/7.55 public boolean addAll(Collection extends E> c) {
17.68/7.55 return addAll(size, c);
17.68/7.55 }
17.68/7.55
17.68/7.55 public boolean addAll(int index, Collection extends E> c) {
17.68/7.55 rangeCheckForAdd(index);
17.68/7.55 int cSize = c.size();
17.68/7.55 if (cSize==0)
17.68/7.55 return false;
17.68/7.55
17.68/7.55 checkForComodification();
17.68/7.55 l.addAll(offset+index, c);
17.68/7.55 this.modCount = l.modCount;
17.68/7.55 size += cSize;
17.68/7.55 return true;
17.68/7.55 }
17.68/7.55
17.68/7.55 public Iterator iterator() {
17.68/7.55 return listIterator();
17.68/7.55 }
17.68/7.55
17.68/7.55 public ListIterator listIterator(final int index) {
17.68/7.55 checkForComodification();
17.68/7.55 rangeCheckForAdd(index);
17.68/7.55
17.68/7.55 return new ListIterator() {
17.68/7.55 private final ListIterator i = l.listIterator(index+offset);
17.68/7.55
17.68/7.55 public boolean hasNext() {
17.68/7.55 return nextIndex() < size;
17.68/7.55 }
17.68/7.55
17.68/7.55 public E next() {
17.68/7.55 if (hasNext())
17.68/7.55 return i.next();
17.68/7.55 else
17.68/7.55 throw new NoSuchElementException();
17.68/7.55 }
17.68/7.55
17.68/7.55 public boolean hasPrevious() {
17.68/7.55 return previousIndex() >= 0;
17.68/7.55 }
17.68/7.55
17.68/7.55 public E previous() {
17.68/7.55 if (hasPrevious())
17.68/7.55 return i.previous();
17.68/7.55 else
17.68/7.55 throw new NoSuchElementException();
17.68/7.55 }
17.68/7.55
17.68/7.55 public int nextIndex() {
17.68/7.55 return i.nextIndex() - offset;
17.68/7.55 }
17.68/7.55
17.68/7.55 public int previousIndex() {
17.68/7.55 return i.previousIndex() - offset;
17.68/7.55 }
17.68/7.55
17.68/7.55 public void remove() {
17.68/7.55 i.remove();
17.68/7.55 SubList.this.modCount = l.modCount;
17.68/7.55 size--;
17.68/7.55 }
17.68/7.55
17.68/7.55 public void set(E e) {
17.68/7.55 i.set(e);
17.68/7.55 }
17.68/7.55
17.68/7.55 public void add(E e) {
17.68/7.55 i.add(e);
17.68/7.55 SubList.this.modCount = l.modCount;
17.68/7.55 size++;
17.68/7.55 }
17.68/7.55 };
17.68/7.55 }
17.68/7.55
17.68/7.55 public List subList(int fromIndex, int toIndex) {
17.68/7.55 return new SubList(this, fromIndex, toIndex);
17.68/7.55 }
17.68/7.55
17.68/7.55 private void rangeCheck(int index) {
17.68/7.55 if (index < 0 || index >= size)
17.68/7.55 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
17.68/7.55 }
17.68/7.55
17.68/7.55 private void rangeCheckForAdd(int index) {
17.68/7.55 if (index < 0 || index > size)
17.68/7.55 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
17.68/7.55 }
17.68/7.55
17.68/7.55 private String outOfBoundsMsg(int index) {
17.68/7.55 return "";
17.68/7.55 }
17.68/7.55
17.68/7.55 private void checkForComodification() {
17.68/7.55 if (this.modCount != l.modCount)
17.68/7.55 throw new ConcurrentModificationException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 class RandomAccessSubList extends SubList implements RandomAccess {
17.68/7.55 RandomAccessSubList(AbstractList list, int fromIndex, int toIndex) {
17.68/7.55 super(list, fromIndex, toIndex);
17.68/7.55 }
17.68/7.55
17.68/7.55 public List subList(int fromIndex, int toIndex) {
17.68/7.55 return new RandomAccessSubList(this, fromIndex, toIndex);
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * This class provides a skeletal implementation of the List
17.68/7.55 * interface to minimize the effort required to implement this interface
17.68/7.55 * backed by a "sequential access" data store (such as a linked list). For
17.68/7.55 * random access data (such as an array), AbstractList should be used
17.68/7.55 * in preference to this class.
17.68/7.55 *
17.68/7.55 * This class is the opposite of the AbstractList class in the sense
17.68/7.55 * that it implements the "random access" methods (get(int index),
17.68/7.55 * set(int index, E element), add(int index, E element) and
17.68/7.55 * remove(int index)) on top of the list's list iterator, instead of
17.68/7.55 * the other way around.
17.68/7.55 *
17.68/7.55 * To implement a list the programmer needs only to extend this class and
17.68/7.55 * provide implementations for the listIterator and size
17.68/7.55 * methods. For an unmodifiable list, the programmer need only implement the
17.68/7.55 * list iterator's hasNext, next, hasPrevious,
17.68/7.55 * previous and index methods.
17.68/7.55 *
17.68/7.55 * For a modifiable list the programmer should additionally implement the list
17.68/7.55 * iterator's set method. For a variable-size list the programmer
17.68/7.55 * should additionally implement the list iterator's remove and
17.68/7.55 * add methods.
17.68/7.55 *
17.68/7.55 * The programmer should generally provide a void (no argument) and collection
17.68/7.55 * constructor, as per the recommendation in the Collection interface
17.68/7.55 * specification.
17.68/7.55 *
17.68/7.55 * This class is a member of the
17.68/7.55 *
17.68/7.55 * Java Collections Framework.
17.68/7.55 *
17.68/7.55 * @author Josh Bloch
17.68/7.55 * @author Neal Gafter
17.68/7.55 * @see Collection
17.68/7.55 * @see List
17.68/7.55 * @see AbstractList
17.68/7.55 * @see AbstractCollection
17.68/7.55 * @since 1.2
17.68/7.55 */
17.68/7.55
17.68/7.55 public abstract class AbstractSequentialList extends AbstractList {
17.68/7.55 /**
17.68/7.55 * Sole constructor. (For invocation by subclass constructors, typically
17.68/7.55 * implicit.)
17.68/7.55 */
17.68/7.55 protected AbstractSequentialList() {
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns the element at the specified position in this list.
17.68/7.55 *
17.68/7.55 * This implementation first gets a list iterator pointing to the
17.68/7.55 * indexed element (with listIterator(index)). Then, it gets
17.68/7.55 * the element using ListIterator.next and returns it.
17.68/7.55 *
17.68/7.55 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.55 */
17.68/7.55 public E get(int index) {
17.68/7.55 try {
17.68/7.55 return listIterator(index).next();
17.68/7.55 } catch (NoSuchElementException exc) {
17.68/7.55 throw new IndexOutOfBoundsException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Replaces the element at the specified position in this list with the
17.68/7.55 * specified element (optional operation).
17.68/7.55 *
17.68/7.55 *
This implementation first gets a list iterator pointing to the
17.68/7.55 * indexed element (with listIterator(index)). Then, it gets
17.68/7.55 * the current element using ListIterator.next and replaces it
17.68/7.55 * with ListIterator.set.
17.68/7.55 *
17.68/7.55 *
Note that this implementation will throw an
17.68/7.55 * UnsupportedOperationException if the list iterator does not
17.68/7.55 * implement the set operation.
17.68/7.55 *
17.68/7.55 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.55 * @throws ClassCastException {@inheritDoc}
17.68/7.55 * @throws NullPointerException {@inheritDoc}
17.68/7.55 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.55 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.55 */
17.68/7.55 public E set(int index, E element) {
17.68/7.55 try {
17.68/7.55 ListIterator e = listIterator(index);
17.68/7.55 E oldVal = e.next();
17.68/7.55 e.set(element);
17.68/7.55 return oldVal;
17.68/7.55 } catch (NoSuchElementException exc) {
17.68/7.55 throw new IndexOutOfBoundsException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Inserts the specified element at the specified position in this list
17.68/7.55 * (optional operation). Shifts the element currently at that position
17.68/7.55 * (if any) and any subsequent elements to the right (adds one to their
17.68/7.55 * indices).
17.68/7.55 *
17.68/7.55 * This implementation first gets a list iterator pointing to the
17.68/7.55 * indexed element (with listIterator(index)). Then, it
17.68/7.55 * inserts the specified element with ListIterator.add.
17.68/7.55 *
17.68/7.55 *
Note that this implementation will throw an
17.68/7.55 * UnsupportedOperationException if the list iterator does not
17.68/7.55 * implement the add operation.
17.68/7.55 *
17.68/7.55 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.55 * @throws ClassCastException {@inheritDoc}
17.68/7.55 * @throws NullPointerException {@inheritDoc}
17.68/7.55 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.55 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.55 */
17.68/7.55 public void add(int index, E element) {
17.68/7.55 try {
17.68/7.55 listIterator(index).add(element);
17.68/7.55 } catch (NoSuchElementException exc) {
17.68/7.55 throw new IndexOutOfBoundsException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes the element at the specified position in this list (optional
17.68/7.55 * operation). Shifts any subsequent elements to the left (subtracts one
17.68/7.55 * from their indices). Returns the element that was removed from the
17.68/7.55 * list.
17.68/7.55 *
17.68/7.55 *
This implementation first gets a list iterator pointing to the
17.68/7.55 * indexed element (with listIterator(index)). Then, it removes
17.68/7.55 * the element with ListIterator.remove.
17.68/7.55 *
17.68/7.55 *
Note that this implementation will throw an
17.68/7.55 * UnsupportedOperationException if the list iterator does not
17.68/7.55 * implement the remove operation.
17.68/7.55 *
17.68/7.55 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.55 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.55 */
17.68/7.55 public E remove(int index) {
17.68/7.55 try {
17.68/7.55 ListIterator e = listIterator(index);
17.68/7.55 E outCast = e.next();
17.68/7.55 e.remove();
17.68/7.55 return outCast;
17.68/7.55 } catch (NoSuchElementException exc) {
17.68/7.55 throw new IndexOutOfBoundsException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 // Bulk Operations
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Inserts all of the elements in the specified collection into this
17.68/7.55 * list at the specified position (optional operation). Shifts the
17.68/7.55 * element currently at that position (if any) and any subsequent
17.68/7.55 * elements to the right (increases their indices). The new elements
17.68/7.55 * will appear in this list in the order that they are returned by the
17.68/7.55 * specified collection's iterator. The behavior of this operation is
17.68/7.55 * undefined if the specified collection is modified while the
17.68/7.55 * operation is in progress. (Note that this will occur if the specified
17.68/7.55 * collection is this list, and it's nonempty.)
17.68/7.55 *
17.68/7.55 * This implementation gets an iterator over the specified collection and
17.68/7.55 * a list iterator over this list pointing to the indexed element (with
17.68/7.55 * listIterator(index)). Then, it iterates over the specified
17.68/7.55 * collection, inserting the elements obtained from the iterator into this
17.68/7.55 * list, one at a time, using ListIterator.add followed by
17.68/7.55 * ListIterator.next (to skip over the added element).
17.68/7.55 *
17.68/7.55 *
Note that this implementation will throw an
17.68/7.55 * UnsupportedOperationException if the list iterator returned by
17.68/7.55 * the listIterator method does not implement the add
17.68/7.55 * operation.
17.68/7.55 *
17.68/7.55 * @throws UnsupportedOperationException {@inheritDoc}
17.68/7.55 * @throws ClassCastException {@inheritDoc}
17.68/7.55 * @throws NullPointerException {@inheritDoc}
17.68/7.55 * @throws IllegalArgumentException {@inheritDoc}
17.68/7.55 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.55 */
17.68/7.55 public boolean addAll(int index, Collection extends E> c) {
17.68/7.55 try {
17.68/7.55 boolean modified = false;
17.68/7.55 ListIterator e1 = listIterator(index);
17.68/7.55 Iterator extends E> e2 = c.iterator();
17.68/7.55 while (e2.hasNext()) {
17.68/7.55 e1.add(e2.next());
17.68/7.55 modified = true;
17.68/7.55 }
17.68/7.55 return modified;
17.68/7.55 } catch (NoSuchElementException exc) {
17.68/7.55 throw new IndexOutOfBoundsException();
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 // Iterators
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns an iterator over the elements in this list (in proper
17.68/7.55 * sequence).
17.68/7.55 *
17.68/7.55 * This implementation merely returns a list iterator over the list.
17.68/7.55 *
17.68/7.55 * @return an iterator over the elements in this list (in proper sequence)
17.68/7.55 */
17.68/7.55 public Iterator iterator() {
17.68/7.55 return listIterator();
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns a list iterator over the elements in this list (in proper
17.68/7.55 * sequence).
17.68/7.55 *
17.68/7.55 * @param index index of first element to be returned from the list
17.68/7.55 * iterator (by a call to the next
method)
17.68/7.55 * @return a list iterator over the elements in this list (in proper
17.68/7.55 * sequence)
17.68/7.55 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.55 */
17.68/7.55 public abstract ListIterator listIterator(int index);
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * The root interface in the collection hierarchy. A collection
17.68/7.55 * represents a group of objects, known as its elements. Some
17.68/7.55 * collections allow duplicate elements and others do not. Some are ordered
17.68/7.55 * and others unordered. The JDK does not provide any direct
17.68/7.55 * implementations of this interface: it provides implementations of more
17.68/7.55 * specific subinterfaces like Set and List. This interface
17.68/7.55 * is typically used to pass collections around and manipulate them where
17.68/7.55 * maximum generality is desired.
17.68/7.55 *
17.68/7.55 * Bags or multisets (unordered collections that may contain
17.68/7.55 * duplicate elements) should implement this interface directly.
17.68/7.55 *
17.68/7.55 *
All general-purpose Collection implementation classes (which
17.68/7.55 * typically implement Collection indirectly through one of its
17.68/7.55 * subinterfaces) should provide two "standard" constructors: a void (no
17.68/7.55 * arguments) constructor, which creates an empty collection, and a
17.68/7.55 * constructor with a single argument of type Collection, which
17.68/7.55 * creates a new collection with the same elements as its argument. In
17.68/7.55 * effect, the latter constructor allows the user to copy any collection,
17.68/7.55 * producing an equivalent collection of the desired implementation type.
17.68/7.55 * There is no way to enforce this convention (as interfaces cannot contain
17.68/7.55 * constructors) but all of the general-purpose Collection
17.68/7.55 * implementations in the Java platform libraries comply.
17.68/7.55 *
17.68/7.55 *
The "destructive" methods contained in this interface, that is, the
17.68/7.55 * methods that modify the collection on which they operate, are specified to
17.68/7.55 * throw UnsupportedOperationException if this collection does not
17.68/7.55 * support the operation. If this is the case, these methods may, but are not
17.68/7.55 * required to, throw an UnsupportedOperationException if the
17.68/7.55 * invocation would have no effect on the collection. For example, invoking
17.68/7.55 * the {@link #addAll(Collection)} method on an unmodifiable collection may,
17.68/7.55 * but is not required to, throw the exception if the collection to be added
17.68/7.55 * is empty.
17.68/7.55 *
17.68/7.55 *
Some collection implementations have restrictions on the elements that
17.68/7.55 * they may contain. For example, some implementations prohibit null elements,
17.68/7.55 * and some have restrictions on the types of their elements. Attempting to
17.68/7.55 * add an ineligible element throws an unchecked exception, typically
17.68/7.55 * NullPointerException or ClassCastException. Attempting
17.68/7.55 * to query the presence of an ineligible element may throw an exception,
17.68/7.55 * or it may simply return false; some implementations will exhibit the former
17.68/7.55 * behavior and some will exhibit the latter. More generally, attempting an
17.68/7.55 * operation on an ineligible element whose completion would not result in
17.68/7.55 * the insertion of an ineligible element into the collection may throw an
17.68/7.55 * exception or it may succeed, at the option of the implementation.
17.68/7.55 * Such exceptions are marked as "optional" in the specification for this
17.68/7.55 * interface.
17.68/7.55 *
17.68/7.55 *
It is up to each collection to determine its own synchronization
17.68/7.55 * policy. In the absence of a stronger guarantee by the
17.68/7.55 * implementation, undefined behavior may result from the invocation
17.68/7.55 * of any method on a collection that is being mutated by another
17.68/7.55 * thread; this includes direct invocations, passing the collection to
17.68/7.55 * a method that might perform invocations, and using an existing
17.68/7.55 * iterator to examine the collection.
17.68/7.55 *
17.68/7.55 *
Many methods in Collections Framework interfaces are defined in
17.68/7.55 * terms of the {@link Object#equals(Object) equals} method. For example,
17.68/7.55 * the specification for the {@link #contains(Object) contains(Object o)}
17.68/7.55 * method says: "returns true if and only if this collection
17.68/7.55 * contains at least one element e such that
17.68/7.55 * (o==null ? e==null : o.equals(e))." This specification should
17.68/7.55 * not be construed to imply that invoking Collection.contains
17.68/7.55 * with a non-null argument o will cause o.equals(e) to be
17.68/7.55 * invoked for any element e. Implementations are free to implement
17.68/7.55 * optimizations whereby the equals invocation is avoided, for
17.68/7.55 * example, by first comparing the hash codes of the two elements. (The
17.68/7.55 * {@link Object#hashCode()} specification guarantees that two objects with
17.68/7.55 * unequal hash codes cannot be equal.) More generally, implementations of
17.68/7.55 * the various Collections Framework interfaces are free to take advantage of
17.68/7.55 * the specified behavior of underlying {@link Object} methods wherever the
17.68/7.55 * implementor deems it appropriate.
17.68/7.55 *
17.68/7.55 *
This interface is a member of the
17.68/7.55 *
17.68/7.55 * Java Collections Framework.
17.68/7.55 *
17.68/7.55 * @author Josh Bloch
17.68/7.55 * @author Neal Gafter
17.68/7.55 * @see Set
17.68/7.55 * @see List
17.68/7.55 * @see Map
17.68/7.55 * @see SortedSet
17.68/7.55 * @see SortedMap
17.68/7.55 * @see HashSet
17.68/7.55 * @see TreeSet
17.68/7.55 * @see ArrayList
17.68/7.55 * @see LinkedList
17.68/7.55 * @see Vector
17.68/7.55 * @see Collections
17.68/7.55 * @see Arrays
17.68/7.55 * @see AbstractCollection
17.68/7.55 * @since 1.2
17.68/7.55 */
17.68/7.55
17.68/7.55 public interface Collection {
17.68/7.55 // Query Operations
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns the number of elements in this collection. If this collection
17.68/7.55 * contains more than Integer.MAX_VALUE elements, returns
17.68/7.55 * Integer.MAX_VALUE.
17.68/7.55 *
17.68/7.55 * @return the number of elements in this collection
17.68/7.55 */
17.68/7.55 int size();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns true if this collection contains no elements.
17.68/7.55 *
17.68/7.55 * @return true if this collection contains no elements
17.68/7.55 */
17.68/7.55 boolean isEmpty();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns true if this collection contains the specified element.
17.68/7.55 * More formally, returns true if and only if this collection
17.68/7.55 * contains at least one element e such that
17.68/7.55 * (o==null ? e==null : o.equals(e)).
17.68/7.55 *
17.68/7.55 * @param o element whose presence in this collection is to be tested
17.68/7.55 * @return true if this collection contains the specified
17.68/7.55 * element
17.68/7.55 * @throws ClassCastException if the type of the specified element
17.68/7.55 * is incompatible with this collection (optional)
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * collection does not permit null elements (optional)
17.68/7.55 */
17.68/7.55 boolean contains(Object o);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns an iterator over the elements in this collection. There are no
17.68/7.55 * guarantees concerning the order in which the elements are returned
17.68/7.55 * (unless this collection is an instance of some class that provides a
17.68/7.55 * guarantee).
17.68/7.55 *
17.68/7.55 * @return an Iterator over the elements in this collection
17.68/7.55 */
17.68/7.55 Iterator iterator();
17.68/7.55
17.68/7.55 // Modification Operations
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Ensures that this collection contains the specified element (optional
17.68/7.55 * operation). Returns true if this collection changed as a
17.68/7.55 * result of the call. (Returns false if this collection does
17.68/7.55 * not permit duplicates and already contains the specified element.)
17.68/7.55 *
17.68/7.55 * Collections that support this operation may place limitations on what
17.68/7.55 * elements may be added to this collection. In particular, some
17.68/7.55 * collections will refuse to add null elements, and others will
17.68/7.55 * impose restrictions on the type of elements that may be added.
17.68/7.55 * Collection classes should clearly specify in their documentation any
17.68/7.55 * restrictions on what elements may be added.
17.68/7.55 *
17.68/7.55 * If a collection refuses to add a particular element for any reason
17.68/7.55 * other than that it already contains the element, it must throw
17.68/7.55 * an exception (rather than returning false). This preserves
17.68/7.55 * the invariant that a collection always contains the specified element
17.68/7.55 * after this call returns.
17.68/7.55 *
17.68/7.55 * @param e element whose presence in this collection is to be ensured
17.68/7.55 * @return true if this collection changed as a result of the
17.68/7.55 * call
17.68/7.55 * @throws UnsupportedOperationException if the add operation
17.68/7.55 * is not supported by this collection
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this collection
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * collection does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the element
17.68/7.55 * prevents it from being added to this collection
17.68/7.55 * @throws IllegalStateException if the element cannot be added at this
17.68/7.55 * time due to insertion restrictions
17.68/7.55 */
17.68/7.55 boolean add(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes a single instance of the specified element from this
17.68/7.55 * collection, if it is present (optional operation). More formally,
17.68/7.55 * removes an element e such that
17.68/7.55 * (o==null ? e==null : o.equals(e)), if
17.68/7.55 * this collection contains one or more such elements. Returns
17.68/7.55 * true if this collection contained the specified element (or
17.68/7.55 * equivalently, if this collection changed as a result of the call).
17.68/7.55 *
17.68/7.55 * @param o element to be removed from this collection, if present
17.68/7.55 * @return true if an element was removed as a result of this call
17.68/7.55 * @throws ClassCastException if the type of the specified element
17.68/7.55 * is incompatible with this collection (optional)
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * collection does not permit null elements (optional)
17.68/7.55 * @throws UnsupportedOperationException if the remove operation
17.68/7.55 * is not supported by this collection
17.68/7.55 */
17.68/7.55 boolean remove(Object o);
17.68/7.55
17.68/7.55
17.68/7.55 // Bulk Operations
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns true if this collection contains all of the elements
17.68/7.55 * in the specified collection.
17.68/7.55 *
17.68/7.55 * @param c collection to be checked for containment in this collection
17.68/7.55 * @return true if this collection contains all of the elements
17.68/7.55 * in the specified collection
17.68/7.55 * @throws ClassCastException if the types of one or more elements
17.68/7.55 * in the specified collection are incompatible with this
17.68/7.55 * collection (optional)
17.68/7.55 * @throws NullPointerException if the specified collection contains one
17.68/7.55 * or more null elements and this collection does not permit null
17.68/7.55 * elements (optional), or if the specified collection is null
17.68/7.55 * @see #contains(Object)
17.68/7.55 */
17.68/7.55 boolean containsAll(Collection> c);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Adds all of the elements in the specified collection to this collection
17.68/7.55 * (optional operation). The behavior of this operation is undefined if
17.68/7.55 * the specified collection is modified while the operation is in progress.
17.68/7.55 * (This implies that the behavior of this call is undefined if the
17.68/7.55 * specified collection is this collection, and this collection is
17.68/7.55 * nonempty.)
17.68/7.55 *
17.68/7.55 * @param c collection containing elements to be added to this collection
17.68/7.55 * @return true if this collection changed as a result of the call
17.68/7.55 * @throws UnsupportedOperationException if the addAll operation
17.68/7.55 * is not supported by this collection
17.68/7.55 * @throws ClassCastException if the class of an element of the specified
17.68/7.55 * collection prevents it from being added to this collection
17.68/7.55 * @throws NullPointerException if the specified collection contains a
17.68/7.55 * null element and this collection does not permit null elements,
17.68/7.55 * or if the specified collection is null
17.68/7.55 * @throws IllegalArgumentException if some property of an element of the
17.68/7.55 * specified collection prevents it from being added to this
17.68/7.55 * collection
17.68/7.55 * @throws IllegalStateException if not all the elements can be added at
17.68/7.55 * this time due to insertion restrictions
17.68/7.55 * @see #add(Object)
17.68/7.55 */
17.68/7.55 boolean addAll(Collection extends E> c);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes all of this collection's elements that are also contained in the
17.68/7.55 * specified collection (optional operation). After this call returns,
17.68/7.55 * this collection will contain no elements in common with the specified
17.68/7.55 * collection.
17.68/7.55 *
17.68/7.55 * @param c collection containing elements to be removed from this collection
17.68/7.55 * @return true if this collection changed as a result of the
17.68/7.55 * call
17.68/7.55 * @throws UnsupportedOperationException if the removeAll method
17.68/7.55 * is not supported by this collection
17.68/7.55 * @throws ClassCastException if the types of one or more elements
17.68/7.55 * in this collection are incompatible with the specified
17.68/7.55 * collection (optional)
17.68/7.55 * @throws NullPointerException if this collection contains one or more
17.68/7.55 * null elements and the specified collection does not support
17.68/7.55 * null elements (optional), or if the specified collection is null
17.68/7.55 * @see #remove(Object)
17.68/7.55 * @see #contains(Object)
17.68/7.55 */
17.68/7.55 boolean removeAll(Collection> c);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retains only the elements in this collection that are contained in the
17.68/7.55 * specified collection (optional operation). In other words, removes from
17.68/7.55 * this collection all of its elements that are not contained in the
17.68/7.55 * specified collection.
17.68/7.55 *
17.68/7.55 * @param c collection containing elements to be retained in this collection
17.68/7.55 * @return true if this collection changed as a result of the call
17.68/7.55 * @throws UnsupportedOperationException if the retainAll operation
17.68/7.55 * is not supported by this collection
17.68/7.55 * @throws ClassCastException if the types of one or more elements
17.68/7.55 * in this collection are incompatible with the specified
17.68/7.55 * collection (optional)
17.68/7.55 * @throws NullPointerException if this collection contains one or more
17.68/7.55 * null elements and the specified collection does not permit null
17.68/7.55 * elements (optional), or if the specified collection is null
17.68/7.55 * @see #remove(Object)
17.68/7.55 * @see #contains(Object)
17.68/7.55 */
17.68/7.55 boolean retainAll(Collection> c);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes all of the elements from this collection (optional operation).
17.68/7.55 * The collection will be empty after this method returns.
17.68/7.55 *
17.68/7.55 * @throws UnsupportedOperationException if the clear operation
17.68/7.55 * is not supported by this collection
17.68/7.55 */
17.68/7.55 void clear();
17.68/7.55
17.68/7.55
17.68/7.55 // Comparison and hashing
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Compares the specified object with this collection for equality.
17.68/7.55 *
17.68/7.55 * While the Collection interface adds no stipulations to the
17.68/7.55 * general contract for the Object.equals, programmers who
17.68/7.55 * implement the Collection interface "directly" (in other words,
17.68/7.55 * create a class that is a Collection but is not a Set
17.68/7.55 * or a List) must exercise care if they choose to override the
17.68/7.55 * Object.equals. It is not necessary to do so, and the simplest
17.68/7.55 * course of action is to rely on Object's implementation, but
17.68/7.55 * the implementor may wish to implement a "value comparison" in place of
17.68/7.55 * the default "reference comparison." (The List and
17.68/7.55 * Set interfaces mandate such value comparisons.)
17.68/7.55 *
17.68/7.55 * The general contract for the Object.equals method states that
17.68/7.55 * equals must be symmetric (in other words, a.equals(b) if and
17.68/7.55 * only if b.equals(a)). The contracts for List.equals
17.68/7.55 * and Set.equals state that lists are only equal to other lists,
17.68/7.55 * and sets to other sets. Thus, a custom equals method for a
17.68/7.55 * collection class that implements neither the List nor
17.68/7.55 * Set interface must return false when this collection
17.68/7.55 * is compared to any list or set. (By the same logic, it is not possible
17.68/7.55 * to write a class that correctly implements both the Set and
17.68/7.55 * List interfaces.)
17.68/7.55 *
17.68/7.55 * @param o object to be compared for equality with this collection
17.68/7.55 * @return true if the specified object is equal to this
17.68/7.55 * collection
17.68/7.55 *
17.68/7.55 * @see Object#equals(Object)
17.68/7.55 * @see Set#equals(Object)
17.68/7.55 * @see List#equals(Object)
17.68/7.55 */
17.68/7.55 boolean equals(Object o);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns the hash code value for this collection. While the
17.68/7.55 * Collection interface adds no stipulations to the general
17.68/7.55 * contract for the Object.hashCode method, programmers should
17.68/7.55 * take note that any class that overrides the Object.equals
17.68/7.55 * method must also override the Object.hashCode method in order
17.68/7.55 * to satisfy the general contract for the Object.hashCodemethod.
17.68/7.55 * In particular, c1.equals(c2) implies that
17.68/7.55 * c1.hashCode()==c2.hashCode().
17.68/7.55 *
17.68/7.55 * @return the hash code value for this collection
17.68/7.55 *
17.68/7.55 * @see Object#hashCode()
17.68/7.55 * @see Object#equals(Object)
17.68/7.55 */
17.68/7.55 int hashCode();
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * This exception may be thrown by methods that have detected concurrent
17.68/7.55 * modification of an object when such modification is not permissible.
17.68/7.55 *
17.68/7.55 * For example, it is not generally permissible for one thread to modify a Collection
17.68/7.55 * while another thread is iterating over it. In general, the results of the
17.68/7.55 * iteration are undefined under these circumstances. Some Iterator
17.68/7.55 * implementations (including those of all the general purpose collection implementations
17.68/7.55 * provided by the JRE) may choose to throw this exception if this behavior is
17.68/7.55 * detected. Iterators that do this are known as fail-fast iterators,
17.68/7.55 * as they fail quickly and cleanly, rather that risking arbitrary,
17.68/7.55 * non-deterministic behavior at an undetermined time in the future.
17.68/7.55 *
17.68/7.55 * Note that this exception does not always indicate that an object has
17.68/7.55 * been concurrently modified by a different thread. If a single
17.68/7.55 * thread issues a sequence of method invocations that violates the
17.68/7.55 * contract of an object, the object may throw this exception. For
17.68/7.55 * example, if a thread modifies a collection directly while it is
17.68/7.55 * iterating over the collection with a fail-fast iterator, the iterator
17.68/7.55 * will throw this exception.
17.68/7.55 *
17.68/7.55 *
Note that fail-fast behavior cannot be guaranteed as it is, generally
17.68/7.55 * speaking, impossible to make any hard guarantees in the presence of
17.68/7.55 * unsynchronized concurrent modification. Fail-fast operations
17.68/7.55 * throw ConcurrentModificationException on a best-effort basis.
17.68/7.55 * Therefore, it would be wrong to write a program that depended on this
17.68/7.55 * exception for its correctness: ConcurrentModificationException
17.68/7.55 * should be used only to detect bugs.
17.68/7.55 *
17.68/7.55 * @author Josh Bloch
17.68/7.55 * @see Collection
17.68/7.55 * @see Iterator
17.68/7.55 * @see ListIterator
17.68/7.55 * @see Vector
17.68/7.55 * @see LinkedList
17.68/7.55 * @see HashSet
17.68/7.55 * @see Hashtable
17.68/7.55 * @see TreeMap
17.68/7.55 * @see AbstractList
17.68/7.55 * @since 1.2
17.68/7.55 */
17.68/7.55 public class ConcurrentModificationException extends RuntimeException {
17.68/7.55 /**
17.68/7.55 * Constructs a ConcurrentModificationException with no
17.68/7.55 * detail message.
17.68/7.55 */
17.68/7.55 public ConcurrentModificationException() {
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Constructs a ConcurrentModificationException with the
17.68/7.55 * specified detail message.
17.68/7.55 *
17.68/7.55 * @param message the detail message pertaining to this exception.
17.68/7.55 */
17.68/7.55 public ConcurrentModificationException(String message) {
17.68/7.55 super(message);
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 /*
17.68/7.55 * This file is available under and governed by the GNU General Public
17.68/7.55 * License version 2 only, as published by the Free Software Foundation.
17.68/7.55 * However, the following notice accompanied the original version of this
17.68/7.55 * file:
17.68/7.55 *
17.68/7.55 * Written by Doug Lea and Josh Bloch with assistance from members of
17.68/7.55 * JCP JSR-166 Expert Group and released to the public domain, as explained
17.68/7.55 * at http://creativecommons.org/licenses/publicdomain
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * A linear collection that supports element insertion and removal at
17.68/7.55 * both ends. The name deque is short for "double ended queue"
17.68/7.55 * and is usually pronounced "deck". Most Deque
17.68/7.55 * implementations place no fixed limits on the number of elements
17.68/7.55 * they may contain, but this interface supports capacity-restricted
17.68/7.55 * deques as well as those with no fixed size limit.
17.68/7.55 *
17.68/7.55 *
This interface defines methods to access the elements at both
17.68/7.55 * ends of the deque. Methods are provided to insert, remove, and
17.68/7.55 * examine the element. Each of these methods exists in two forms:
17.68/7.55 * one throws an exception if the operation fails, the other returns a
17.68/7.55 * special value (either null or false, depending on
17.68/7.55 * the operation). The latter form of the insert operation is
17.68/7.55 * designed specifically for use with capacity-restricted
17.68/7.55 * Deque implementations; in most implementations, insert
17.68/7.55 * operations cannot fail.
17.68/7.55 *
17.68/7.55 *
The twelve methods described above are summarized in the
17.68/7.55 * following table:
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * |
17.68/7.55 * First Element (Head) |
17.68/7.55 * Last Element (Tail) |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * |
17.68/7.55 * Throws exception |
17.68/7.55 * Special value |
17.68/7.55 * Throws exception |
17.68/7.55 * Special value |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * Insert |
17.68/7.55 * {@link #addFirst addFirst(e)} |
17.68/7.55 * {@link #offerFirst offerFirst(e)} |
17.68/7.55 * {@link #addLast addLast(e)} |
17.68/7.55 * {@link #offerLast offerLast(e)} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * Remove |
17.68/7.55 * {@link #removeFirst removeFirst()} |
17.68/7.55 * {@link #pollFirst pollFirst()} |
17.68/7.55 * {@link #removeLast removeLast()} |
17.68/7.55 * {@link #pollLast pollLast()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * Examine |
17.68/7.55 * {@link #getFirst getFirst()} |
17.68/7.55 * {@link #peekFirst peekFirst()} |
17.68/7.55 * {@link #getLast getLast()} |
17.68/7.55 * {@link #peekLast peekLast()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * This interface extends the {@link Queue} interface. When a deque is
17.68/7.55 * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
17.68/7.55 * added at the end of the deque and removed from the beginning. The methods
17.68/7.55 * inherited from the Queue interface are precisely equivalent to
17.68/7.55 * Deque methods as indicated in the following table:
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * Queue Method |
17.68/7.55 * Equivalent Deque Method |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link java.util.Queue#add add(e)} |
17.68/7.55 * {@link #addLast addLast(e)} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link java.util.Queue#offer offer(e)} |
17.68/7.55 * {@link #offerLast offerLast(e)} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link java.util.Queue#remove remove()} |
17.68/7.55 * {@link #removeFirst removeFirst()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link java.util.Queue#poll poll()} |
17.68/7.55 * {@link #pollFirst pollFirst()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link java.util.Queue#element element()} |
17.68/7.55 * {@link #getFirst getFirst()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link java.util.Queue#peek peek()} |
17.68/7.55 * {@link #peek peekFirst()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * Deques can also be used as LIFO (Last-In-First-Out) stacks. This
17.68/7.55 * interface should be used in preference to the legacy {@link Stack} class.
17.68/7.55 * When a deque is used as a stack, elements are pushed and popped from the
17.68/7.55 * beginning of the deque. Stack methods are precisely equivalent to
17.68/7.55 * Deque methods as indicated in the table below:
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * Stack Method |
17.68/7.55 * Equivalent Deque Method |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link #push push(e)} |
17.68/7.55 * {@link #addFirst addFirst(e)} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link #pop pop()} |
17.68/7.55 * {@link #removeFirst removeFirst()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * {@link #peek peek()} |
17.68/7.55 * {@link #peekFirst peekFirst()} |
17.68/7.55 *
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * Note that the {@link #peek peek} method works equally well when
17.68/7.55 * a deque is used as a queue or a stack; in either case, elements are
17.68/7.55 * drawn from the beginning of the deque.
17.68/7.55 *
17.68/7.55 *
This interface provides two methods to remove interior
17.68/7.55 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
17.68/7.55 * {@link #removeLastOccurrence removeLastOccurrence}.
17.68/7.55 *
17.68/7.55 *
Unlike the {@link List} interface, this interface does not
17.68/7.55 * provide support for indexed access to elements.
17.68/7.55 *
17.68/7.55 *
While Deque implementations are not strictly required
17.68/7.55 * to prohibit the insertion of null elements, they are strongly
17.68/7.55 * encouraged to do so. Users of any Deque implementations
17.68/7.55 * that do allow null elements are strongly encouraged not to
17.68/7.55 * take advantage of the ability to insert nulls. This is so because
17.68/7.55 * null is used as a special return value by various methods
17.68/7.55 * to indicated that the deque is empty.
17.68/7.55 *
17.68/7.55 *
Deque implementations generally do not define
17.68/7.55 * element-based versions of the equals and hashCode
17.68/7.55 * methods, but instead inherit the identity-based versions from class
17.68/7.55 * Object.
17.68/7.55 *
17.68/7.55 *
This interface is a member of the Java Collections
17.68/7.55 * Framework.
17.68/7.55 *
17.68/7.55 * @author Doug Lea
17.68/7.55 * @author Josh Bloch
17.68/7.55 * @since 1.6
17.68/7.55 * @param the type of elements held in this collection
17.68/7.55 */
17.68/7.55
17.68/7.55 public interface Deque extends Queue {
17.68/7.55 /**
17.68/7.55 * Inserts the specified element at the front of this deque if it is
17.68/7.55 * possible to do so immediately without violating capacity restrictions.
17.68/7.55 * When using a capacity-restricted deque, it is generally preferable to
17.68/7.55 * use method {@link #offerFirst}.
17.68/7.55 *
17.68/7.55 * @param e the element to add
17.68/7.55 * @throws IllegalStateException if the element cannot be added at this
17.68/7.55 * time due to capacity restrictions
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this deque
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the specified
17.68/7.55 * element prevents it from being added to this deque
17.68/7.55 */
17.68/7.55 void addFirst(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Inserts the specified element at the end of this deque if it is
17.68/7.55 * possible to do so immediately without violating capacity restrictions.
17.68/7.55 * When using a capacity-restricted deque, it is generally preferable to
17.68/7.55 * use method {@link #offerLast}.
17.68/7.55 *
17.68/7.55 * This method is equivalent to {@link #add}.
17.68/7.55 *
17.68/7.55 * @param e the element to add
17.68/7.55 * @throws IllegalStateException if the element cannot be added at this
17.68/7.55 * time due to capacity restrictions
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this deque
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the specified
17.68/7.55 * element prevents it from being added to this deque
17.68/7.55 */
17.68/7.55 void addLast(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Inserts the specified element at the front of this deque unless it would
17.68/7.55 * violate capacity restrictions. When using a capacity-restricted deque,
17.68/7.55 * this method is generally preferable to the {@link #addFirst} method,
17.68/7.55 * which can fail to insert an element only by throwing an exception.
17.68/7.55 *
17.68/7.55 * @param e the element to add
17.68/7.55 * @return true if the element was added to this deque, else
17.68/7.55 * false
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this deque
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the specified
17.68/7.55 * element prevents it from being added to this deque
17.68/7.55 */
17.68/7.55 boolean offerFirst(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Inserts the specified element at the end of this deque unless it would
17.68/7.55 * violate capacity restrictions. When using a capacity-restricted deque,
17.68/7.55 * this method is generally preferable to the {@link #addLast} method,
17.68/7.55 * which can fail to insert an element only by throwing an exception.
17.68/7.55 *
17.68/7.55 * @param e the element to add
17.68/7.55 * @return true if the element was added to this deque, else
17.68/7.55 * false
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this deque
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the specified
17.68/7.55 * element prevents it from being added to this deque
17.68/7.55 */
17.68/7.55 boolean offerLast(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves and removes the first element of this deque. This method
17.68/7.55 * differs from {@link #pollFirst pollFirst} only in that it throws an
17.68/7.55 * exception if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the head of this deque
17.68/7.55 * @throws NoSuchElementException if this deque is empty
17.68/7.55 */
17.68/7.55 E removeFirst();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves and removes the last element of this deque. This method
17.68/7.55 * differs from {@link #pollLast pollLast} only in that it throws an
17.68/7.55 * exception if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the tail of this deque
17.68/7.55 * @throws NoSuchElementException if this deque is empty
17.68/7.55 */
17.68/7.55 E removeLast();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves and removes the first element of this deque,
17.68/7.55 * or returns null if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the head of this deque, or null if this deque is empty
17.68/7.55 */
17.68/7.55 E pollFirst();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves and removes the last element of this deque,
17.68/7.55 * or returns null if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the tail of this deque, or null if this deque is empty
17.68/7.55 */
17.68/7.55 E pollLast();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves, but does not remove, the first element of this deque.
17.68/7.55 *
17.68/7.55 * This method differs from {@link #peekFirst peekFirst} only in that it
17.68/7.55 * throws an exception if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the head of this deque
17.68/7.55 * @throws NoSuchElementException if this deque is empty
17.68/7.55 */
17.68/7.55 E getFirst();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves, but does not remove, the last element of this deque.
17.68/7.55 * This method differs from {@link #peekLast peekLast} only in that it
17.68/7.55 * throws an exception if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the tail of this deque
17.68/7.55 * @throws NoSuchElementException if this deque is empty
17.68/7.55 */
17.68/7.55 E getLast();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves, but does not remove, the first element of this deque,
17.68/7.55 * or returns null if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the head of this deque, or null if this deque is empty
17.68/7.55 */
17.68/7.55 E peekFirst();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves, but does not remove, the last element of this deque,
17.68/7.55 * or returns null if this deque is empty.
17.68/7.55 *
17.68/7.55 * @return the tail of this deque, or null if this deque is empty
17.68/7.55 */
17.68/7.55 E peekLast();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes the first occurrence of the specified element from this deque.
17.68/7.55 * If the deque does not contain the element, it is unchanged.
17.68/7.55 * More formally, removes the first element e such that
17.68/7.55 * (o==null ? e==null : o.equals(e))
17.68/7.55 * (if such an element exists).
17.68/7.55 * Returns true if this deque contained the specified element
17.68/7.55 * (or equivalently, if this deque changed as a result of the call).
17.68/7.55 *
17.68/7.55 * @param o element to be removed from this deque, if present
17.68/7.55 * @return true if an element was removed as a result of this call
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * is incompatible with this deque (optional)
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements (optional)
17.68/7.55 */
17.68/7.55 boolean removeFirstOccurrence(Object o);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes the last occurrence of the specified element from this deque.
17.68/7.55 * If the deque does not contain the element, it is unchanged.
17.68/7.55 * More formally, removes the last element e such that
17.68/7.55 * (o==null ? e==null : o.equals(e))
17.68/7.55 * (if such an element exists).
17.68/7.55 * Returns true if this deque contained the specified element
17.68/7.55 * (or equivalently, if this deque changed as a result of the call).
17.68/7.55 *
17.68/7.55 * @param o element to be removed from this deque, if present
17.68/7.55 * @return true if an element was removed as a result of this call
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * is incompatible with this deque (optional)
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements (optional)
17.68/7.55 */
17.68/7.55 boolean removeLastOccurrence(Object o);
17.68/7.55
17.68/7.55 // *** Queue methods ***
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Inserts the specified element into the queue represented by this deque
17.68/7.55 * (in other words, at the tail of this deque) if it is possible to do so
17.68/7.55 * immediately without violating capacity restrictions, returning
17.68/7.55 * true upon success and throwing an
17.68/7.55 * IllegalStateException if no space is currently available.
17.68/7.55 * When using a capacity-restricted deque, it is generally preferable to
17.68/7.55 * use {@link #offer(Object) offer}.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #addLast}.
17.68/7.55 *
17.68/7.55 * @param e the element to add
17.68/7.55 * @return true (as specified by {@link Collection#add})
17.68/7.55 * @throws IllegalStateException if the element cannot be added at this
17.68/7.55 * time due to capacity restrictions
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this deque
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the specified
17.68/7.55 * element prevents it from being added to this deque
17.68/7.55 */
17.68/7.55 boolean add(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Inserts the specified element into the queue represented by this deque
17.68/7.55 * (in other words, at the tail of this deque) if it is possible to do so
17.68/7.55 * immediately without violating capacity restrictions, returning
17.68/7.55 * true upon success and false if no space is currently
17.68/7.55 * available. When using a capacity-restricted deque, this method is
17.68/7.55 * generally preferable to the {@link #add} method, which can fail to
17.68/7.55 * insert an element only by throwing an exception.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #offerLast}.
17.68/7.55 *
17.68/7.55 * @param e the element to add
17.68/7.55 * @return true if the element was added to this deque, else
17.68/7.55 * false
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this deque
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the specified
17.68/7.55 * element prevents it from being added to this deque
17.68/7.55 */
17.68/7.55 boolean offer(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves and removes the head of the queue represented by this deque
17.68/7.55 * (in other words, the first element of this deque).
17.68/7.55 * This method differs from {@link #poll poll} only in that it throws an
17.68/7.55 * exception if this deque is empty.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #removeFirst()}.
17.68/7.55 *
17.68/7.55 * @return the head of the queue represented by this deque
17.68/7.55 * @throws NoSuchElementException if this deque is empty
17.68/7.55 */
17.68/7.55 E remove();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves and removes the head of the queue represented by this deque
17.68/7.55 * (in other words, the first element of this deque), or returns
17.68/7.55 * null if this deque is empty.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #pollFirst()}.
17.68/7.55 *
17.68/7.55 * @return the first element of this deque, or null if
17.68/7.55 * this deque is empty
17.68/7.55 */
17.68/7.55 E poll();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves, but does not remove, the head of the queue represented by
17.68/7.55 * this deque (in other words, the first element of this deque).
17.68/7.55 * This method differs from {@link #peek peek} only in that it throws an
17.68/7.55 * exception if this deque is empty.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #getFirst()}.
17.68/7.55 *
17.68/7.55 * @return the head of the queue represented by this deque
17.68/7.55 * @throws NoSuchElementException if this deque is empty
17.68/7.55 */
17.68/7.55 E element();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Retrieves, but does not remove, the head of the queue represented by
17.68/7.55 * this deque (in other words, the first element of this deque), or
17.68/7.55 * returns null if this deque is empty.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #peekFirst()}.
17.68/7.55 *
17.68/7.55 * @return the head of the queue represented by this deque, or
17.68/7.55 * null if this deque is empty
17.68/7.55 */
17.68/7.55 E peek();
17.68/7.55
17.68/7.55
17.68/7.55 // *** Stack methods ***
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Pushes an element onto the stack represented by this deque (in other
17.68/7.55 * words, at the head of this deque) if it is possible to do so
17.68/7.55 * immediately without violating capacity restrictions, returning
17.68/7.55 * true upon success and throwing an
17.68/7.55 * IllegalStateException if no space is currently available.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #addFirst}.
17.68/7.55 *
17.68/7.55 * @param e the element to push
17.68/7.55 * @throws IllegalStateException if the element cannot be added at this
17.68/7.55 * time due to capacity restrictions
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * prevents it from being added to this deque
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements
17.68/7.55 * @throws IllegalArgumentException if some property of the specified
17.68/7.55 * element prevents it from being added to this deque
17.68/7.55 */
17.68/7.55 void push(E e);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Pops an element from the stack represented by this deque. In other
17.68/7.55 * words, removes and returns the first element of this deque.
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #removeFirst()}.
17.68/7.55 *
17.68/7.55 * @return the element at the front of this deque (which is the top
17.68/7.55 * of the stack represented by this deque)
17.68/7.55 * @throws NoSuchElementException if this deque is empty
17.68/7.55 */
17.68/7.55 E pop();
17.68/7.55
17.68/7.55
17.68/7.55 // *** Collection methods ***
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes the first occurrence of the specified element from this deque.
17.68/7.55 * If the deque does not contain the element, it is unchanged.
17.68/7.55 * More formally, removes the first element e such that
17.68/7.55 * (o==null ? e==null : o.equals(e))
17.68/7.55 * (if such an element exists).
17.68/7.55 * Returns true if this deque contained the specified element
17.68/7.55 * (or equivalently, if this deque changed as a result of the call).
17.68/7.55 *
17.68/7.55 *
This method is equivalent to {@link #removeFirstOccurrence}.
17.68/7.55 *
17.68/7.55 * @param o element to be removed from this deque, if present
17.68/7.55 * @return true if an element was removed as a result of this call
17.68/7.55 * @throws ClassCastException if the class of the specified element
17.68/7.55 * is incompatible with this deque (optional)
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements (optional)
17.68/7.55 */
17.68/7.55 boolean remove(Object o);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns true if this deque contains the specified element.
17.68/7.55 * More formally, returns true if and only if this deque contains
17.68/7.55 * at least one element e such that
17.68/7.55 * (o==null ? e==null : o.equals(e)).
17.68/7.55 *
17.68/7.55 * @param o element whose presence in this deque is to be tested
17.68/7.55 * @return true if this deque contains the specified element
17.68/7.55 * @throws ClassCastException if the type of the specified element
17.68/7.55 * is incompatible with this deque (optional)
17.68/7.55 * @throws NullPointerException if the specified element is null and this
17.68/7.55 * deque does not permit null elements (optional)
17.68/7.55 */
17.68/7.55 boolean contains(Object o);
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns the number of elements in this deque.
17.68/7.55 *
17.68/7.55 * @return the number of elements in this deque
17.68/7.55 */
17.68/7.55 public int size();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns an iterator over the elements in this deque in proper sequence.
17.68/7.55 * The elements will be returned in order from first (head) to last (tail).
17.68/7.55 *
17.68/7.55 * @return an iterator over the elements in this deque in proper sequence
17.68/7.55 */
17.68/7.55 Iterator iterator();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns an iterator over the elements in this deque in reverse
17.68/7.55 * sequential order. The elements will be returned in order from
17.68/7.55 * last (tail) to first (head).
17.68/7.55 *
17.68/7.55 * @return an iterator over the elements in this deque in reverse
17.68/7.55 * sequence
17.68/7.55 */
17.68/7.55 Iterator descendingIterator();
17.68/7.55
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Thrown to indicate that a method has been passed an illegal or
17.68/7.55 * inappropriate argument.
17.68/7.55 *
17.68/7.55 * @author unascribed
17.68/7.55 * @see java.lang.Thread#setPriority(int)
17.68/7.55 * @since JDK1.0
17.68/7.55 */
17.68/7.55 public
17.68/7.55 class IllegalArgumentException extends RuntimeException {
17.68/7.55 /**
17.68/7.55 * Constructs an IllegalArgumentException
with no
17.68/7.55 * detail message.
17.68/7.55 */
17.68/7.55 public IllegalArgumentException() {
17.68/7.55 super();
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Constructs an IllegalArgumentException
with the
17.68/7.55 * specified detail message.
17.68/7.55 *
17.68/7.55 * @param s the detail message.
17.68/7.55 */
17.68/7.55 public IllegalArgumentException(String s) {
17.68/7.55 super(s);
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Constructs a new exception with the specified detail message and
17.68/7.55 * cause.
17.68/7.55 *
17.68/7.55 * Note that the detail message associated with cause
is
17.68/7.55 * not automatically incorporated in this exception's detail
17.68/7.55 * message.
17.68/7.55 *
17.68/7.55 * @param message the detail message (which is saved for later retrieval
17.68/7.55 * by the {@link Throwable#getMessage()} method).
17.68/7.55 * @param cause the cause (which is saved for later retrieval by the
17.68/7.55 * {@link Throwable#getCause()} method). (A null value
17.68/7.55 * is permitted, and indicates that the cause is nonexistent or
17.68/7.55 * unknown.)
17.68/7.55 * @since 1.5
17.68/7.55 */
17.68/7.55 public IllegalArgumentException(String message, Throwable cause) {
17.68/7.55 super(message, cause);
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Constructs a new exception with the specified cause and a detail
17.68/7.55 * message of (cause==null ? null : cause.toString()) (which
17.68/7.55 * typically contains the class and detail message of cause).
17.68/7.55 * This constructor is useful for exceptions that are little more than
17.68/7.55 * wrappers for other throwables (for example, {@link
17.68/7.55 * java.security.PrivilegedActionException}).
17.68/7.55 *
17.68/7.55 * @param cause the cause (which is saved for later retrieval by the
17.68/7.55 * {@link Throwable#getCause()} method). (A null value is
17.68/7.55 * permitted, and indicates that the cause is nonexistent or
17.68/7.55 * unknown.)
17.68/7.55 * @since 1.5
17.68/7.55 */
17.68/7.55 public IllegalArgumentException(Throwable cause) {
17.68/7.55 super(cause);
17.68/7.55 }
17.68/7.55
17.68/7.55 private static final long serialVersionUID = -5365630128856068164L;
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Signals that a method has been invoked at an illegal or
17.68/7.55 * inappropriate time. In other words, the Java environment or
17.68/7.55 * Java application is not in an appropriate state for the requested
17.68/7.55 * operation.
17.68/7.55 *
17.68/7.55 * @author Jonni Kanerva
17.68/7.55 * @since JDK1.1
17.68/7.55 */
17.68/7.55 public
17.68/7.55 class IllegalStateException extends RuntimeException {
17.68/7.55 /**
17.68/7.55 * Constructs an IllegalStateException with no detail message.
17.68/7.55 * A detail message is a String that describes this particular exception.
17.68/7.55 */
17.68/7.55 public IllegalStateException() {
17.68/7.55 super();
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Constructs an IllegalStateException with the specified detail
17.68/7.55 * message. A detail message is a String that describes this particular
17.68/7.55 * exception.
17.68/7.55 *
17.68/7.55 * @param s the String that contains a detailed message
17.68/7.55 */
17.68/7.55 public IllegalStateException(String s) {
17.68/7.55 super(s);
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Constructs a new exception with the specified detail message and
17.68/7.55 * cause.
17.68/7.55 *
17.68/7.55 *
Note that the detail message associated with cause
is
17.68/7.55 * not automatically incorporated in this exception's detail
17.68/7.55 * message.
17.68/7.55 *
17.68/7.55 * @param message the detail message (which is saved for later retrieval
17.68/7.55 * by the {@link Throwable#getMessage()} method).
17.68/7.55 * @param cause the cause (which is saved for later retrieval by the
17.68/7.55 * {@link Throwable#getCause()} method). (A null value
17.68/7.55 * is permitted, and indicates that the cause is nonexistent or
17.68/7.55 * unknown.)
17.68/7.55 * @since 1.5
17.68/7.55 */
17.68/7.55 public IllegalStateException(String message, Throwable cause) {
17.68/7.55 super(message, cause);
17.68/7.55 }
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Constructs a new exception with the specified cause and a detail
17.68/7.55 * message of (cause==null ? null : cause.toString()) (which
17.68/7.55 * typically contains the class and detail message of cause).
17.68/7.55 * This constructor is useful for exceptions that are little more than
17.68/7.55 * wrappers for other throwables (for example, {@link
17.68/7.55 * java.security.PrivilegedActionException}).
17.68/7.55 *
17.68/7.55 * @param cause the cause (which is saved for later retrieval by the
17.68/7.55 * {@link Throwable#getCause()} method). (A null value is
17.68/7.55 * permitted, and indicates that the cause is nonexistent or
17.68/7.55 * unknown.)
17.68/7.55 * @since 1.5
17.68/7.55 */
17.68/7.55 public IllegalStateException(Throwable cause) {
17.68/7.55 super(cause);
17.68/7.55 }
17.68/7.55
17.68/7.55 static final long serialVersionUID = -1848914673093119416L;
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * An iterator over a collection. {@code Iterator} takes the place of
17.68/7.55 * {@link Enumeration} in the Java Collections Framework. Iterators
17.68/7.55 * differ from enumerations in two ways:
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * - Iterators allow the caller to remove elements from the
17.68/7.55 * underlying collection during the iteration with well-defined
17.68/7.55 * semantics.
17.68/7.55 *
- Method names have been improved.
17.68/7.55 *
17.68/7.55 *
17.68/7.55 * This interface is a member of the
17.68/7.55 *
17.68/7.55 * Java Collections Framework.
17.68/7.55 *
17.68/7.55 * @author Josh Bloch
17.68/7.55 * @see Collection
17.68/7.55 * @see ListIterator
17.68/7.55 * @see Iterable
17.68/7.55 * @since 1.2
17.68/7.55 */
17.68/7.55 public interface Iterator {
17.68/7.55 /**
17.68/7.55 * Returns {@code true} if the iteration has more elements.
17.68/7.55 * (In other words, returns {@code true} if {@link #next} would
17.68/7.55 * return an element rather than throwing an exception.)
17.68/7.55 *
17.68/7.55 * @return {@code true} if the iteration has more elements
17.68/7.55 */
17.68/7.55 boolean hasNext();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Returns the next element in the iteration.
17.68/7.55 *
17.68/7.55 * @return the next element in the iteration
17.68/7.55 * @throws NoSuchElementException if the iteration has no more elements
17.68/7.55 */
17.68/7.55 E next();
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Removes from the underlying collection the last element returned
17.68/7.55 * by this iterator (optional operation). This method can be called
17.68/7.55 * only once per call to {@link #next}. The behavior of an iterator
17.68/7.55 * is unspecified if the underlying collection is modified while the
17.68/7.55 * iteration is in progress in any way other than by calling this
17.68/7.55 * method.
17.68/7.55 *
17.68/7.55 * @throws UnsupportedOperationException if the {@code remove}
17.68/7.55 * operation is not supported by this iterator
17.68/7.55 *
17.68/7.55 * @throws IllegalStateException if the {@code next} method has not
17.68/7.55 * yet been called, or the {@code remove} method has already
17.68/7.55 * been called after the last call to the {@code next}
17.68/7.55 * method
17.68/7.55 */
17.68/7.55 void remove();
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 public class juLinkedListCreatePeekLast {
17.68/7.55 public static void main(String[] args) {
17.68/7.55 Random.args = args;
17.68/7.55
17.68/7.55 LinkedList l = createList(Random.random());
17.68/7.55 l.peekLast();
17.68/7.55 }
17.68/7.55
17.68/7.55 public static LinkedList createList(int n) {
17.68/7.55 LinkedList l = new LinkedList();
17.68/7.55 while (n > 0) {
17.68/7.55 l.addLast(new Content(Random.random()));
17.68/7.55 n--;
17.68/7.55 }
17.68/7.55 return l;
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55 final class Content {
17.68/7.55 int val;
17.68/7.55
17.68/7.55 public Content(int v) {
17.68/7.55 this.val = v;
17.68/7.55 }
17.68/7.55
17.68/7.55 public int hashCode() {
17.68/7.55 return val^31;
17.68/7.55 }
17.68/7.55
17.68/7.55 public boolean equals(Object o) {
17.68/7.55 if (o instanceof Content) {
17.68/7.55 return this.val == ((Content) o).val;
17.68/7.55 }
17.68/7.55 return false;
17.68/7.55 }
17.68/7.55 }
17.68/7.55
17.68/7.55
17.68/7.55 /*
17.68/7.55 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
17.68/7.55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17.68/7.55 *
17.68/7.55 * This code is free software; you can redistribute it and/or modify it
17.68/7.55 * under the terms of the GNU General Public License version 2 only, as
17.68/7.55 * published by the Free Software Foundation. Sun designates this
17.68/7.55 * particular file as subject to the "Classpath" exception as provided
17.68/7.55 * by Sun in the LICENSE file that accompanied this code.
17.68/7.55 *
17.68/7.55 * This code is distributed in the hope that it will be useful, but WITHOUT
17.68/7.55 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17.68/7.55 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17.68/7.55 * version 2 for more details (a copy is included in the LICENSE file that
17.68/7.55 * accompanied this code).
17.68/7.55 *
17.68/7.55 * You should have received a copy of the GNU General Public License version
17.68/7.55 * 2 along with this work; if not, write to the Free Software Foundation,
17.68/7.55 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17.68/7.55 *
17.68/7.55 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
17.68/7.55 * CA 95054 USA or visit www.sun.com if you need additional information or
17.68/7.55 * have any questions.
17.68/7.55 */
17.68/7.55
17.68/7.55 package javaUtilEx;
17.68/7.55
17.68/7.55 /**
17.68/7.55 * Linked list implementation of the List interface. Implements all
17.68/7.55 * optional list operations, and permits all elements (including
17.68/7.56 * null). In addition to implementing the List interface,
17.68/7.56 * the LinkedList class provides uniformly named methods to
17.68/7.56 * get, remove and insert an element at the
17.68/7.56 * beginning and end of the list. These operations allow linked lists to be
17.68/7.56 * used as a stack, {@linkplain Queue queue}, or {@linkplain Deque
17.68/7.56 * double-ended queue}.
17.68/7.56 *
17.68/7.56 * The class implements the Deque interface, providing
17.68/7.56 * first-in-first-out queue operations for add,
17.68/7.56 * poll, along with other stack and deque operations.
17.68/7.56 *
17.68/7.56 * All of the operations perform as could be expected for a doubly-linked
17.68/7.56 * list. Operations that index into the list will traverse the list from
17.68/7.56 * the beginning or the end, whichever is closer to the specified index.
17.68/7.56 *
17.68/7.56 *
Note that this implementation is not synchronized.
17.68/7.56 * If multiple threads access a linked list concurrently, and at least
17.68/7.56 * one of the threads modifies the list structurally, it must be
17.68/7.56 * synchronized externally. (A structural modification is any operation
17.68/7.56 * that adds or deletes one or more elements; merely setting the value of
17.68/7.56 * an element is not a structural modification.) This is typically
17.68/7.56 * accomplished by synchronizing on some object that naturally
17.68/7.56 * encapsulates the list.
17.68/7.56 *
17.68/7.56 * If no such object exists, the list should be "wrapped" using the
17.68/7.56 * {@link Collections#synchronizedList Collections.synchronizedList}
17.68/7.56 * method. This is best done at creation time, to prevent accidental
17.68/7.56 * unsynchronized access to the list:
17.68/7.56 * List list = Collections.synchronizedList(new LinkedList(...));
17.68/7.56 *
17.68/7.56 * The iterators returned by this class's iterator and
17.68/7.56 * listIterator methods are fail-fast: if the list is
17.68/7.56 * structurally modified at any time after the iterator is created, in
17.68/7.56 * any way except through the Iterator's own remove or
17.68/7.56 * add methods, the iterator will throw a {@link
17.68/7.56 * ConcurrentModificationException}. Thus, in the face of concurrent
17.68/7.56 * modification, the iterator fails quickly and cleanly, rather than
17.68/7.56 * risking arbitrary, non-deterministic behavior at an undetermined
17.68/7.56 * time in the future.
17.68/7.56 *
17.68/7.56 *
Note that the fail-fast behavior of an iterator cannot be guaranteed
17.68/7.56 * as it is, generally speaking, impossible to make any hard guarantees in the
17.68/7.56 * presence of unsynchronized concurrent modification. Fail-fast iterators
17.68/7.56 * throw ConcurrentModificationException on a best-effort basis.
17.68/7.56 * Therefore, it would be wrong to write a program that depended on this
17.68/7.56 * exception for its correctness: the fail-fast behavior of iterators
17.68/7.56 * should be used only to detect bugs.
17.68/7.56 *
17.68/7.56 *
This class is a member of the
17.68/7.56 *
17.68/7.56 * Java Collections Framework.
17.68/7.56 *
17.68/7.56 * @author Josh Bloch
17.68/7.56 * @see List
17.68/7.56 * @see ArrayList
17.68/7.56 * @see Vector
17.68/7.56 * @since 1.2
17.68/7.56 * @param the type of elements held in this collection
17.68/7.56 */
17.68/7.56
17.68/7.56 public class LinkedList
17.68/7.56 extends AbstractSequentialList
17.68/7.56 implements List, Deque
17.68/7.56 {
17.68/7.56 private transient Entry header = new Entry(null, null, null);
17.68/7.56 private transient int size = 0;
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Constructs an empty list.
17.68/7.56 */
17.68/7.56 public LinkedList() {
17.68/7.56 header.next = header.previous = header;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Constructs a list containing the elements of the specified
17.68/7.56 * collection, in the order they are returned by the collection's
17.68/7.56 * iterator.
17.68/7.56 *
17.68/7.56 * @param c the collection whose elements are to be placed into this list
17.68/7.56 * @throws NullPointerException if the specified collection is null
17.68/7.56 */
17.68/7.56 public LinkedList(Collection extends E> c) {
17.68/7.56 this();
17.68/7.56 addAll(c);
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns the first element in this list.
17.68/7.56 *
17.68/7.56 * @return the first element in this list
17.68/7.56 * @throws NoSuchElementException if this list is empty
17.68/7.56 */
17.68/7.56 public E getFirst() {
17.68/7.56 if (size==0)
17.68/7.56 throw new NoSuchElementException();
17.68/7.56
17.68/7.56 return header.next.element;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns the last element in this list.
17.68/7.56 *
17.68/7.56 * @return the last element in this list
17.68/7.56 * @throws NoSuchElementException if this list is empty
17.68/7.56 */
17.68/7.56 public E getLast() {
17.68/7.56 if (size==0)
17.68/7.56 throw new NoSuchElementException();
17.68/7.56
17.68/7.56 return header.previous.element;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Removes and returns the first element from this list.
17.68/7.56 *
17.68/7.56 * @return the first element from this list
17.68/7.56 * @throws NoSuchElementException if this list is empty
17.68/7.56 */
17.68/7.56 public E removeFirst() {
17.68/7.56 return remove(header.next);
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Removes and returns the last element from this list.
17.68/7.56 *
17.68/7.56 * @return the last element from this list
17.68/7.56 * @throws NoSuchElementException if this list is empty
17.68/7.56 */
17.68/7.56 public E removeLast() {
17.68/7.56 return remove(header.previous);
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Inserts the specified element at the beginning of this list.
17.68/7.56 *
17.68/7.56 * @param e the element to add
17.68/7.56 */
17.68/7.56 public void addFirst(E e) {
17.68/7.56 addBefore(e, header.next);
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Appends the specified element to the end of this list.
17.68/7.56 *
17.68/7.56 * This method is equivalent to {@link #add}.
17.68/7.56 *
17.68/7.56 * @param e the element to add
17.68/7.56 */
17.68/7.56 public void addLast(E e) {
17.68/7.56 addBefore(e, header);
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns true if this list contains the specified element.
17.68/7.56 * More formally, returns true if and only if this list contains
17.68/7.56 * at least one element e such that
17.68/7.56 * (o==null ? e==null : o.equals(e)).
17.68/7.56 *
17.68/7.56 * @param o element whose presence in this list is to be tested
17.68/7.56 * @return true if this list contains the specified element
17.68/7.56 */
17.68/7.56 public boolean contains(Object o) {
17.68/7.56 return indexOf(o) != -1;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns the number of elements in this list.
17.68/7.56 *
17.68/7.56 * @return the number of elements in this list
17.68/7.56 */
17.68/7.56 public int size() {
17.68/7.56 return size;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Appends the specified element to the end of this list.
17.68/7.56 *
17.68/7.56 *
This method is equivalent to {@link #addLast}.
17.68/7.56 *
17.68/7.56 * @param e element to be appended to this list
17.68/7.56 * @return true (as specified by {@link Collection#add})
17.68/7.56 */
17.68/7.56 public boolean add(E e) {
17.68/7.56 addBefore(e, header);
17.68/7.56 return true;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Removes the first occurrence of the specified element from this list,
17.68/7.56 * if it is present. If this list does not contain the element, it is
17.68/7.56 * unchanged. More formally, removes the element with the lowest index
17.68/7.56 * i such that
17.68/7.56 * (o==null ? get(i)==null : o.equals(get(i)))
17.68/7.56 * (if such an element exists). Returns true if this list
17.68/7.56 * contained the specified element (or equivalently, if this list
17.68/7.56 * changed as a result of the call).
17.68/7.56 *
17.68/7.56 * @param o element to be removed from this list, if present
17.68/7.56 * @return true if this list contained the specified element
17.68/7.56 */
17.68/7.56 public boolean remove(Object o) {
17.68/7.56 if (o==null) {
17.68/7.56 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.56 if (e.element==null) {
17.68/7.56 remove(e);
17.68/7.56 return true;
17.68/7.56 }
17.68/7.56 }
17.68/7.56 } else {
17.68/7.56 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.56 if (o.equals(e.element)) {
17.68/7.56 remove(e);
17.68/7.56 return true;
17.68/7.56 }
17.68/7.56 }
17.68/7.56 }
17.68/7.56 return false;
17.68/7.56 }
17.68/7.56 /**
17.68/7.56 * Removes all of the elements from this list.
17.68/7.56 */
17.68/7.56 public void clear() {
17.68/7.56 Entry e = header.next;
17.68/7.56 while (e != header) {
17.68/7.56 Entry next = e.next;
17.68/7.56 e.next = e.previous = null;
17.68/7.56 e.element = null;
17.68/7.56 e = next;
17.68/7.56 }
17.68/7.56 header.next = header.previous = header;
17.68/7.56 size = 0;
17.68/7.56 modCount++;
17.68/7.56 }
17.68/7.56
17.68/7.56
17.68/7.56 // Positional Access Operations
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns the element at the specified position in this list.
17.68/7.56 *
17.68/7.56 * @param index index of the element to return
17.68/7.56 * @return the element at the specified position in this list
17.68/7.56 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.56 */
17.68/7.56 public E get(int index) {
17.68/7.56 return entry(index).element;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Replaces the element at the specified position in this list with the
17.68/7.56 * specified element.
17.68/7.56 *
17.68/7.56 * @param index index of the element to replace
17.68/7.56 * @param element element to be stored at the specified position
17.68/7.56 * @return the element previously at the specified position
17.68/7.56 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.56 */
17.68/7.56 public E set(int index, E element) {
17.68/7.56 Entry e = entry(index);
17.68/7.56 E oldVal = e.element;
17.68/7.56 e.element = element;
17.68/7.56 return oldVal;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Inserts the specified element at the specified position in this list.
17.68/7.56 * Shifts the element currently at that position (if any) and any
17.68/7.56 * subsequent elements to the right (adds one to their indices).
17.68/7.56 *
17.68/7.56 * @param index index at which the specified element is to be inserted
17.68/7.56 * @param element element to be inserted
17.68/7.56 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.56 */
17.68/7.56 public void add(int index, E element) {
17.68/7.56 addBefore(element, (index==size ? header : entry(index)));
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Removes the element at the specified position in this list. Shifts any
17.68/7.56 * subsequent elements to the left (subtracts one from their indices).
17.68/7.56 * Returns the element that was removed from the list.
17.68/7.56 *
17.68/7.56 * @param index the index of the element to be removed
17.68/7.56 * @return the element previously at the specified position
17.68/7.56 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.56 */
17.68/7.56 public E remove(int index) {
17.68/7.56 return remove(entry(index));
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns the indexed entry.
17.68/7.56 */
17.68/7.56 private Entry entry(int index) {
17.68/7.56 if (index < 0 || index >= size)
17.68/7.56 throw new IndexOutOfBoundsException();
17.68/7.56 Entry e = header;
17.68/7.56 if (index < (size >> 1)) {
17.68/7.56 for (int i = 0; i <= index; i++)
17.68/7.56 e = e.next;
17.68/7.56 } else {
17.68/7.56 for (int i = size; i > index; i--)
17.68/7.56 e = e.previous;
17.68/7.56 }
17.68/7.56 return e;
17.68/7.56 }
17.68/7.56
17.68/7.56
17.68/7.56 // Search Operations
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns the index of the first occurrence of the specified element
17.68/7.56 * in this list, or -1 if this list does not contain the element.
17.68/7.56 * More formally, returns the lowest index i such that
17.68/7.56 * (o==null ? get(i)==null : o.equals(get(i))),
17.68/7.56 * or -1 if there is no such index.
17.68/7.56 *
17.68/7.56 * @param o element to search for
17.68/7.56 * @return the index of the first occurrence of the specified element in
17.68/7.56 * this list, or -1 if this list does not contain the element
17.68/7.56 */
17.68/7.56 public int indexOf(Object o) {
17.68/7.56 int index = 0;
17.68/7.56 if (o==null) {
17.68/7.56 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.56 if (e.element==null)
17.68/7.56 return index;
17.68/7.56 index++;
17.68/7.56 }
17.68/7.56 } else {
17.68/7.56 for (Entry e = header.next; e != header; e = e.next) {
17.68/7.56 if (o.equals(e.element))
17.68/7.56 return index;
17.68/7.56 index++;
17.68/7.56 }
17.68/7.56 }
17.68/7.56 return -1;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns the index of the last occurrence of the specified element
17.68/7.56 * in this list, or -1 if this list does not contain the element.
17.68/7.56 * More formally, returns the highest index i such that
17.68/7.56 * (o==null ? get(i)==null : o.equals(get(i))),
17.68/7.56 * or -1 if there is no such index.
17.68/7.56 *
17.68/7.56 * @param o element to search for
17.68/7.56 * @return the index of the last occurrence of the specified element in
17.68/7.56 * this list, or -1 if this list does not contain the element
17.68/7.56 */
17.68/7.56 public int lastIndexOf(Object o) {
17.68/7.56 int index = size;
17.68/7.56 if (o==null) {
17.68/7.56 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.56 index--;
17.68/7.56 if (e.element==null)
17.68/7.56 return index;
17.68/7.56 }
17.68/7.56 } else {
17.68/7.56 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.56 index--;
17.68/7.56 if (o.equals(e.element))
17.68/7.56 return index;
17.68/7.56 }
17.68/7.56 }
17.68/7.56 return -1;
17.68/7.56 }
17.68/7.56
17.68/7.56 // Queue operations.
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves, but does not remove, the head (first element) of this list.
17.68/7.56 * @return the head of this list, or null if this list is empty
17.68/7.56 * @since 1.5
17.68/7.56 */
17.68/7.56 public E peek() {
17.68/7.56 if (size==0)
17.68/7.56 return null;
17.68/7.56 return getFirst();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves, but does not remove, the head (first element) of this list.
17.68/7.56 * @return the head of this list
17.68/7.56 * @throws NoSuchElementException if this list is empty
17.68/7.56 * @since 1.5
17.68/7.56 */
17.68/7.56 public E element() {
17.68/7.56 return getFirst();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves and removes the head (first element) of this list
17.68/7.56 * @return the head of this list, or null if this list is empty
17.68/7.56 * @since 1.5
17.68/7.56 */
17.68/7.56 public E poll() {
17.68/7.56 if (size==0)
17.68/7.56 return null;
17.68/7.56 return removeFirst();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves and removes the head (first element) of this list.
17.68/7.56 *
17.68/7.56 * @return the head of this list
17.68/7.56 * @throws NoSuchElementException if this list is empty
17.68/7.56 * @since 1.5
17.68/7.56 */
17.68/7.56 public E remove() {
17.68/7.56 return removeFirst();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Adds the specified element as the tail (last element) of this list.
17.68/7.56 *
17.68/7.56 * @param e the element to add
17.68/7.56 * @return true (as specified by {@link Queue#offer})
17.68/7.56 * @since 1.5
17.68/7.56 */
17.68/7.56 public boolean offer(E e) {
17.68/7.56 return add(e);
17.68/7.56 }
17.68/7.56
17.68/7.56 // Deque operations
17.68/7.56 /**
17.68/7.56 * Inserts the specified element at the front of this list.
17.68/7.56 *
17.68/7.56 * @param e the element to insert
17.68/7.56 * @return true (as specified by {@link Deque#offerFirst})
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public boolean offerFirst(E e) {
17.68/7.56 addFirst(e);
17.68/7.56 return true;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Inserts the specified element at the end of this list.
17.68/7.56 *
17.68/7.56 * @param e the element to insert
17.68/7.56 * @return true (as specified by {@link Deque#offerLast})
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public boolean offerLast(E e) {
17.68/7.56 addLast(e);
17.68/7.56 return true;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves, but does not remove, the first element of this list,
17.68/7.56 * or returns null if this list is empty.
17.68/7.56 *
17.68/7.56 * @return the first element of this list, or null
17.68/7.56 * if this list is empty
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public E peekFirst() {
17.68/7.56 if (size==0)
17.68/7.56 return null;
17.68/7.56 return getFirst();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves, but does not remove, the last element of this list,
17.68/7.56 * or returns null if this list is empty.
17.68/7.56 *
17.68/7.56 * @return the last element of this list, or null
17.68/7.56 * if this list is empty
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public E peekLast() {
17.68/7.56 if (size==0)
17.68/7.56 return null;
17.68/7.56 return getLast();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves and removes the first element of this list,
17.68/7.56 * or returns null if this list is empty.
17.68/7.56 *
17.68/7.56 * @return the first element of this list, or null if
17.68/7.56 * this list is empty
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public E pollFirst() {
17.68/7.56 if (size==0)
17.68/7.56 return null;
17.68/7.56 return removeFirst();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Retrieves and removes the last element of this list,
17.68/7.56 * or returns null if this list is empty.
17.68/7.56 *
17.68/7.56 * @return the last element of this list, or null if
17.68/7.56 * this list is empty
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public E pollLast() {
17.68/7.56 if (size==0)
17.68/7.56 return null;
17.68/7.56 return removeLast();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Pushes an element onto the stack represented by this list. In other
17.68/7.56 * words, inserts the element at the front of this list.
17.68/7.56 *
17.68/7.56 * This method is equivalent to {@link #addFirst}.
17.68/7.56 *
17.68/7.56 * @param e the element to push
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public void push(E e) {
17.68/7.56 addFirst(e);
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Pops an element from the stack represented by this list. In other
17.68/7.56 * words, removes and returns the first element of this list.
17.68/7.56 *
17.68/7.56 *
This method is equivalent to {@link #removeFirst()}.
17.68/7.56 *
17.68/7.56 * @return the element at the front of this list (which is the top
17.68/7.56 * of the stack represented by this list)
17.68/7.56 * @throws NoSuchElementException if this list is empty
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public E pop() {
17.68/7.56 return removeFirst();
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Removes the first occurrence of the specified element in this
17.68/7.56 * list (when traversing the list from head to tail). If the list
17.68/7.56 * does not contain the element, it is unchanged.
17.68/7.56 *
17.68/7.56 * @param o element to be removed from this list, if present
17.68/7.56 * @return true if the list contained the specified element
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public boolean removeFirstOccurrence(Object o) {
17.68/7.56 return remove(o);
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Removes the last occurrence of the specified element in this
17.68/7.56 * list (when traversing the list from head to tail). If the list
17.68/7.56 * does not contain the element, it is unchanged.
17.68/7.56 *
17.68/7.56 * @param o element to be removed from this list, if present
17.68/7.56 * @return true if the list contained the specified element
17.68/7.56 * @since 1.6
17.68/7.56 */
17.68/7.56 public boolean removeLastOccurrence(Object o) {
17.68/7.56 if (o==null) {
17.68/7.56 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.56 if (e.element==null) {
17.68/7.56 remove(e);
17.68/7.56 return true;
17.68/7.56 }
17.68/7.56 }
17.68/7.56 } else {
17.68/7.56 for (Entry e = header.previous; e != header; e = e.previous) {
17.68/7.56 if (o.equals(e.element)) {
17.68/7.56 remove(e);
17.68/7.56 return true;
17.68/7.56 }
17.68/7.56 }
17.68/7.56 }
17.68/7.56 return false;
17.68/7.56 }
17.68/7.56
17.68/7.56 /**
17.68/7.56 * Returns a list-iterator of the elements in this list (in proper
17.68/7.56 * sequence), starting at the specified position in the list.
17.68/7.56 * Obeys the general contract of List.listIterator(int).
17.68/7.56 *
17.68/7.56 * The list-iterator is fail-fast: if the list is structurally
17.68/7.56 * modified at any time after the Iterator is created, in any way except
17.68/7.56 * through the list-iterator's own remove or add
17.68/7.56 * methods, the list-iterator will throw a
17.68/7.56 * ConcurrentModificationException. Thus, in the face of
17.68/7.56 * concurrent modification, the iterator fails quickly and cleanly, rather
17.68/7.56 * than risking arbitrary, non-deterministic behavior at an undetermined
17.68/7.56 * time in the future.
17.68/7.56 *
17.68/7.56 * @param index index of the first element to be returned from the
17.68/7.56 * list-iterator (by a call to next)
17.68/7.56 * @return a ListIterator of the elements in this list (in proper
17.68/7.56 * sequence), starting at the specified position in the list
17.68/7.56 * @throws IndexOutOfBoundsException {@inheritDoc}
17.68/7.56 * @see List#listIterator(int)
17.68/7.56 */
17.68/7.56 public ListIterator listIterator(int index) {
17.68/7.56 return new ListItr(index);
17.68/7.56 }
17.68/7.56
17.68/7.56 private class ListItr implements ListIterator {
17.68/7.56 private Entry lastReturned = header;
17.68/7.56 private Entry next;
17.68/7.56 private int nextIndex;
17.68/7.56 private int expectedModCount = modCount;
17.68/7.56
17.68/7.56 ListItr(int index) {
17.68/7.56 if (index < 0 || index > size)
17.68/7.56 throw new IndexOutOfBoundsException();
17.68/7.56 if (index < (size >> 1)) {
17.68/7.56 next = header.next;
17.68/7.56 for (nextIndex=0; nextIndexindex; nextIndex--)
17.68/7.56 next = next.previous;
17.68/7.56 }
17.68/7.56 }
17.68/7.56
17.68/7.56 public boolean hasNext() {
17.68/7.56 return nextIndex != size;
17.68/7.56 }
17.68/7.56
17.68/7.56 public E next() {
17.68/7.56 checkForComodification();
17.68/7.56 if (nextIndex == size)
17.68/7.56 throw new NoSuchElementException();
17.68/7.56
17.68/7.56 lastReturned = next;
17.68/7.56 next = next.next;
17.68/7.56 nextIndex++;
17.68/7.56 return lastReturned.element;
17.68/7.56 }
17.68/7.56
17.68/7.56 public boolean hasPrevious() {
17.68/7.56 return nextIndex != 0;
17.68/7.56 }
17.68/7.56
17.68/7.56 public E previous() {
17.68/7.56 if (nextIndex == 0)
17.68/7.56 throw new NoSuchElementException();
17.68/7.56
17.68/7.56 lastReturned = next = next.previous;
17.68/7.56 nextIndex--;
17.68/7.56 checkForComodification();
17.68/7.56 return lastReturned.element;
17.68/7.56 }
17.68/7.56
17.68/7.56 public int nextIndex() {
17.68/7.56 return nextIndex;
17.68/7.56 }
17.68/7.56
17.68/7.56 public int previousIndex() {
17.68/7.56 return nextIndex-1;
17.68/7.56 }
17.68/7.56
17.68/7.56 public void remove() {
17.68/7.56 checkForComodification();
17.68/7.56 Entry lastNext = lastReturned.next;
17.68/7.56 try {
17.68/7.56 LinkedList.this.remove(lastReturned);
17.68/7.56 } catch (NoSuchElementException e) {
17.68/7.56 throw new IllegalStateException();
17.68/7.56 }
17.68/7.56 if (next==lastReturned)
17.68/7.56 next = lastNext;
17.68/7.56 else
17.68/7.56 nextIndex--;
17.68/7.56 lastReturned = header;
17.68/7.56 expectedModCount++;
17.68/7.56 }
17.68/7.56
17.68/7.56 public void set(E e) {
17.68/7.56 if (lastReturned == header)
17.68/7.56 throw new IllegalStateException();
17.68/7.56 checkForComodification();
17.68/7.56 lastReturned.element = e;
17.68/7.56 }
17.68/7.56
17.68/7.56 public void add(E e) {
17.68/7.56 checkForComodification();
17.68/7.56 lastReturned = header;
17.68/7.56 addBefore(e, next);
17.68/7.56 nextIndex++;
17.68/7.56 expectedModCount++;
17.68/7.56 }
17.68/7.56
17.68/7.56 final void checkForComodification() {
17.68/7.56 if (modCount != expectedModCount)
17.68/7.56 throw new ConcurrentModificationException();
17.68/7.56 }
17.68/7.56 }
17.68/7.56
17.68/7.56 private static class Entry {
17.68/7.56 E element;
17.68/7.56 Entry next;
17.68/7.56 Entry previous;
17.68/7.56
17.68/7.56 Entry(E element, Entry next, Entry previous) {
17.68/7.56 this.element = element;
17.68/7.56 this.next = next;
17.68/7.56 this.previous = previous;
17.68/7.56 }
17.68/7.56 }
17.68/7.56
17.68/7.56 private Entry