|
From: <ls...@us...> - 2007-07-07 20:31:57
|
Revision: 3363
http://jnode.svn.sourceforge.net/jnode/?rev=3363&view=rev
Author: lsantha
Date: 2007-07-07 13:31:54 -0700 (Sat, 07 Jul 2007)
Log Message:
-----------
Openjdk integration.
Added Paths:
-----------
trunk/core/src/openjdk/java/java/util/concurrent/
trunk/core/src/openjdk/java/java/util/concurrent/AbstractExecutorService.java
trunk/core/src/openjdk/java/java/util/concurrent/ArrayBlockingQueue.java
trunk/core/src/openjdk/java/java/util/concurrent/BlockingDeque.java
trunk/core/src/openjdk/java/java/util/concurrent/BlockingQueue.java
trunk/core/src/openjdk/java/java/util/concurrent/BrokenBarrierException.java
trunk/core/src/openjdk/java/java/util/concurrent/Callable.java
trunk/core/src/openjdk/java/java/util/concurrent/CancellationException.java
trunk/core/src/openjdk/java/java/util/concurrent/CompletionService.java
trunk/core/src/openjdk/java/java/util/concurrent/ConcurrentHashMap.java
trunk/core/src/openjdk/java/java/util/concurrent/ConcurrentLinkedQueue.java
trunk/core/src/openjdk/java/java/util/concurrent/ConcurrentMap.java
trunk/core/src/openjdk/java/java/util/concurrent/ConcurrentNavigableMap.java
trunk/core/src/openjdk/java/java/util/concurrent/ConcurrentSkipListMap.java
trunk/core/src/openjdk/java/java/util/concurrent/ConcurrentSkipListSet.java
trunk/core/src/openjdk/java/java/util/concurrent/CopyOnWriteArrayList.java
trunk/core/src/openjdk/java/java/util/concurrent/CopyOnWriteArraySet.java
trunk/core/src/openjdk/java/java/util/concurrent/CountDownLatch.java
trunk/core/src/openjdk/java/java/util/concurrent/CyclicBarrier.java
trunk/core/src/openjdk/java/java/util/concurrent/DelayQueue.java
trunk/core/src/openjdk/java/java/util/concurrent/Delayed.java
trunk/core/src/openjdk/java/java/util/concurrent/Exchanger.java
trunk/core/src/openjdk/java/java/util/concurrent/ExecutionException.java
trunk/core/src/openjdk/java/java/util/concurrent/Executor.java
trunk/core/src/openjdk/java/java/util/concurrent/ExecutorCompletionService.java
trunk/core/src/openjdk/java/java/util/concurrent/ExecutorService.java
trunk/core/src/openjdk/java/java/util/concurrent/Executors.java
trunk/core/src/openjdk/java/java/util/concurrent/Future.java
trunk/core/src/openjdk/java/java/util/concurrent/FutureTask.java
trunk/core/src/openjdk/java/java/util/concurrent/LinkedBlockingDeque.java
trunk/core/src/openjdk/java/java/util/concurrent/LinkedBlockingQueue.java
trunk/core/src/openjdk/java/java/util/concurrent/PriorityBlockingQueue.java
trunk/core/src/openjdk/java/java/util/concurrent/RejectedExecutionException.java
trunk/core/src/openjdk/java/java/util/concurrent/RejectedExecutionHandler.java
trunk/core/src/openjdk/java/java/util/concurrent/RunnableFuture.java
trunk/core/src/openjdk/java/java/util/concurrent/RunnableScheduledFuture.java
trunk/core/src/openjdk/java/java/util/concurrent/ScheduledExecutorService.java
trunk/core/src/openjdk/java/java/util/concurrent/ScheduledFuture.java
trunk/core/src/openjdk/java/java/util/concurrent/ScheduledThreadPoolExecutor.java
trunk/core/src/openjdk/java/java/util/concurrent/Semaphore.java
trunk/core/src/openjdk/java/java/util/concurrent/SynchronousQueue.java
trunk/core/src/openjdk/java/java/util/concurrent/ThreadFactory.java
trunk/core/src/openjdk/java/java/util/concurrent/ThreadPoolExecutor.java
trunk/core/src/openjdk/java/java/util/concurrent/TimeUnit.java
trunk/core/src/openjdk/java/java/util/concurrent/TimeoutException.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicBoolean.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicInteger.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicIntegerArray.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicLong.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicLongArray.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicMarkableReference.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicReference.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicReferenceArray.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/AtomicStampedReference.java
trunk/core/src/openjdk/java/java/util/concurrent/atomic/package.html
trunk/core/src/openjdk/java/java/util/concurrent/locks/
trunk/core/src/openjdk/java/java/util/concurrent/locks/AbstractOwnableSynchronizer.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/AbstractQueuedSynchronizer.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/Condition.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/Lock.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/LockSupport.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/ReadWriteLock.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/ReentrantLock.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/ReentrantReadWriteLock.java
trunk/core/src/openjdk/java/java/util/concurrent/locks/package.html
trunk/core/src/openjdk/java/java/util/concurrent/package.html
Added: trunk/core/src/openjdk/java/java/util/concurrent/AbstractExecutorService.java
===================================================================
--- trunk/core/src/openjdk/java/java/util/concurrent/AbstractExecutorService.java (rev 0)
+++ trunk/core/src/openjdk/java/java/util/concurrent/AbstractExecutorService.java 2007-07-07 20:31:54 UTC (rev 3363)
@@ -0,0 +1,311 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * This file is available under and governed by the GNU General Public
+ * License version 2 only, as published by the Free Software Foundation.
+ * However, the following notice accompanied the original version of this
+ * file:
+ *
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ */
+
+package java.util.concurrent;
+import java.util.*;
+
+/**
+ * Provides default implementations of {@link ExecutorService}
+ * execution methods. This class implements the <tt>submit</tt>,
+ * <tt>invokeAny</tt> and <tt>invokeAll</tt> methods using a
+ * {@link RunnableFuture} returned by <tt>newTaskFor</tt>, which defaults
+ * to the {@link FutureTask} class provided in this package. For example,
+ * the implementation of <tt>submit(Runnable)</tt> creates an
+ * associated <tt>RunnableFuture</tt> that is executed and
+ * returned. Subclasses may override the <tt>newTaskFor</tt> methods
+ * to return <tt>RunnableFuture</tt> implementations other than
+ * <tt>FutureTask</tt>.
+ *
+ * <p> <b>Extension example</b>. Here is a sketch of a class
+ * that customizes {@link ThreadPoolExecutor} to use
+ * a <tt>CustomTask</tt> class instead of the default <tt>FutureTask</tt>:
+ * <pre>
+ * public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
+ *
+ * static class CustomTask<V> implements RunnableFuture<V> {...}
+ *
+ * protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
+ * return new CustomTask<V>(c);
+ * }
+ * protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
+ * return new CustomTask<V>(r, v);
+ * }
+ * // ... add constructors, etc.
+ * }
+ * </pre>
+ * @since 1.5
+ * @author Doug Lea
+ */
+public abstract class AbstractExecutorService implements ExecutorService {
+
+ /**
+ * Returns a <tt>RunnableFuture</tt> for the given runnable and default
+ * value.
+ *
+ * @param runnable the runnable task being wrapped
+ * @param value the default value for the returned future
+ * @return a <tt>RunnableFuture</tt> which when run will run the
+ * underlying runnable and which, as a <tt>Future</tt>, will yield
+ * the given value as its result and provide for cancellation of
+ * the underlying task.
+ * @since 1.6
+ */
+ protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
+ return new FutureTask<T>(runnable, value);
+ }
+
+ /**
+ * Returns a <tt>RunnableFuture</tt> for the given callable task.
+ *
+ * @param callable the callable task being wrapped
+ * @return a <tt>RunnableFuture</tt> which when run will call the
+ * underlying callable and which, as a <tt>Future</tt>, will yield
+ * the callable's result as its result and provide for
+ * cancellation of the underlying task.
+ * @since 1.6
+ */
+ protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
+ return new FutureTask<T>(callable);
+ }
+
+ /**
+ * @throws RejectedExecutionException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public Future<?> submit(Runnable task) {
+ if (task == null) throw new NullPointerException();
+ RunnableFuture<Object> ftask = newTaskFor(task, null);
+ execute(ftask);
+ return ftask;
+ }
+
+ /**
+ * @throws RejectedExecutionException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public <T> Future<T> submit(Runnable task, T result) {
+ if (task == null) throw new NullPointerException();
+ RunnableFuture<T> ftask = newTaskFor(task, result);
+ execute(ftask);
+ return ftask;
+ }
+
+ /**
+ * @throws RejectedExecutionException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public <T> Future<T> submit(Callable<T> task) {
+ if (task == null) throw new NullPointerException();
+ RunnableFuture<T> ftask = newTaskFor(task);
+ execute(ftask);
+ return ftask;
+ }
+
+ /**
+ * the main mechanics of invokeAny.
+ */
+ private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
+ boolean timed, long nanos)
+ throws InterruptedException, ExecutionException, TimeoutException {
+ if (tasks == null)
+ throw new NullPointerException();
+ int ntasks = tasks.size();
+ if (ntasks == 0)
+ throw new IllegalArgumentException();
+ List<Future<T>> futures= new ArrayList<Future<T>>(ntasks);
+ ExecutorCompletionService<T> ecs =
+ new ExecutorCompletionService<T>(this);
+
+ // For efficiency, especially in executors with limited
+ // parallelism, check to see if previously submitted tasks are
+ // done before submitting more of them. This interleaving
+ // plus the exception mechanics account for messiness of main
+ // loop.
+
+ try {
+ // Record exceptions so that if we fail to obtain any
+ // result, we can throw the last exception we got.
+ ExecutionException ee = null;
+ long lastTime = (timed)? System.nanoTime() : 0;
+ Iterator<? extends Callable<T>> it = tasks.iterator();
+
+ // Start one task for sure; the rest incrementally
+ futures.add(ecs.submit(it.next()));
+ --ntasks;
+ int active = 1;
+
+ for (;;) {
+ Future<T> f = ecs.poll();
+ if (f == null) {
+ if (ntasks > 0) {
+ --ntasks;
+ futures.add(ecs.submit(it.next()));
+ ++active;
+ }
+ else if (active == 0)
+ break;
+ else if (timed) {
+ f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
+ if (f == null)
+ throw new TimeoutException();
+ long now = System.nanoTime();
+ nanos -= now - lastTime;
+ lastTime = now;
+ }
+ else
+ f = ecs.take();
+ }
+ if (f != null) {
+ --active;
+ try {
+ return f.get();
+ } catch (InterruptedException ie) {
+ throw ie;
+ } catch (ExecutionException eex) {
+ ee = eex;
+ } catch (RuntimeException rex) {
+ ee = new ExecutionException(rex);
+ }
+ }
+ }
+
+ if (ee == null)
+ ee = new ExecutionException();
+ throw ee;
+
+ } finally {
+ for (Future<T> f : futures)
+ f.cancel(true);
+ }
+ }
+
+ public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
+ throws InterruptedException, ExecutionException {
+ try {
+ return doInvokeAny(tasks, false, 0);
+ } catch (TimeoutException cannotHappen) {
+ assert false;
+ return null;
+ }
+ }
+
+ public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
+ long timeout, TimeUnit unit)
+ throws InterruptedException, ExecutionException, TimeoutException {
+ return doInvokeAny(tasks, true, unit.toNanos(timeout));
+ }
+
+ public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
+ throws InterruptedException {
+ if (tasks == null)
+ throw new NullPointerException();
+ List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
+ boolean done = false;
+ try {
+ for (Callable<T> t : tasks) {
+ RunnableFuture<T> f = newTaskFor(t);
+ futures.add(f);
+ execute(f);
+ }
+ for (Future<T> f : futures) {
+ if (!f.isDone()) {
+ try {
+ f.get();
+ } catch (CancellationException ignore) {
+ } catch (ExecutionException ignore) {
+ }
+ }
+ }
+ done = true;
+ return futures;
+ } finally {
+ if (!done)
+ for (Future<T> f : futures)
+ f.cancel(true);
+ }
+ }
+
+ public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
+ long timeout, TimeUnit unit)
+ throws InterruptedException {
+ if (tasks == null || unit == null)
+ throw new NullPointerException();
+ long nanos = unit.toNanos(timeout);
+ List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
+ boolean done = false;
+ try {
+ for (Callable<T> t : tasks)
+ futures.add(newTaskFor(t));
+
+ long lastTime = System.nanoTime();
+
+ // Interleave time checks and calls to execute in case
+ // executor doesn't have any/much parallelism.
+ Iterator<Future<T>> it = futures.iterator();
+ while (it.hasNext()) {
+ execute((Runnable)(it.next()));
+ long now = System.nanoTime();
+ nanos -= now - lastTime;
+ lastTime = now;
+ if (nanos <= 0)
+ return futures;
+ }
+
+ for (Future<T> f : futures) {
+ if (!f.isDone()) {
+ if (nanos <= 0)
+ return futures;
+ try {
+ f.get(nanos, TimeUnit.NANOSECONDS);
+ } catch (CancellationException ignore) {
+ } catch (ExecutionException ignore) {
+ } catch (TimeoutException toe) {
+ return futures;
+ }
+ long now = System.nanoTime();
+ nanos -= now - lastTime;
+ lastTime = now;
+ }
+ }
+ done = true;
+ return futures;
+ } finally {
+ if (!done)
+ for (Future<T> f : futures)
+ f.cancel(true);
+ }
+ }
+
+}
Added: trunk/core/src/openjdk/java/java/util/concurrent/ArrayBlockingQueue.java
===================================================================
--- trunk/core/src/openjdk/java/java/util/concurrent/ArrayBlockingQueue.java (rev 0)
+++ trunk/core/src/openjdk/java/java/util/concurrent/ArrayBlockingQueue.java 2007-07-07 20:31:54 UTC (rev 3363)
@@ -0,0 +1,807 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * This file is available under and governed by the GNU General Public
+ * License version 2 only, as published by the Free Software Foundation.
+ * However, the following notice accompanied the original version of this
+ * file:
+ *
+ * Written by Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ */
+
+package java.util.concurrent;
+import java.util.concurrent.locks.*;
+import java.util.*;
+
+/**
+ * A bounded {@linkplain BlockingQueue blocking queue} backed by an
+ * array. This queue orders elements FIFO (first-in-first-out). The
+ * <em>head</em> of the queue is that element that has been on the
+ * queue the longest time. The <em>tail</em> of the queue is that
+ * element that has been on the queue the shortest time. New elements
+ * are inserted at the tail of the queue, and the queue retrieval
+ * operations obtain elements at the head of the queue.
+ *
+ * <p>This is a classic "bounded buffer", in which a
+ * fixed-sized array holds elements inserted by producers and
+ * extracted by consumers. Once created, the capacity cannot be
+ * increased. Attempts to <tt>put</tt> an element into a full queue
+ * will result in the operation blocking; attempts to <tt>take</tt> an
+ * element from an empty queue will similarly block.
+ *
+ * <p> This class supports an optional fairness policy for ordering
+ * waiting producer and consumer threads. By default, this ordering
+ * is not guaranteed. However, a queue constructed with fairness set
+ * to <tt>true</tt> grants threads access in FIFO order. Fairness
+ * generally decreases throughput but reduces variability and avoids
+ * starvation.
+ *
+ * <p>This class and its iterator implement all of the
+ * <em>optional</em> methods of the {@link Collection} and {@link
+ * Iterator} interfaces.
+ *
+ * <p>This class is a member of the
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">
+ * Java Collections Framework</a>.
+ *
+ * @since 1.5
+ * @author Doug Lea
+ * @param <E> the type of elements held in this collection
+ */
+public class ArrayBlockingQueue<E> extends AbstractQueue<E>
+ implements BlockingQueue<E>, java.io.Serializable {
+
+ /**
+ * Serialization ID. This class relies on default serialization
+ * even for the items array, which is default-serialized, even if
+ * it is empty. Otherwise it could not be declared final, which is
+ * necessary here.
+ */
+ private static final long serialVersionUID = -817911632652898426L;
+
+ /** The queued items */
+ private final E[] items;
+ /** items index for next take, poll or remove */
+ private int takeIndex;
+ /** items index for next put, offer, or add. */
+ private int putIndex;
+ /** Number of items in the queue */
+ private int count;
+
+ /*
+ * Concurrency control uses the classic two-condition algorithm
+ * found in any textbook.
+ */
+
+ /** Main lock guarding all access */
+ private final ReentrantLock lock;
+ /** Condition for waiting takes */
+ private final Condition notEmpty;
+ /** Condition for waiting puts */
+ private final Condition notFull;
+
+ // Internal helper methods
+
+ /**
+ * Circularly increment i.
+ */
+ final int inc(int i) {
+ return (++i == items.length)? 0 : i;
+ }
+
+ /**
+ * Inserts element at current put position, advances, and signals.
+ * Call only when holding lock.
+ */
+ private void insert(E x) {
+ items[putIndex] = x;
+ putIndex = inc(putIndex);
+ ++count;
+ notEmpty.signal();
+ }
+
+ /**
+ * Extracts element at current take position, advances, and signals.
+ * Call only when holding lock.
+ */
+ private E extract() {
+ final E[] items = this.items;
+ E x = items[takeIndex];
+ items[takeIndex] = null;
+ takeIndex = inc(takeIndex);
+ --count;
+ notFull.signal();
+ return x;
+ }
+
+ /**
+ * Utility for remove and iterator.remove: Delete item at position i.
+ * Call only when holding lock.
+ */
+ void removeAt(int i) {
+ final E[] items = this.items;
+ // if removing front item, just advance
+ if (i == takeIndex) {
+ items[takeIndex] = null;
+ takeIndex = inc(takeIndex);
+ } else {
+ // slide over all others up through putIndex.
+ for (;;) {
+ int nexti = inc(i);
+ if (nexti != putIndex) {
+ items[i] = items[nexti];
+ i = nexti;
+ } else {
+ items[i] = null;
+ putIndex = i;
+ break;
+ }
+ }
+ }
+ --count;
+ notFull.signal();
+ }
+
+ /**
+ * Creates an <tt>ArrayBlockingQueue</tt> with the given (fixed)
+ * capacity and default access policy.
+ *
+ * @param capacity the capacity of this queue
+ * @throws IllegalArgumentException if <tt>capacity</tt> is less than 1
+ */
+ public ArrayBlockingQueue(int capacity) {
+ this(capacity, false);
+ }
+
+ /**
+ * Creates an <tt>ArrayBlockingQueue</tt> with the given (fixed)
+ * capacity and the specified access policy.
+ *
+ * @param capacity the capacity of this queue
+ * @param fair if <tt>true</tt> then queue accesses for threads blocked
+ * on insertion or removal, are processed in FIFO order;
+ * if <tt>false</tt> the access order is unspecified.
+ * @throws IllegalArgumentException if <tt>capacity</tt> is less than 1
+ */
+ public ArrayBlockingQueue(int capacity, boolean fair) {
+ if (capacity <= 0)
+ throw new IllegalArgumentException();
+ this.items = (E[]) new Object[capacity];
+ lock = new ReentrantLock(fair);
+ notEmpty = lock.newCondition();
+ notFull = lock.newCondition();
+ }
+
+ /**
+ * Creates an <tt>ArrayBlockingQueue</tt> with the given (fixed)
+ * capacity, the specified access policy and initially containing the
+ * elements of the given collection,
+ * added in traversal order of the collection's iterator.
+ *
+ * @param capacity the capacity of this queue
+ * @param fair if <tt>true</tt> then queue accesses for threads blocked
+ * on insertion or removal, are processed in FIFO order;
+ * if <tt>false</tt> the access order is unspecified.
+ * @param c the collection of elements to initially contain
+ * @throws IllegalArgumentException if <tt>capacity</tt> is less than
+ * <tt>c.size()</tt>, or less than 1.
+ * @throws NullPointerException if the specified collection or any
+ * of its elements are null
+ */
+ public ArrayBlockingQueue(int capacity, boolean fair,
+ Collection<? extends E> c) {
+ this(capacity, fair);
+ if (capacity < c.size())
+ throw new IllegalArgumentException();
+
+ for (Iterator<? extends E> it = c.iterator(); it.hasNext();)
+ add(it.next());
+ }
+
+ /**
+ * Inserts the specified element at the tail of this queue if it is
+ * possible to do so immediately without exceeding the queue's capacity,
+ * returning <tt>true</tt> upon success and throwing an
+ * <tt>IllegalStateException</tt> if this queue is full.
+ *
+ * @param e the element to add
+ * @return <tt>true</tt> (as specified by {@link Collection#add})
+ * @throws IllegalStateException if this queue is full
+ * @throws NullPointerException if the specified element is null
+ */
+ public boolean add(E e) {
+ return super.add(e);
+ }
+
+ /**
+ * Inserts the specified element at the tail of this queue if it is
+ * possible to do so immediately without exceeding the queue's capacity,
+ * returning <tt>true</tt> upon success and <tt>false</tt> if this queue
+ * is full. This method is generally preferable to method {@link #add},
+ * which can fail to insert an element only by throwing an exception.
+ *
+ * @throws NullPointerException if the specified element is null
+ */
+ public boolean offer(E e) {
+ if (e == null) throw new NullPointerException();
+ final ReentrantLock lock = this.lock;
+ lock.lock();
+ try {
+ if (count == items.length)
+ return false;
+ else {
+ insert(e);
+ return true;
+ }
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ /**
+ * Inserts the specified element at the tail of this queue, waiting
+ * for space to become available if the queue is full.
+ *
+ * @throws InterruptedException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public void put(E e) throws InterruptedException {
+ if (e == null) throw new NullPointerException();
+ final E[] items = this.items;
+ final ReentrantLock lock = this.lock;
+ lock.lockInterruptibly();
+ try {
+ try {
+ while (count == items.length)
+ notFull.await();
+ } catch (InterruptedException ie) {
+ notFull.signal(); // propagate to non-interrupted thread
+ throw ie;
+ }
+ insert(e);
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ /**
+ * Inserts the specified element at the tail of this queue, waiting
+ * up to the specified wait time for space to become available if
+ * the queue is full.
+ *
+ * @throws InterruptedException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public boolean offer(E e, long timeout, TimeUnit unit)
+ throws InterruptedException {
+
+ if (e == null) throw new NullPointerException();
+ long nanos = unit.toNanos(timeout);
+ final ReentrantLock lock = this.lock;
+ lock.lockInterruptibly();
+ try {
+ for (;;) {
+ if (count != items.length) {
+ insert(e);
+ return true;
+ }
+ if (nanos <= 0)
+ return false;
+ try {
+ nanos = notFull.awaitNanos(nanos);
+ } catch (InterruptedException ie) {
+ notFull.signal(); // propagate to non-interrupted thread
+ throw ie;
+ }
+ }
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ public E poll() {
+ final ReentrantLock lock = this.lock;
+ lock.lock();
...
[truncated message content] |
|
From: <ls...@us...> - 2007-10-06 20:30:33
|
Revision: 3538
http://jnode.svn.sourceforge.net/jnode/?rev=3538&view=rev
Author: lsantha
Date: 2007-10-06 13:30:32 -0700 (Sat, 06 Oct 2007)
Log Message:
-----------
Openjdk integration of the Collections framework.
Added Paths:
-----------
trunk/core/src/openjdk/java/java/util/AbstractCollection.java
trunk/core/src/openjdk/java/java/util/AbstractList.java
trunk/core/src/openjdk/java/java/util/AbstractMap.java
trunk/core/src/openjdk/java/java/util/AbstractQueue.java
trunk/core/src/openjdk/java/java/util/AbstractSequentialList.java
trunk/core/src/openjdk/java/java/util/AbstractSet.java
trunk/core/src/openjdk/java/java/util/ArrayDeque.java
trunk/core/src/openjdk/java/java/util/ArrayList.java
trunk/core/src/openjdk/java/java/util/Collections.java
trunk/core/src/openjdk/java/java/util/Dictionary.java
trunk/core/src/openjdk/java/java/util/EnumMap.java
trunk/core/src/openjdk/java/java/util/HashMap.java
trunk/core/src/openjdk/java/java/util/HashSet.java
trunk/core/src/openjdk/java/java/util/Hashtable.java
trunk/core/src/openjdk/java/java/util/IdentityHashMap.java
trunk/core/src/openjdk/java/java/util/LinkedHashMap.java
trunk/core/src/openjdk/java/java/util/LinkedHashSet.java
trunk/core/src/openjdk/java/java/util/LinkedList.java
trunk/core/src/openjdk/java/java/util/PriorityQueue.java
trunk/core/src/openjdk/java/java/util/Stack.java
trunk/core/src/openjdk/java/java/util/TreeMap.java
trunk/core/src/openjdk/java/java/util/TreeSet.java
trunk/core/src/openjdk/java/java/util/Vector.java
trunk/core/src/openjdk/java/java/util/WeakHashMap.java
Added: trunk/core/src/openjdk/java/java/util/AbstractCollection.java
===================================================================
--- trunk/core/src/openjdk/java/java/util/AbstractCollection.java (rev 0)
+++ trunk/core/src/openjdk/java/java/util/AbstractCollection.java 2007-10-06 20:30:32 UTC (rev 3538)
@@ -0,0 +1,447 @@
+/*
+ * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package java.util;
+
+/**
+ * This class provides a skeletal implementation of the <tt>Collection</tt>
+ * interface, to minimize the effort required to implement this interface. <p>
+ *
+ * To implement an unmodifiable collection, the programmer needs only to
+ * extend this class and provide implementations for the <tt>iterator</tt> and
+ * <tt>size</tt> methods. (The iterator returned by the <tt>iterator</tt>
+ * method must implement <tt>hasNext</tt> and <tt>next</tt>.)<p>
+ *
+ * To implement a modifiable collection, the programmer must additionally
+ * override this class's <tt>add</tt> method (which otherwise throws an
+ * <tt>UnsupportedOperationException</tt>), and the iterator returned by the
+ * <tt>iterator</tt> method must additionally implement its <tt>remove</tt>
+ * method.<p>
+ *
+ * The programmer should generally provide a void (no argument) and
+ * <tt>Collection</tt> constructor, as per the recommendation in the
+ * <tt>Collection</tt> interface specification.<p>
+ *
+ * The documentation for each non-abstract method in this class describes its
+ * implementation in detail. Each of these methods may be overridden if
+ * the collection being implemented admits a more efficient implementation.<p>
+ *
+ * This class is a member of the
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">
+ * Java Collections Framework</a>.
+ *
+ * @author Josh Bloch
+ * @author Neal Gafter
+ * @version 1.44, 05/05/07
+ * @see Collection
+ * @since 1.2
+ */
+
+public abstract class AbstractCollection<E> implements Collection<E> {
+ /**
+ * Sole constructor. (For invocation by subclass constructors, typically
+ * implicit.)
+ */
+ protected AbstractCollection() {
+ }
+
+ // Query Operations
+
+ /**
+ * Returns an iterator over the elements contained in this collection.
+ *
+ * @return an iterator over the elements contained in this collection
+ */
+ public abstract Iterator<E> iterator();
+
+ public abstract int size();
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation returns <tt>size() == 0</tt>.
+ */
+ public boolean isEmpty() {
+ return size() == 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation iterates over the elements in the collection,
+ * checking each element in turn for equality with the specified element.
+ *
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public boolean contains(Object o) {
+ Iterator<E> e = iterator();
+ if (o==null) {
+ while (e.hasNext())
+ if (e.next()==null)
+ return true;
+ } else {
+ while (e.hasNext())
+ if (o.equals(e.next()))
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation returns an array containing all the elements
+ * returned by this collection's iterator, in the same order, stored in
+ * consecutive elements of the array, starting with index {@code 0}.
+ * The length of the returned array is equal to the number of elements
+ * returned by the iterator, even if the size of this collection changes
+ * during iteration, as might happen if the collection permits
+ * concurrent modification during iteration. The {@code size} method is
+ * called only as an optimization hint; the correct result is returned
+ * even if the iterator returns a different number of elements.
+ *
+ * <p>This method is equivalent to:
+ *
+ * <pre> {@code
+ * List<E> list = new ArrayList<E>(size());
+ * for (E e : this)
+ * list.add(e);
+ * return list.toArray();
+ * }</pre>
+ */
+ public Object[] toArray() {
+ // Estimate size of array; be prepared to see more or fewer elements
+ Object[] r = new Object[size()];
+ Iterator<E> it = iterator();
+ for (int i = 0; i < r.length; i++) {
+ if (! it.hasNext()) // fewer elements than expected
+ return Arrays.copyOf(r, i);
+ r[i] = it.next();
+ }
+ return it.hasNext() ? finishToArray(r, it) : r;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation returns an array containing all the elements
+ * returned by this collection's iterator in the same order, stored in
+ * consecutive elements of the array, starting with index {@code 0}.
+ * If the number of elements returned by the iterator is too large to
+ * fit into the specified array, then the elements are returned in a
+ * newly allocated array with length equal to the number of elements
+ * returned by the iterator, even if the size of this collection
+ * changes during iteration, as might happen if the collection permits
+ * concurrent modification during iteration. The {@code size} method is
+ * called only as an optimization hint; the correct result is returned
+ * even if the iterator returns a different number of elements.
+ *
+ * <p>This method is equivalent to:
+ *
+ * <pre> {@code
+ * List<E> list = new ArrayList<E>(size());
+ * for (E e : this)
+ * list.add(e);
+ * return list.toArray(a);
+ * }</pre>
+ *
+ * @throws ArrayStoreException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public <T> T[] toArray(T[] a) {
+ // Estimate size of array; be prepared to see more or fewer elements
+ int size = size();
+ T[] r = a.length >= size ? a :
+ (T[])java.lang.reflect.Array
+ .newInstance(a.getClass().getComponentType(), size);
+ Iterator<E> it = iterator();
+
+ for (int i = 0; i < r.length; i++) {
+ if (! it.hasNext()) { // fewer elements than expected
+ if (a != r)
+ return Arrays.copyOf(r, i);
+ r[i] = null; // null-terminate
+ return r;
+ }
+ r[i] = (T)it.next();
+ }
+ return it.hasNext() ? finishToArray(r, it) : r;
+ }
+
+ /**
+ * Reallocates the array being used within toArray when the iterator
+ * returned more elements than expected, and finishes filling it from
+ * the iterator.
+ *
+ * @param r the array, replete with previously stored elements
+ * @param it the in-progress iterator over this collection
+ * @return array containing the elements in the given array, plus any
+ * further elements returned by the iterator, trimmed to size
+ */
+ private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
+ int i = r.length;
+ while (it.hasNext()) {
+ int cap = r.length;
+ if (i == cap) {
+ int newCap = ((cap / 2) + 1) * 3;
+ if (newCap <= cap) { // integer overflow
+ if (cap == Integer.MAX_VALUE)
+ throw new OutOfMemoryError
+ ("Required array size too large");
+ newCap = Integer.MAX_VALUE;
+ }
+ r = Arrays.copyOf(r, newCap);
+ }
+ r[i++] = (T)it.next();
+ }
+ // trim if overallocated
+ return (i == r.length) ? r : Arrays.copyOf(r, i);
+ }
+
+ // Modification Operations
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation always throws an
+ * <tt>UnsupportedOperationException</tt>.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ * @throws IllegalArgumentException {@inheritDoc}
+ * @throws IllegalStateException {@inheritDoc}
+ */
+ public boolean add(E e) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation iterates over the collection looking for the
+ * specified element. If it finds the element, it removes the element
+ * from the collection using the iterator's remove method.
+ *
+ * <p>Note that this implementation throws an
+ * <tt>UnsupportedOperationException</tt> if the iterator returned by this
+ * collection's iterator method does not implement the <tt>remove</tt>
+ * method and this collection contains the specified object.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public boolean remove(Object o) {
+ Iterator<E> e = iterator();
+ if (o==null) {
+ while (e.hasNext()) {
+ if (e.next()==null) {
+ e.remove();
+ return true;
+ }
+ }
+ } else {
+ while (e.hasNext()) {
+ if (o.equals(e.next())) {
+ e.remove();
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+
+ // Bulk Operations
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation iterates over the specified collection,
+ * checking each element returned by the iterator in turn to see
+ * if it's contained in this collection. If all elements are so
+ * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
+ *
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ * @see #contains(Object)
+ */
+ public boolean containsAll(Collection<?> c) {
+ Iterator<?> e = c.iterator();
+ while (e.hasNext())
+ if (!contains(e.next()))
+ return false;
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation iterates over the specified collection, and adds
+ * each object returned by the iterator to this collection, in turn.
+ *
+ * <p>Note that this implementation will throw an
+ * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
+ * overridden (assuming the specified collection is non-empty).
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ * @throws IllegalArgumentException {@inheritDoc}
+ * @throws IllegalStateException {@inheritDoc}
+ *
+ * @see #add(Object)
+ */
+ public boolean addAll(Collection<? extends E> c) {
+ boolean modified = false;
+ Iterator<? extends E> e = c.iterator();
+ while (e.hasNext()) {
+ if (add(e.next()))
+ modified = true;
+ }
+ return modified;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation iterates over this collection, checking each
+ * element returned by the iterator in turn to see if it's contained
+ * in the specified collection. If it's so contained, it's removed from
+ * this collection with the iterator's <tt>remove</tt> method.
+ *
+ * <p>Note that this implementation will throw an
+ * <tt>UnsupportedOperationException</tt> if the iterator returned by the
+ * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
+ * and this collection contains one or more elements in common with the
+ * specified collection.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ *
+ * @see #remove(Object)
+ * @see #contains(Object)
+ */
+ public boolean removeAll(Collection<?> c) {
+ boolean modified = false;
+ Iterator<?> e = iterator();
+ while (e.hasNext()) {
+ if (c.contains(e.next())) {
+ e.remove();
+ modified = true;
+ }
+ }
+ return modified;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation iterates over this collection, checking each
+ * element returned by the iterator in turn to see if it's contained
+ * in the specified collection. If it's not so contained, it's removed
+ * from this collection with the iterator's <tt>remove</tt> method.
+ *
+ * <p>Note that this implementation will throw an
+ * <tt>UnsupportedOperationException</tt> if the iterator returned by the
+ * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
+ * and this collection contains one or more elements not present in the
+ * specified collection.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ *
+ * @see #remove(Object)
+ * @see #contains(Object)
+ */
+ public boolean retainAll(Collection<?> c) {
+ boolean modified = false;
+ Iterator<E> e = iterator();
+ while (e.hasNext()) {
+ if (!c.contains(e.next())) {
+ e.remove();
+ modified = true;
+ }
+ }
+ return modified;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation iterates over this collection, removing each
+ * element using the <tt>Iterator.remove</tt> operation. Most
+ * implementations will probably choose to override this method for
+ * efficiency.
+ *
+ * <p>Note that this implementation will throw an
+ * <tt>UnsupportedOperationException</tt> if the iterator returned by this
+ * collection's <tt>iterator</tt> method does not implement the
+ * <tt>remove</tt> method and this collection is non-empty.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ */
+ public void clear() {
+ Iterator<E> e = iterator();
+ while (e.hasNext()) {
+ e.next();
+ e.remove();
+ }
+ }
+
+
+ // String conversion
+
+ /**
+ * Returns a string representation of this collection. The string
+ * representation consists of a list of the collection's elements in the
+ * order they are returned by its iterator, enclosed in square brackets
+ * (<tt>"[]"</tt>). Adjacent elements are separated by the characters
+ * <tt>", "</tt> (comma and space). Elements are converted to strings as
+ * by {@link String#valueOf(Object)}.
+ *
+ * @return a string representation of this collection
+ */
+ public String toString() {
+ Iterator<E> i = iterator();
+ if (! i.hasNext())
+ return "[]";
+
+ StringBuilder sb = new StringBuilder();
+ sb.append('[');
+ for (;;) {
+ E e = i.next();
+ sb.append(e == this ? "(this Collection)" : e);
+ if (! i.hasNext())
+ return sb.append(']').toString();
+ sb.append(", ");
+ }
+ }
+
+}
Added: trunk/core/src/openjdk/java/java/util/AbstractList.java
===================================================================
--- trunk/core/src/openjdk/java/java/util/AbstractList.java (rev 0)
+++ trunk/core/src/openjdk/java/java/util/AbstractList.java 2007-10-06 20:30:32 UTC (rev 3538)
@@ -0,0 +1,782 @@
+/*
+ * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package java.util;
+
+/**
+ * This class provides a skeletal implementation of the {@link List}
+ * interface to minimize the effort required to implement this interface
+ * backed by a "random access" data store (such as an array). For sequential
+ * access data (such as a linked list), {@link AbstractSequentialList} should
+ * be used in preference to this class.
+ *
+ * <p>To implement an unmodifiable list, the programmer needs only to extend
+ * this class and provide implementations for the {@link #get(int)} and
+ * {@link List#size() size()} methods.
+ *
+ * <p>To implement a modifiable list, the programmer must additionally
+ * override the {@link #set(int, Object) set(int, E)} method (which otherwise
+ * throws an {@code UnsupportedOperationException}). If the list is
+ * variable-size the programmer must additionally override the
+ * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
+ *
+ * <p>The programmer should generally provide a void (no argument) and collection
+ * constructor, as per the recommendation in the {@link Collection} interface
+ * specification.
+ *
+ * <p>Unlike the other abstract collection implementations, the programmer does
+ * <i>not</i> have to provide an iterator implementation; the iterator and
+ * list iterator are implemented by this class, on top of the "random access"
+ * methods:
+ * {@link #get(int)},
+ * {@link #set(int, Object) set(int, E)},
+ * {@link #add(int, Object) add(int, E)} and
+ * {@link #remove(int)}.
+ *
+ * <p>The documentation for each non-abstract method in this class describes its
+ * implementation in detail. Each of these methods may be overridden if the
+ * collection being implemented admits a more efficient implementation.
+ *
+ * <p>This class is a member of the
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">
+ * Java Collections Framework</a>.
+ *
+ * @author Josh Bloch
+ * @author Neal Gafter
+ * @version 1.58, 05/05/07
+ * @since 1.2
+ */
+
+public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
+ /**
+ * Sole constructor. (For invocation by subclass constructors, typically
+ * implicit.)
+ */
+ protected AbstractList() {
+ }
+
+ /**
+ * Appends the specified element to the end of this list (optional
+ * operation).
+ *
+ * <p>Lists that support this operation may place limitations on what
+ * elements may be added to this list. In particular, some
+ * lists will refuse to add null elements, and others will impose
+ * restrictions on the type of elements that may be added. List
+ * classes should clearly specify in their documentation any restrictions
+ * on what elements may be added.
+ *
+ * <p>This implementation calls {@code add(size(), e)}.
+ *
+ * <p>Note that this implementation throws an
+ * {@code UnsupportedOperationException} unless
+ * {@link #add(int, Object) add(int, E)} is overridden.
+ *
+ * @param e element to be appended to this list
+ * @return {@code true} (as specified by {@link Collection#add})
+ * @throws UnsupportedOperationException if the {@code add} operation
+ * is not supported by this list
+ * @throws ClassCastException if the class of the specified element
+ * prevents it from being added to this list
+ * @throws NullPointerException if the specified element is null and this
+ * list does not permit null elements
+ * @throws IllegalArgumentException if some property of this element
+ * prevents it from being added to this list
+ */
+ public boolean add(E e) {
+ add(size(), e);
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws IndexOutOfBoundsException {@inheritDoc}
+ */
+ abstract public E get(int index);
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation always throws an
+ * {@code UnsupportedOperationException}.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ * @throws IllegalArgumentException {@inheritDoc}
+ * @throws IndexOutOfBoundsException {@inheritDoc}
+ */
+ public E set(int index, E element) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation always throws an
+ * {@code UnsupportedOperationException}.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ * @throws IllegalArgumentException {@inheritDoc}
+ * @throws IndexOutOfBoundsException {@inheritDoc}
+ */
+ public void add(int index, E element) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation always throws an
+ * {@code UnsupportedOperationException}.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws IndexOutOfBoundsException {@inheritDoc}
+ */
+ public E remove(int index) {
+ throw new UnsupportedOperationException();
+ }
+
+
+ // Search Operations
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation first gets a list iterator (with
+ * {@code listIterator()}). Then, it iterates over the list until the
+ * specified element is found or the end of the list is reached.
+ *
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public int indexOf(Object o) {
+ ListIterator<E> e = listIterator();
+ if (o==null) {
+ while (e.hasNext())
+ if (e.next()==null)
+ return e.previousIndex();
+ } else {
+ while (e.hasNext())
+ if (o.equals(e.next()))
+ return e.previousIndex();
+ }
+ return -1;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation first gets a list iterator that points to the end
+ * of the list (with {@code listIterator(size())}). Then, it iterates
+ * backwards over the list until the specified element is found, or the
+ * beginning of the list is reached.
+ *
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ */
+ public int lastIndexOf(Object o) {
+ ListIterator<E> e = listIterator(size());
+ if (o==null) {
+ while (e.hasPrevious())
+ if (e.previous()==null)
+ return e.nextIndex();
+ } else {
+ while (e.hasPrevious())
+ if (o.equals(e.previous()))
+ return e.nextIndex();
+ }
+ return -1;
+ }
+
+
+ // Bulk Operations
+
+ /**
+ * Removes all of the elements from this list (optional operation).
+ * The list will be empty after this call returns.
+ *
+ * <p>This implementation calls {@code removeRange(0, size())}.
+ *
+ * <p>Note that this implementation throws an
+ * {@code UnsupportedOperationException} unless {@code remove(int
+ * index)} or {@code removeRange(int fromIndex, int toIndex)} is
+ * overridden.
+ *
+ * @throws UnsupportedOperationException if the {@code clear} operation
+ * is not supported by this list
+ */
+ public void clear() {
+ removeRange(0, size());
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation gets an iterator over the specified collection
+ * and iterates over it, inserting the elements obtained from the
+ * iterator into this list at the appropriate position, one at a time,
+ * using {@code add(int, E)}.
+ * Many implementations will override this method for efficiency.
+ *
+ * <p>Note that this implementation throws an
+ * {@code UnsupportedOperationException} unless
+ * {@link #add(int, Object) add(int, E)} is overridden.
+ *
+ * @throws UnsupportedOperationException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException {@inheritDoc}
+ * @throws IllegalArgumentException {@inheritDoc}
+ * @throws IndexOutOfBoundsException {@inheritDoc}
+ */
+ public boolean addAll(int index, Collection<? extends E> c) {
+ boolean modified = false;
+ Iterator<? extends E> e = c.iterator();
+ while (e.hasNext()) {
+ add(index++, e.next());
+ modified = true;
+ }
+ return modified;
+ }
+
+
+ // Iterators
+
+ /**
+ * Returns an iterator over the elements in this list in proper sequence.
+ *
+ * <p>This implementation returns a straightforward implementation of the
+ * iterator interface, relying on the backing list's {@code size()},
+ * {@code get(int)}, and {@code remove(int)} methods.
+ *
+ * <p>Note that the iterator returned by this method will throw an
+ * {@code UnsupportedOperationException} in response to its
+ * {@code remove} method unless the list's {@code remove(int)} method is
+ * overridden.
+ *
+ * <p>This implementation can be made to throw runtime exceptions in the
+ * face of concurrent modification, as described in the specification
+ * for the (protected) {@code modCount} field.
+ *
+ * @return an iterator over the elements in this list in proper sequence
+ *
+ * @see #modCount
+ */
+ public Iterator<E> iterator() {
+ return new Itr();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation returns {@code listIterator(0)}.
+ *
+ * @see #listIterator(int)
+ */
+ public ListIterator<E> listIterator() {
+ return listIterator(0);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation returns a straightforward implementation of the
+ * {@code ListIterator} interface that extends the implementation of the
+ * {@code Iterator} interface returned by the {@code iterator()} method.
+ * The {@code ListIterator} implementation relies on the backing list's
+ * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
+ * and {@code remove(int)} methods.
+ *
+ * <p>Note that the list iterator returned by this implementation will
+ * throw an {@code UnsupportedOperationException} in response to its
+ * {@code remove}, {@code set} and {@code add} methods unless the
+ * list's {@code remove(int)}, {@code set(int, E)}, and
+ * {@code add(int, E)} methods are overridden.
+ *
+ * <p>This implementation can be made to throw runtime exceptions in the
+ * face of concurrent modification, as described in the specification for
+ * the (protected) {@code modCount} field.
+ *
+ * @throws IndexOutOfBoundsException {@inheritDoc}
+ *
+ * @see #modCount
+ */
+ public ListIterator<E> listIterator(final int index) {
+ if (index<0 || index>size())
+ throw new IndexOutOfBoundsException("Index: "+index);
+
+ return new ListItr(index);
+ }
+
+ private class Itr implements Iterator<E> {
+ /**
+ * Index of element to be returned by subsequent...
[truncated message content] |