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