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