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