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