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