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