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