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