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