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