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