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