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