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