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