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