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