More classes needed by David jdk7-b147
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Feb 2013 16:10:10 +0100
branchjdk7-b147
changeset 633bc6f3be91306
parent 597 ee8a922f4268
child 634 80532cda6175
child 637 74b8fb771bc2
More classes needed by David
emul/compact/src/main/java/java/lang/AbstractMethodError.java
emul/compact/src/main/java/java/lang/IncompatibleClassChangeError.java
emul/compact/src/main/java/java/lang/NoSuchFieldError.java
emul/compact/src/main/java/java/util/PriorityQueue.java
emul/compact/src/main/java/java/util/concurrent/TimeUnit.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/compact/src/main/java/java/lang/AbstractMethodError.java	Fri Feb 01 16:10:10 2013 +0100
     1.3 @@ -0,0 +1,58 @@
     1.4 +/*
     1.5 + * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +/**
    1.32 + * Thrown when an application tries to call an abstract method.
    1.33 + * Normally, this error is caught by the compiler; this error can
    1.34 + * only occur at run time if the definition of some class has
    1.35 + * incompatibly changed since the currently executing method was last
    1.36 + * compiled.
    1.37 + *
    1.38 + * @author  unascribed
    1.39 + * @since   JDK1.0
    1.40 + */
    1.41 +public
    1.42 +class AbstractMethodError extends IncompatibleClassChangeError {
    1.43 +    private static final long serialVersionUID = -1654391082989018462L;
    1.44 +
    1.45 +    /**
    1.46 +     * Constructs an <code>AbstractMethodError</code> with no detail  message.
    1.47 +     */
    1.48 +    public AbstractMethodError() {
    1.49 +        super();
    1.50 +    }
    1.51 +
    1.52 +    /**
    1.53 +     * Constructs an <code>AbstractMethodError</code> with the specified
    1.54 +     * detail message.
    1.55 +     *
    1.56 +     * @param   s   the detail message.
    1.57 +     */
    1.58 +    public AbstractMethodError(String s) {
    1.59 +        super(s);
    1.60 +    }
    1.61 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/emul/compact/src/main/java/java/lang/IncompatibleClassChangeError.java	Fri Feb 01 16:10:10 2013 +0100
     2.3 @@ -0,0 +1,57 @@
     2.4 +/*
     2.5 + * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.  Oracle designates this
    2.11 + * particular file as subject to the "Classpath" exception as provided
    2.12 + * by Oracle in the LICENSE file that accompanied this code.
    2.13 + *
    2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.17 + * version 2 for more details (a copy is included in the LICENSE file that
    2.18 + * accompanied this code).
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License version
    2.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.23 + *
    2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.25 + * or visit www.oracle.com if you need additional information or have any
    2.26 + * questions.
    2.27 + */
    2.28 +
    2.29 +package java.lang;
    2.30 +
    2.31 +/**
    2.32 + * Thrown when an incompatible class change has occurred to some class
    2.33 + * definition. The definition of some class, on which the currently
    2.34 + * executing method depends, has since changed.
    2.35 + *
    2.36 + * @author  unascribed
    2.37 + * @since   JDK1.0
    2.38 + */
    2.39 +public
    2.40 +class IncompatibleClassChangeError extends LinkageError {
    2.41 +    private static final long serialVersionUID = -4914975503642802119L;
    2.42 +
    2.43 +    /**
    2.44 +     * Constructs an <code>IncompatibleClassChangeError</code> with no
    2.45 +     * detail message.
    2.46 +     */
    2.47 +    public IncompatibleClassChangeError () {
    2.48 +        super();
    2.49 +    }
    2.50 +
    2.51 +    /**
    2.52 +     * Constructs an <code>IncompatibleClassChangeError</code> with the
    2.53 +     * specified detail message.
    2.54 +     *
    2.55 +     * @param   s   the detail message.
    2.56 +     */
    2.57 +    public IncompatibleClassChangeError(String s) {
    2.58 +        super(s);
    2.59 +    }
    2.60 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/emul/compact/src/main/java/java/lang/NoSuchFieldError.java	Fri Feb 01 16:10:10 2013 +0100
     3.3 @@ -0,0 +1,59 @@
     3.4 +/*
     3.5 + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.  Oracle designates this
    3.11 + * particular file as subject to the "Classpath" exception as provided
    3.12 + * by Oracle in the LICENSE file that accompanied this code.
    3.13 + *
    3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.17 + * version 2 for more details (a copy is included in the LICENSE file that
    3.18 + * accompanied this code).
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License version
    3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.23 + *
    3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.25 + * or visit www.oracle.com if you need additional information or have any
    3.26 + * questions.
    3.27 + */
    3.28 +
    3.29 +package java.lang;
    3.30 +
    3.31 +/**
    3.32 + * Thrown if an application tries to access or modify a specified
    3.33 + * field of an object, and that object no longer has that field.
    3.34 + * <p>
    3.35 + * Normally, this error is caught by the compiler; this error can
    3.36 + * only occur at run time if the definition of a class has
    3.37 + * incompatibly changed.
    3.38 + *
    3.39 + * @author  unascribed
    3.40 + * @since   JDK1.0
    3.41 + */
    3.42 +public
    3.43 +class NoSuchFieldError extends IncompatibleClassChangeError {
    3.44 +    private static final long serialVersionUID = -3456430195886129035L;
    3.45 +
    3.46 +    /**
    3.47 +     * Constructs a <code>NoSuchFieldError</code> with no detail message.
    3.48 +     */
    3.49 +    public NoSuchFieldError() {
    3.50 +        super();
    3.51 +    }
    3.52 +
    3.53 +    /**
    3.54 +     * Constructs a <code>NoSuchFieldError</code> with the specified
    3.55 +     * detail message.
    3.56 +     *
    3.57 +     * @param   s   the detail message.
    3.58 +     */
    3.59 +    public NoSuchFieldError(String s) {
    3.60 +        super(s);
    3.61 +    }
    3.62 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/emul/compact/src/main/java/java/util/PriorityQueue.java	Fri Feb 01 16:10:10 2013 +0100
     4.3 @@ -0,0 +1,775 @@
     4.4 +/*
     4.5 + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.  Oracle designates this
    4.11 + * particular file as subject to the "Classpath" exception as provided
    4.12 + * by Oracle in the LICENSE file that accompanied this code.
    4.13 + *
    4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.17 + * version 2 for more details (a copy is included in the LICENSE file that
    4.18 + * accompanied this code).
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License version
    4.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.23 + *
    4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.25 + * or visit www.oracle.com if you need additional information or have any
    4.26 + * questions.
    4.27 + */
    4.28 +
    4.29 +package java.util;
    4.30 +
    4.31 +/**
    4.32 + * An unbounded priority {@linkplain Queue queue} based on a priority heap.
    4.33 + * The elements of the priority queue are ordered according to their
    4.34 + * {@linkplain Comparable natural ordering}, or by a {@link Comparator}
    4.35 + * provided at queue construction time, depending on which constructor is
    4.36 + * used.  A priority queue does not permit {@code null} elements.
    4.37 + * A priority queue relying on natural ordering also does not permit
    4.38 + * insertion of non-comparable objects (doing so may result in
    4.39 + * {@code ClassCastException}).
    4.40 + *
    4.41 + * <p>The <em>head</em> of this queue is the <em>least</em> element
    4.42 + * with respect to the specified ordering.  If multiple elements are
    4.43 + * tied for least value, the head is one of those elements -- ties are
    4.44 + * broken arbitrarily.  The queue retrieval operations {@code poll},
    4.45 + * {@code remove}, {@code peek}, and {@code element} access the
    4.46 + * element at the head of the queue.
    4.47 + *
    4.48 + * <p>A priority queue is unbounded, but has an internal
    4.49 + * <i>capacity</i> governing the size of an array used to store the
    4.50 + * elements on the queue.  It is always at least as large as the queue
    4.51 + * size.  As elements are added to a priority queue, its capacity
    4.52 + * grows automatically.  The details of the growth policy are not
    4.53 + * specified.
    4.54 + *
    4.55 + * <p>This class and its iterator implement all of the
    4.56 + * <em>optional</em> methods of the {@link Collection} and {@link
    4.57 + * Iterator} interfaces.  The Iterator provided in method {@link
    4.58 + * #iterator()} is <em>not</em> guaranteed to traverse the elements of
    4.59 + * the priority queue in any particular order. If you need ordered
    4.60 + * traversal, consider using {@code Arrays.sort(pq.toArray())}.
    4.61 + *
    4.62 + * <p> <strong>Note that this implementation is not synchronized.</strong>
    4.63 + * Multiple threads should not access a {@code PriorityQueue}
    4.64 + * instance concurrently if any of the threads modifies the queue.
    4.65 + * Instead, use the thread-safe {@link
    4.66 + * java.util.concurrent.PriorityBlockingQueue} class.
    4.67 + *
    4.68 + * <p>Implementation note: this implementation provides
    4.69 + * O(log(n)) time for the enqueing and dequeing methods
    4.70 + * ({@code offer}, {@code poll}, {@code remove()} and {@code add});
    4.71 + * linear time for the {@code remove(Object)} and {@code contains(Object)}
    4.72 + * methods; and constant time for the retrieval methods
    4.73 + * ({@code peek}, {@code element}, and {@code size}).
    4.74 + *
    4.75 + * <p>This class is a member of the
    4.76 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
    4.77 + * Java Collections Framework</a>.
    4.78 + *
    4.79 + * @since 1.5
    4.80 + * @author Josh Bloch, Doug Lea
    4.81 + * @param <E> the type of elements held in this collection
    4.82 + */
    4.83 +public class PriorityQueue<E> extends AbstractQueue<E>
    4.84 +    implements java.io.Serializable {
    4.85 +
    4.86 +    private static final long serialVersionUID = -7720805057305804111L;
    4.87 +
    4.88 +    private static final int DEFAULT_INITIAL_CAPACITY = 11;
    4.89 +
    4.90 +    /**
    4.91 +     * Priority queue represented as a balanced binary heap: the two
    4.92 +     * children of queue[n] are queue[2*n+1] and queue[2*(n+1)].  The
    4.93 +     * priority queue is ordered by comparator, or by the elements'
    4.94 +     * natural ordering, if comparator is null: For each node n in the
    4.95 +     * heap and each descendant d of n, n <= d.  The element with the
    4.96 +     * lowest value is in queue[0], assuming the queue is nonempty.
    4.97 +     */
    4.98 +    private transient Object[] queue;
    4.99 +
   4.100 +    /**
   4.101 +     * The number of elements in the priority queue.
   4.102 +     */
   4.103 +    private int size = 0;
   4.104 +
   4.105 +    /**
   4.106 +     * The comparator, or null if priority queue uses elements'
   4.107 +     * natural ordering.
   4.108 +     */
   4.109 +    private final Comparator<? super E> comparator;
   4.110 +
   4.111 +    /**
   4.112 +     * The number of times this priority queue has been
   4.113 +     * <i>structurally modified</i>.  See AbstractList for gory details.
   4.114 +     */
   4.115 +    private transient int modCount = 0;
   4.116 +
   4.117 +    /**
   4.118 +     * Creates a {@code PriorityQueue} with the default initial
   4.119 +     * capacity (11) that orders its elements according to their
   4.120 +     * {@linkplain Comparable natural ordering}.
   4.121 +     */
   4.122 +    public PriorityQueue() {
   4.123 +        this(DEFAULT_INITIAL_CAPACITY, null);
   4.124 +    }
   4.125 +
   4.126 +    /**
   4.127 +     * Creates a {@code PriorityQueue} with the specified initial
   4.128 +     * capacity that orders its elements according to their
   4.129 +     * {@linkplain Comparable natural ordering}.
   4.130 +     *
   4.131 +     * @param initialCapacity the initial capacity for this priority queue
   4.132 +     * @throws IllegalArgumentException if {@code initialCapacity} is less
   4.133 +     *         than 1
   4.134 +     */
   4.135 +    public PriorityQueue(int initialCapacity) {
   4.136 +        this(initialCapacity, null);
   4.137 +    }
   4.138 +
   4.139 +    /**
   4.140 +     * Creates a {@code PriorityQueue} with the specified initial capacity
   4.141 +     * that orders its elements according to the specified comparator.
   4.142 +     *
   4.143 +     * @param  initialCapacity the initial capacity for this priority queue
   4.144 +     * @param  comparator the comparator that will be used to order this
   4.145 +     *         priority queue.  If {@code null}, the {@linkplain Comparable
   4.146 +     *         natural ordering} of the elements will be used.
   4.147 +     * @throws IllegalArgumentException if {@code initialCapacity} is
   4.148 +     *         less than 1
   4.149 +     */
   4.150 +    public PriorityQueue(int initialCapacity,
   4.151 +                         Comparator<? super E> comparator) {
   4.152 +        // Note: This restriction of at least one is not actually needed,
   4.153 +        // but continues for 1.5 compatibility
   4.154 +        if (initialCapacity < 1)
   4.155 +            throw new IllegalArgumentException();
   4.156 +        this.queue = new Object[initialCapacity];
   4.157 +        this.comparator = comparator;
   4.158 +    }
   4.159 +
   4.160 +    /**
   4.161 +     * Creates a {@code PriorityQueue} containing the elements in the
   4.162 +     * specified collection.  If the specified collection is an instance of
   4.163 +     * a {@link SortedSet} or is another {@code PriorityQueue}, this
   4.164 +     * priority queue will be ordered according to the same ordering.
   4.165 +     * Otherwise, this priority queue will be ordered according to the
   4.166 +     * {@linkplain Comparable natural ordering} of its elements.
   4.167 +     *
   4.168 +     * @param  c the collection whose elements are to be placed
   4.169 +     *         into this priority queue
   4.170 +     * @throws ClassCastException if elements of the specified collection
   4.171 +     *         cannot be compared to one another according to the priority
   4.172 +     *         queue's ordering
   4.173 +     * @throws NullPointerException if the specified collection or any
   4.174 +     *         of its elements are null
   4.175 +     */
   4.176 +    @SuppressWarnings("unchecked")
   4.177 +    public PriorityQueue(Collection<? extends E> c) {
   4.178 +        if (c instanceof SortedSet<?>) {
   4.179 +            SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
   4.180 +            this.comparator = (Comparator<? super E>) ss.comparator();
   4.181 +            initElementsFromCollection(ss);
   4.182 +        }
   4.183 +        else if (c instanceof PriorityQueue<?>) {
   4.184 +            PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
   4.185 +            this.comparator = (Comparator<? super E>) pq.comparator();
   4.186 +            initFromPriorityQueue(pq);
   4.187 +        }
   4.188 +        else {
   4.189 +            this.comparator = null;
   4.190 +            initFromCollection(c);
   4.191 +        }
   4.192 +    }
   4.193 +
   4.194 +    /**
   4.195 +     * Creates a {@code PriorityQueue} containing the elements in the
   4.196 +     * specified priority queue.  This priority queue will be
   4.197 +     * ordered according to the same ordering as the given priority
   4.198 +     * queue.
   4.199 +     *
   4.200 +     * @param  c the priority queue whose elements are to be placed
   4.201 +     *         into this priority queue
   4.202 +     * @throws ClassCastException if elements of {@code c} cannot be
   4.203 +     *         compared to one another according to {@code c}'s
   4.204 +     *         ordering
   4.205 +     * @throws NullPointerException if the specified priority queue or any
   4.206 +     *         of its elements are null
   4.207 +     */
   4.208 +    @SuppressWarnings("unchecked")
   4.209 +    public PriorityQueue(PriorityQueue<? extends E> c) {
   4.210 +        this.comparator = (Comparator<? super E>) c.comparator();
   4.211 +        initFromPriorityQueue(c);
   4.212 +    }
   4.213 +
   4.214 +    /**
   4.215 +     * Creates a {@code PriorityQueue} containing the elements in the
   4.216 +     * specified sorted set.   This priority queue will be ordered
   4.217 +     * according to the same ordering as the given sorted set.
   4.218 +     *
   4.219 +     * @param  c the sorted set whose elements are to be placed
   4.220 +     *         into this priority queue
   4.221 +     * @throws ClassCastException if elements of the specified sorted
   4.222 +     *         set cannot be compared to one another according to the
   4.223 +     *         sorted set's ordering
   4.224 +     * @throws NullPointerException if the specified sorted set or any
   4.225 +     *         of its elements are null
   4.226 +     */
   4.227 +    @SuppressWarnings("unchecked")
   4.228 +    public PriorityQueue(SortedSet<? extends E> c) {
   4.229 +        this.comparator = (Comparator<? super E>) c.comparator();
   4.230 +        initElementsFromCollection(c);
   4.231 +    }
   4.232 +
   4.233 +    private void initFromPriorityQueue(PriorityQueue<? extends E> c) {
   4.234 +        if (c.getClass() == PriorityQueue.class) {
   4.235 +            this.queue = c.toArray();
   4.236 +            this.size = c.size();
   4.237 +        } else {
   4.238 +            initFromCollection(c);
   4.239 +        }
   4.240 +    }
   4.241 +
   4.242 +    private void initElementsFromCollection(Collection<? extends E> c) {
   4.243 +        Object[] a = c.toArray();
   4.244 +        // If c.toArray incorrectly doesn't return Object[], copy it.
   4.245 +        if (a.getClass() != Object[].class)
   4.246 +            a = Arrays.copyOf(a, a.length, Object[].class);
   4.247 +        int len = a.length;
   4.248 +        if (len == 1 || this.comparator != null)
   4.249 +            for (int i = 0; i < len; i++)
   4.250 +                if (a[i] == null)
   4.251 +                    throw new NullPointerException();
   4.252 +        this.queue = a;
   4.253 +        this.size = a.length;
   4.254 +    }
   4.255 +
   4.256 +    /**
   4.257 +     * Initializes queue array with elements from the given Collection.
   4.258 +     *
   4.259 +     * @param c the collection
   4.260 +     */
   4.261 +    private void initFromCollection(Collection<? extends E> c) {
   4.262 +        initElementsFromCollection(c);
   4.263 +        heapify();
   4.264 +    }
   4.265 +
   4.266 +    /**
   4.267 +     * The maximum size of array to allocate.
   4.268 +     * Some VMs reserve some header words in an array.
   4.269 +     * Attempts to allocate larger arrays may result in
   4.270 +     * OutOfMemoryError: Requested array size exceeds VM limit
   4.271 +     */
   4.272 +    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
   4.273 +
   4.274 +    /**
   4.275 +     * Increases the capacity of the array.
   4.276 +     *
   4.277 +     * @param minCapacity the desired minimum capacity
   4.278 +     */
   4.279 +    private void grow(int minCapacity) {
   4.280 +        int oldCapacity = queue.length;
   4.281 +        // Double size if small; else grow by 50%
   4.282 +        int newCapacity = oldCapacity + ((oldCapacity < 64) ?
   4.283 +                                         (oldCapacity + 2) :
   4.284 +                                         (oldCapacity >> 1));
   4.285 +        // overflow-conscious code
   4.286 +        if (newCapacity - MAX_ARRAY_SIZE > 0)
   4.287 +            newCapacity = hugeCapacity(minCapacity);
   4.288 +        queue = Arrays.copyOf(queue, newCapacity);
   4.289 +    }
   4.290 +
   4.291 +    private static int hugeCapacity(int minCapacity) {
   4.292 +        if (minCapacity < 0) // overflow
   4.293 +            throw new OutOfMemoryError();
   4.294 +        return (minCapacity > MAX_ARRAY_SIZE) ?
   4.295 +            Integer.MAX_VALUE :
   4.296 +            MAX_ARRAY_SIZE;
   4.297 +    }
   4.298 +
   4.299 +    /**
   4.300 +     * Inserts the specified element into this priority queue.
   4.301 +     *
   4.302 +     * @return {@code true} (as specified by {@link Collection#add})
   4.303 +     * @throws ClassCastException if the specified element cannot be
   4.304 +     *         compared with elements currently in this priority queue
   4.305 +     *         according to the priority queue's ordering
   4.306 +     * @throws NullPointerException if the specified element is null
   4.307 +     */
   4.308 +    public boolean add(E e) {
   4.309 +        return offer(e);
   4.310 +    }
   4.311 +
   4.312 +    /**
   4.313 +     * Inserts the specified element into this priority queue.
   4.314 +     *
   4.315 +     * @return {@code true} (as specified by {@link Queue#offer})
   4.316 +     * @throws ClassCastException if the specified element cannot be
   4.317 +     *         compared with elements currently in this priority queue
   4.318 +     *         according to the priority queue's ordering
   4.319 +     * @throws NullPointerException if the specified element is null
   4.320 +     */
   4.321 +    public boolean offer(E e) {
   4.322 +        if (e == null)
   4.323 +            throw new NullPointerException();
   4.324 +        modCount++;
   4.325 +        int i = size;
   4.326 +        if (i >= queue.length)
   4.327 +            grow(i + 1);
   4.328 +        size = i + 1;
   4.329 +        if (i == 0)
   4.330 +            queue[0] = e;
   4.331 +        else
   4.332 +            siftUp(i, e);
   4.333 +        return true;
   4.334 +    }
   4.335 +
   4.336 +    public E peek() {
   4.337 +        if (size == 0)
   4.338 +            return null;
   4.339 +        return (E) queue[0];
   4.340 +    }
   4.341 +
   4.342 +    private int indexOf(Object o) {
   4.343 +        if (o != null) {
   4.344 +            for (int i = 0; i < size; i++)
   4.345 +                if (o.equals(queue[i]))
   4.346 +                    return i;
   4.347 +        }
   4.348 +        return -1;
   4.349 +    }
   4.350 +
   4.351 +    /**
   4.352 +     * Removes a single instance of the specified element from this queue,
   4.353 +     * if it is present.  More formally, removes an element {@code e} such
   4.354 +     * that {@code o.equals(e)}, if this queue contains one or more such
   4.355 +     * elements.  Returns {@code true} if and only if this queue contained
   4.356 +     * the specified element (or equivalently, if this queue changed as a
   4.357 +     * result of the call).
   4.358 +     *
   4.359 +     * @param o element to be removed from this queue, if present
   4.360 +     * @return {@code true} if this queue changed as a result of the call
   4.361 +     */
   4.362 +    public boolean remove(Object o) {
   4.363 +        int i = indexOf(o);
   4.364 +        if (i == -1)
   4.365 +            return false;
   4.366 +        else {
   4.367 +            removeAt(i);
   4.368 +            return true;
   4.369 +        }
   4.370 +    }
   4.371 +
   4.372 +    /**
   4.373 +     * Version of remove using reference equality, not equals.
   4.374 +     * Needed by iterator.remove.
   4.375 +     *
   4.376 +     * @param o element to be removed from this queue, if present
   4.377 +     * @return {@code true} if removed
   4.378 +     */
   4.379 +    boolean removeEq(Object o) {
   4.380 +        for (int i = 0; i < size; i++) {
   4.381 +            if (o == queue[i]) {
   4.382 +                removeAt(i);
   4.383 +                return true;
   4.384 +            }
   4.385 +        }
   4.386 +        return false;
   4.387 +    }
   4.388 +
   4.389 +    /**
   4.390 +     * Returns {@code true} if this queue contains the specified element.
   4.391 +     * More formally, returns {@code true} if and only if this queue contains
   4.392 +     * at least one element {@code e} such that {@code o.equals(e)}.
   4.393 +     *
   4.394 +     * @param o object to be checked for containment in this queue
   4.395 +     * @return {@code true} if this queue contains the specified element
   4.396 +     */
   4.397 +    public boolean contains(Object o) {
   4.398 +        return indexOf(o) != -1;
   4.399 +    }
   4.400 +
   4.401 +    /**
   4.402 +     * Returns an array containing all of the elements in this queue.
   4.403 +     * The elements are in no particular order.
   4.404 +     *
   4.405 +     * <p>The returned array will be "safe" in that no references to it are
   4.406 +     * maintained by this queue.  (In other words, this method must allocate
   4.407 +     * a new array).  The caller is thus free to modify the returned array.
   4.408 +     *
   4.409 +     * <p>This method acts as bridge between array-based and collection-based
   4.410 +     * APIs.
   4.411 +     *
   4.412 +     * @return an array containing all of the elements in this queue
   4.413 +     */
   4.414 +    public Object[] toArray() {
   4.415 +        return Arrays.copyOf(queue, size);
   4.416 +    }
   4.417 +
   4.418 +    /**
   4.419 +     * Returns an array containing all of the elements in this queue; the
   4.420 +     * runtime type of the returned array is that of the specified array.
   4.421 +     * The returned array elements are in no particular order.
   4.422 +     * If the queue fits in the specified array, it is returned therein.
   4.423 +     * Otherwise, a new array is allocated with the runtime type of the
   4.424 +     * specified array and the size of this queue.
   4.425 +     *
   4.426 +     * <p>If the queue fits in the specified array with room to spare
   4.427 +     * (i.e., the array has more elements than the queue), the element in
   4.428 +     * the array immediately following the end of the collection is set to
   4.429 +     * {@code null}.
   4.430 +     *
   4.431 +     * <p>Like the {@link #toArray()} method, this method acts as bridge between
   4.432 +     * array-based and collection-based APIs.  Further, this method allows
   4.433 +     * precise control over the runtime type of the output array, and may,
   4.434 +     * under certain circumstances, be used to save allocation costs.
   4.435 +     *
   4.436 +     * <p>Suppose <tt>x</tt> is a queue known to contain only strings.
   4.437 +     * The following code can be used to dump the queue into a newly
   4.438 +     * allocated array of <tt>String</tt>:
   4.439 +     *
   4.440 +     * <pre>
   4.441 +     *     String[] y = x.toArray(new String[0]);</pre>
   4.442 +     *
   4.443 +     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
   4.444 +     * <tt>toArray()</tt>.
   4.445 +     *
   4.446 +     * @param a the array into which the elements of the queue are to
   4.447 +     *          be stored, if it is big enough; otherwise, a new array of the
   4.448 +     *          same runtime type is allocated for this purpose.
   4.449 +     * @return an array containing all of the elements in this queue
   4.450 +     * @throws ArrayStoreException if the runtime type of the specified array
   4.451 +     *         is not a supertype of the runtime type of every element in
   4.452 +     *         this queue
   4.453 +     * @throws NullPointerException if the specified array is null
   4.454 +     */
   4.455 +    public <T> T[] toArray(T[] a) {
   4.456 +        if (a.length < size)
   4.457 +            // Make a new array of a's runtime type, but my contents:
   4.458 +            return (T[]) Arrays.copyOf(queue, size, a.getClass());
   4.459 +        System.arraycopy(queue, 0, a, 0, size);
   4.460 +        if (a.length > size)
   4.461 +            a[size] = null;
   4.462 +        return a;
   4.463 +    }
   4.464 +
   4.465 +    /**
   4.466 +     * Returns an iterator over the elements in this queue. The iterator
   4.467 +     * does not return the elements in any particular order.
   4.468 +     *
   4.469 +     * @return an iterator over the elements in this queue
   4.470 +     */
   4.471 +    public Iterator<E> iterator() {
   4.472 +        return new Itr();
   4.473 +    }
   4.474 +
   4.475 +    private final class Itr implements Iterator<E> {
   4.476 +        /**
   4.477 +         * Index (into queue array) of element to be returned by
   4.478 +         * subsequent call to next.
   4.479 +         */
   4.480 +        private int cursor = 0;
   4.481 +
   4.482 +        /**
   4.483 +         * Index of element returned by most recent call to next,
   4.484 +         * unless that element came from the forgetMeNot list.
   4.485 +         * Set to -1 if element is deleted by a call to remove.
   4.486 +         */
   4.487 +        private int lastRet = -1;
   4.488 +
   4.489 +        /**
   4.490 +         * A queue of elements that were moved from the unvisited portion of
   4.491 +         * the heap into the visited portion as a result of "unlucky" element
   4.492 +         * removals during the iteration.  (Unlucky element removals are those
   4.493 +         * that require a siftup instead of a siftdown.)  We must visit all of
   4.494 +         * the elements in this list to complete the iteration.  We do this
   4.495 +         * after we've completed the "normal" iteration.
   4.496 +         *
   4.497 +         * We expect that most iterations, even those involving removals,
   4.498 +         * will not need to store elements in this field.
   4.499 +         */
   4.500 +        private ArrayDeque<E> forgetMeNot = null;
   4.501 +
   4.502 +        /**
   4.503 +         * Element returned by the most recent call to next iff that
   4.504 +         * element was drawn from the forgetMeNot list.
   4.505 +         */
   4.506 +        private E lastRetElt = null;
   4.507 +
   4.508 +        /**
   4.509 +         * The modCount value that the iterator believes that the backing
   4.510 +         * Queue should have.  If this expectation is violated, the iterator
   4.511 +         * has detected concurrent modification.
   4.512 +         */
   4.513 +        private int expectedModCount = modCount;
   4.514 +
   4.515 +        public boolean hasNext() {
   4.516 +            return cursor < size ||
   4.517 +                (forgetMeNot != null && !forgetMeNot.isEmpty());
   4.518 +        }
   4.519 +
   4.520 +        public E next() {
   4.521 +            if (expectedModCount != modCount)
   4.522 +                throw new ConcurrentModificationException();
   4.523 +            if (cursor < size)
   4.524 +                return (E) queue[lastRet = cursor++];
   4.525 +            if (forgetMeNot != null) {
   4.526 +                lastRet = -1;
   4.527 +                lastRetElt = forgetMeNot.poll();
   4.528 +                if (lastRetElt != null)
   4.529 +                    return lastRetElt;
   4.530 +            }
   4.531 +            throw new NoSuchElementException();
   4.532 +        }
   4.533 +
   4.534 +        public void remove() {
   4.535 +            if (expectedModCount != modCount)
   4.536 +                throw new ConcurrentModificationException();
   4.537 +            if (lastRet != -1) {
   4.538 +                E moved = PriorityQueue.this.removeAt(lastRet);
   4.539 +                lastRet = -1;
   4.540 +                if (moved == null)
   4.541 +                    cursor--;
   4.542 +                else {
   4.543 +                    if (forgetMeNot == null)
   4.544 +                        forgetMeNot = new ArrayDeque<>();
   4.545 +                    forgetMeNot.add(moved);
   4.546 +                }
   4.547 +            } else if (lastRetElt != null) {
   4.548 +                PriorityQueue.this.removeEq(lastRetElt);
   4.549 +                lastRetElt = null;
   4.550 +            } else {
   4.551 +                throw new IllegalStateException();
   4.552 +            }
   4.553 +            expectedModCount = modCount;
   4.554 +        }
   4.555 +    }
   4.556 +
   4.557 +    public int size() {
   4.558 +        return size;
   4.559 +    }
   4.560 +
   4.561 +    /**
   4.562 +     * Removes all of the elements from this priority queue.
   4.563 +     * The queue will be empty after this call returns.
   4.564 +     */
   4.565 +    public void clear() {
   4.566 +        modCount++;
   4.567 +        for (int i = 0; i < size; i++)
   4.568 +            queue[i] = null;
   4.569 +        size = 0;
   4.570 +    }
   4.571 +
   4.572 +    public E poll() {
   4.573 +        if (size == 0)
   4.574 +            return null;
   4.575 +        int s = --size;
   4.576 +        modCount++;
   4.577 +        E result = (E) queue[0];
   4.578 +        E x = (E) queue[s];
   4.579 +        queue[s] = null;
   4.580 +        if (s != 0)
   4.581 +            siftDown(0, x);
   4.582 +        return result;
   4.583 +    }
   4.584 +
   4.585 +    /**
   4.586 +     * Removes the ith element from queue.
   4.587 +     *
   4.588 +     * Normally this method leaves the elements at up to i-1,
   4.589 +     * inclusive, untouched.  Under these circumstances, it returns
   4.590 +     * null.  Occasionally, in order to maintain the heap invariant,
   4.591 +     * it must swap a later element of the list with one earlier than
   4.592 +     * i.  Under these circumstances, this method returns the element
   4.593 +     * that was previously at the end of the list and is now at some
   4.594 +     * position before i. This fact is used by iterator.remove so as to
   4.595 +     * avoid missing traversing elements.
   4.596 +     */
   4.597 +    private E removeAt(int i) {
   4.598 +        assert i >= 0 && i < size;
   4.599 +        modCount++;
   4.600 +        int s = --size;
   4.601 +        if (s == i) // removed last element
   4.602 +            queue[i] = null;
   4.603 +        else {
   4.604 +            E moved = (E) queue[s];
   4.605 +            queue[s] = null;
   4.606 +            siftDown(i, moved);
   4.607 +            if (queue[i] == moved) {
   4.608 +                siftUp(i, moved);
   4.609 +                if (queue[i] != moved)
   4.610 +                    return moved;
   4.611 +            }
   4.612 +        }
   4.613 +        return null;
   4.614 +    }
   4.615 +
   4.616 +    /**
   4.617 +     * Inserts item x at position k, maintaining heap invariant by
   4.618 +     * promoting x up the tree until it is greater than or equal to
   4.619 +     * its parent, or is the root.
   4.620 +     *
   4.621 +     * To simplify and speed up coercions and comparisons. the
   4.622 +     * Comparable and Comparator versions are separated into different
   4.623 +     * methods that are otherwise identical. (Similarly for siftDown.)
   4.624 +     *
   4.625 +     * @param k the position to fill
   4.626 +     * @param x the item to insert
   4.627 +     */
   4.628 +    private void siftUp(int k, E x) {
   4.629 +        if (comparator != null)
   4.630 +            siftUpUsingComparator(k, x);
   4.631 +        else
   4.632 +            siftUpComparable(k, x);
   4.633 +    }
   4.634 +
   4.635 +    private void siftUpComparable(int k, E x) {
   4.636 +        Comparable<? super E> key = (Comparable<? super E>) x;
   4.637 +        while (k > 0) {
   4.638 +            int parent = (k - 1) >>> 1;
   4.639 +            Object e = queue[parent];
   4.640 +            if (key.compareTo((E) e) >= 0)
   4.641 +                break;
   4.642 +            queue[k] = e;
   4.643 +            k = parent;
   4.644 +        }
   4.645 +        queue[k] = key;
   4.646 +    }
   4.647 +
   4.648 +    private void siftUpUsingComparator(int k, E x) {
   4.649 +        while (k > 0) {
   4.650 +            int parent = (k - 1) >>> 1;
   4.651 +            Object e = queue[parent];
   4.652 +            if (comparator.compare(x, (E) e) >= 0)
   4.653 +                break;
   4.654 +            queue[k] = e;
   4.655 +            k = parent;
   4.656 +        }
   4.657 +        queue[k] = x;
   4.658 +    }
   4.659 +
   4.660 +    /**
   4.661 +     * Inserts item x at position k, maintaining heap invariant by
   4.662 +     * demoting x down the tree repeatedly until it is less than or
   4.663 +     * equal to its children or is a leaf.
   4.664 +     *
   4.665 +     * @param k the position to fill
   4.666 +     * @param x the item to insert
   4.667 +     */
   4.668 +    private void siftDown(int k, E x) {
   4.669 +        if (comparator != null)
   4.670 +            siftDownUsingComparator(k, x);
   4.671 +        else
   4.672 +            siftDownComparable(k, x);
   4.673 +    }
   4.674 +
   4.675 +    private void siftDownComparable(int k, E x) {
   4.676 +        Comparable<? super E> key = (Comparable<? super E>)x;
   4.677 +        int half = size >>> 1;        // loop while a non-leaf
   4.678 +        while (k < half) {
   4.679 +            int child = (k << 1) + 1; // assume left child is least
   4.680 +            Object c = queue[child];
   4.681 +            int right = child + 1;
   4.682 +            if (right < size &&
   4.683 +                ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
   4.684 +                c = queue[child = right];
   4.685 +            if (key.compareTo((E) c) <= 0)
   4.686 +                break;
   4.687 +            queue[k] = c;
   4.688 +            k = child;
   4.689 +        }
   4.690 +        queue[k] = key;
   4.691 +    }
   4.692 +
   4.693 +    private void siftDownUsingComparator(int k, E x) {
   4.694 +        int half = size >>> 1;
   4.695 +        while (k < half) {
   4.696 +            int child = (k << 1) + 1;
   4.697 +            Object c = queue[child];
   4.698 +            int right = child + 1;
   4.699 +            if (right < size &&
   4.700 +                comparator.compare((E) c, (E) queue[right]) > 0)
   4.701 +                c = queue[child = right];
   4.702 +            if (comparator.compare(x, (E) c) <= 0)
   4.703 +                break;
   4.704 +            queue[k] = c;
   4.705 +            k = child;
   4.706 +        }
   4.707 +        queue[k] = x;
   4.708 +    }
   4.709 +
   4.710 +    /**
   4.711 +     * Establishes the heap invariant (described above) in the entire tree,
   4.712 +     * assuming nothing about the order of the elements prior to the call.
   4.713 +     */
   4.714 +    private void heapify() {
   4.715 +        for (int i = (size >>> 1) - 1; i >= 0; i--)
   4.716 +            siftDown(i, (E) queue[i]);
   4.717 +    }
   4.718 +
   4.719 +    /**
   4.720 +     * Returns the comparator used to order the elements in this
   4.721 +     * queue, or {@code null} if this queue is sorted according to
   4.722 +     * the {@linkplain Comparable natural ordering} of its elements.
   4.723 +     *
   4.724 +     * @return the comparator used to order this queue, or
   4.725 +     *         {@code null} if this queue is sorted according to the
   4.726 +     *         natural ordering of its elements
   4.727 +     */
   4.728 +    public Comparator<? super E> comparator() {
   4.729 +        return comparator;
   4.730 +    }
   4.731 +
   4.732 +    /**
   4.733 +     * Saves the state of the instance to a stream (that
   4.734 +     * is, serializes it).
   4.735 +     *
   4.736 +     * @serialData The length of the array backing the instance is
   4.737 +     *             emitted (int), followed by all of its elements
   4.738 +     *             (each an {@code Object}) in the proper order.
   4.739 +     * @param s the stream
   4.740 +     */
   4.741 +    private void writeObject(java.io.ObjectOutputStream s)
   4.742 +        throws java.io.IOException{
   4.743 +        // Write out element count, and any hidden stuff
   4.744 +        s.defaultWriteObject();
   4.745 +
   4.746 +        // Write out array length, for compatibility with 1.5 version
   4.747 +        s.writeInt(Math.max(2, size + 1));
   4.748 +
   4.749 +        // Write out all elements in the "proper order".
   4.750 +        for (int i = 0; i < size; i++)
   4.751 +            s.writeObject(queue[i]);
   4.752 +    }
   4.753 +
   4.754 +    /**
   4.755 +     * Reconstitutes the {@code PriorityQueue} instance from a stream
   4.756 +     * (that is, deserializes it).
   4.757 +     *
   4.758 +     * @param s the stream
   4.759 +     */
   4.760 +    private void readObject(java.io.ObjectInputStream s)
   4.761 +        throws java.io.IOException, ClassNotFoundException {
   4.762 +        // Read in size, and any hidden stuff
   4.763 +        s.defaultReadObject();
   4.764 +
   4.765 +        // Read in (and discard) array length
   4.766 +        s.readInt();
   4.767 +
   4.768 +        queue = new Object[size];
   4.769 +
   4.770 +        // Read in all elements.
   4.771 +        for (int i = 0; i < size; i++)
   4.772 +            queue[i] = s.readObject();
   4.773 +
   4.774 +        // Elements are guaranteed to be in "proper order", but the
   4.775 +        // spec has never explained what that might be.
   4.776 +        heapify();
   4.777 +    }
   4.778 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/emul/compact/src/main/java/java/util/concurrent/TimeUnit.java	Fri Feb 01 16:10:10 2013 +0100
     5.3 @@ -0,0 +1,364 @@
     5.4 +/*
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.6 + *
     5.7 + * This code is free software; you can redistribute it and/or modify it
     5.8 + * under the terms of the GNU General Public License version 2 only, as
     5.9 + * published by the Free Software Foundation.  Oracle designates this
    5.10 + * particular file as subject to the "Classpath" exception as provided
    5.11 + * by Oracle in the LICENSE file that accompanied this code.
    5.12 + *
    5.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.16 + * version 2 for more details (a copy is included in the LICENSE file that
    5.17 + * accompanied this code).
    5.18 + *
    5.19 + * You should have received a copy of the GNU General Public License version
    5.20 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    5.22 + *
    5.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    5.24 + * or visit www.oracle.com if you need additional information or have any
    5.25 + * questions.
    5.26 + */
    5.27 +
    5.28 +/*
    5.29 + * This file is available under and governed by the GNU General Public
    5.30 + * License version 2 only, as published by the Free Software Foundation.
    5.31 + * However, the following notice accompanied the original version of this
    5.32 + * file:
    5.33 + *
    5.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
    5.35 + * Expert Group and released to the public domain, as explained at
    5.36 + * http://creativecommons.org/publicdomain/zero/1.0/
    5.37 + */
    5.38 +
    5.39 +package java.util.concurrent;
    5.40 +
    5.41 +/**
    5.42 + * A <tt>TimeUnit</tt> represents time durations at a given unit of
    5.43 + * granularity and provides utility methods to convert across units,
    5.44 + * and to perform timing and delay operations in these units.  A
    5.45 + * <tt>TimeUnit</tt> does not maintain time information, but only
    5.46 + * helps organize and use time representations that may be maintained
    5.47 + * separately across various contexts.  A nanosecond is defined as one
    5.48 + * thousandth of a microsecond, a microsecond as one thousandth of a
    5.49 + * millisecond, a millisecond as one thousandth of a second, a minute
    5.50 + * as sixty seconds, an hour as sixty minutes, and a day as twenty four
    5.51 + * hours.
    5.52 + *
    5.53 + * <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods
    5.54 + * how a given timing parameter should be interpreted. For example,
    5.55 + * the following code will timeout in 50 milliseconds if the {@link
    5.56 + * java.util.concurrent.locks.Lock lock} is not available:
    5.57 + *
    5.58 + * <pre>  Lock lock = ...;
    5.59 + *  if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
    5.60 + * </pre>
    5.61 + * while this code will timeout in 50 seconds:
    5.62 + * <pre>
    5.63 + *  Lock lock = ...;
    5.64 + *  if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
    5.65 + * </pre>
    5.66 + *
    5.67 + * Note however, that there is no guarantee that a particular timeout
    5.68 + * implementation will be able to notice the passage of time at the
    5.69 + * same granularity as the given <tt>TimeUnit</tt>.
    5.70 + *
    5.71 + * @since 1.5
    5.72 + * @author Doug Lea
    5.73 + */
    5.74 +public enum TimeUnit {
    5.75 +    NANOSECONDS {
    5.76 +        public long toNanos(long d)   { return d; }
    5.77 +        public long toMicros(long d)  { return d/(C1/C0); }
    5.78 +        public long toMillis(long d)  { return d/(C2/C0); }
    5.79 +        public long toSeconds(long d) { return d/(C3/C0); }
    5.80 +        public long toMinutes(long d) { return d/(C4/C0); }
    5.81 +        public long toHours(long d)   { return d/(C5/C0); }
    5.82 +        public long toDays(long d)    { return d/(C6/C0); }
    5.83 +        public long convert(long d, TimeUnit u) { return u.toNanos(d); }
    5.84 +        int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
    5.85 +    },
    5.86 +    MICROSECONDS {
    5.87 +        public long toNanos(long d)   { return x(d, C1/C0, MAX/(C1/C0)); }
    5.88 +        public long toMicros(long d)  { return d; }
    5.89 +        public long toMillis(long d)  { return d/(C2/C1); }
    5.90 +        public long toSeconds(long d) { return d/(C3/C1); }
    5.91 +        public long toMinutes(long d) { return d/(C4/C1); }
    5.92 +        public long toHours(long d)   { return d/(C5/C1); }
    5.93 +        public long toDays(long d)    { return d/(C6/C1); }
    5.94 +        public long convert(long d, TimeUnit u) { return u.toMicros(d); }
    5.95 +        int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
    5.96 +    },
    5.97 +    MILLISECONDS {
    5.98 +        public long toNanos(long d)   { return x(d, C2/C0, MAX/(C2/C0)); }
    5.99 +        public long toMicros(long d)  { return x(d, C2/C1, MAX/(C2/C1)); }
   5.100 +        public long toMillis(long d)  { return d; }
   5.101 +        public long toSeconds(long d) { return d/(C3/C2); }
   5.102 +        public long toMinutes(long d) { return d/(C4/C2); }
   5.103 +        public long toHours(long d)   { return d/(C5/C2); }
   5.104 +        public long toDays(long d)    { return d/(C6/C2); }
   5.105 +        public long convert(long d, TimeUnit u) { return u.toMillis(d); }
   5.106 +        int excessNanos(long d, long m) { return 0; }
   5.107 +    },
   5.108 +    SECONDS {
   5.109 +        public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
   5.110 +        public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
   5.111 +        public long toMillis(long d)  { return x(d, C3/C2, MAX/(C3/C2)); }
   5.112 +        public long toSeconds(long d) { return d; }
   5.113 +        public long toMinutes(long d) { return d/(C4/C3); }
   5.114 +        public long toHours(long d)   { return d/(C5/C3); }
   5.115 +        public long toDays(long d)    { return d/(C6/C3); }
   5.116 +        public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
   5.117 +        int excessNanos(long d, long m) { return 0; }
   5.118 +    },
   5.119 +    MINUTES {
   5.120 +        public long toNanos(long d)   { return x(d, C4/C0, MAX/(C4/C0)); }
   5.121 +        public long toMicros(long d)  { return x(d, C4/C1, MAX/(C4/C1)); }
   5.122 +        public long toMillis(long d)  { return x(d, C4/C2, MAX/(C4/C2)); }
   5.123 +        public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
   5.124 +        public long toMinutes(long d) { return d; }
   5.125 +        public long toHours(long d)   { return d/(C5/C4); }
   5.126 +        public long toDays(long d)    { return d/(C6/C4); }
   5.127 +        public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
   5.128 +        int excessNanos(long d, long m) { return 0; }
   5.129 +    },
   5.130 +    HOURS {
   5.131 +        public long toNanos(long d)   { return x(d, C5/C0, MAX/(C5/C0)); }
   5.132 +        public long toMicros(long d)  { return x(d, C5/C1, MAX/(C5/C1)); }
   5.133 +        public long toMillis(long d)  { return x(d, C5/C2, MAX/(C5/C2)); }
   5.134 +        public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
   5.135 +        public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
   5.136 +        public long toHours(long d)   { return d; }
   5.137 +        public long toDays(long d)    { return d/(C6/C5); }
   5.138 +        public long convert(long d, TimeUnit u) { return u.toHours(d); }
   5.139 +        int excessNanos(long d, long m) { return 0; }
   5.140 +    },
   5.141 +    DAYS {
   5.142 +        public long toNanos(long d)   { return x(d, C6/C0, MAX/(C6/C0)); }
   5.143 +        public long toMicros(long d)  { return x(d, C6/C1, MAX/(C6/C1)); }
   5.144 +        public long toMillis(long d)  { return x(d, C6/C2, MAX/(C6/C2)); }
   5.145 +        public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
   5.146 +        public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
   5.147 +        public long toHours(long d)   { return x(d, C6/C5, MAX/(C6/C5)); }
   5.148 +        public long toDays(long d)    { return d; }
   5.149 +        public long convert(long d, TimeUnit u) { return u.toDays(d); }
   5.150 +        int excessNanos(long d, long m) { return 0; }
   5.151 +    };
   5.152 +
   5.153 +    // Handy constants for conversion methods
   5.154 +    static final long C0 = 1L;
   5.155 +    static final long C1 = C0 * 1000L;
   5.156 +    static final long C2 = C1 * 1000L;
   5.157 +    static final long C3 = C2 * 1000L;
   5.158 +    static final long C4 = C3 * 60L;
   5.159 +    static final long C5 = C4 * 60L;
   5.160 +    static final long C6 = C5 * 24L;
   5.161 +
   5.162 +    static final long MAX = Long.MAX_VALUE;
   5.163 +
   5.164 +    /**
   5.165 +     * Scale d by m, checking for overflow.
   5.166 +     * This has a short name to make above code more readable.
   5.167 +     */
   5.168 +    static long x(long d, long m, long over) {
   5.169 +        if (d >  over) return Long.MAX_VALUE;
   5.170 +        if (d < -over) return Long.MIN_VALUE;
   5.171 +        return d * m;
   5.172 +    }
   5.173 +
   5.174 +    // To maintain full signature compatibility with 1.5, and to improve the
   5.175 +    // clarity of the generated javadoc (see 6287639: Abstract methods in
   5.176 +    // enum classes should not be listed as abstract), method convert
   5.177 +    // etc. are not declared abstract but otherwise act as abstract methods.
   5.178 +
   5.179 +    /**
   5.180 +     * Convert the given time duration in the given unit to this
   5.181 +     * unit.  Conversions from finer to coarser granularities
   5.182 +     * truncate, so lose precision. For example converting
   5.183 +     * <tt>999</tt> milliseconds to seconds results in
   5.184 +     * <tt>0</tt>. Conversions from coarser to finer granularities
   5.185 +     * with arguments that would numerically overflow saturate to
   5.186 +     * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
   5.187 +     * if positive.
   5.188 +     *
   5.189 +     * <p>For example, to convert 10 minutes to milliseconds, use:
   5.190 +     * <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt>
   5.191 +     *
   5.192 +     * @param sourceDuration the time duration in the given <tt>sourceUnit</tt>
   5.193 +     * @param sourceUnit the unit of the <tt>sourceDuration</tt> argument
   5.194 +     * @return the converted duration in this unit,
   5.195 +     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
   5.196 +     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
   5.197 +     */
   5.198 +    public long convert(long sourceDuration, TimeUnit sourceUnit) {
   5.199 +        throw new AbstractMethodError();
   5.200 +    }
   5.201 +
   5.202 +    /**
   5.203 +     * Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
   5.204 +     * @param duration the duration
   5.205 +     * @return the converted duration,
   5.206 +     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
   5.207 +     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
   5.208 +     * @see #convert
   5.209 +     */
   5.210 +    public long toNanos(long duration) {
   5.211 +        throw new AbstractMethodError();
   5.212 +    }
   5.213 +
   5.214 +    /**
   5.215 +     * Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
   5.216 +     * @param duration the duration
   5.217 +     * @return the converted duration,
   5.218 +     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
   5.219 +     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
   5.220 +     * @see #convert
   5.221 +     */
   5.222 +    public long toMicros(long duration) {
   5.223 +        throw new AbstractMethodError();
   5.224 +    }
   5.225 +
   5.226 +    /**
   5.227 +     * Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
   5.228 +     * @param duration the duration
   5.229 +     * @return the converted duration,
   5.230 +     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
   5.231 +     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
   5.232 +     * @see #convert
   5.233 +     */
   5.234 +    public long toMillis(long duration) {
   5.235 +        throw new AbstractMethodError();
   5.236 +    }
   5.237 +
   5.238 +    /**
   5.239 +     * Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
   5.240 +     * @param duration the duration
   5.241 +     * @return the converted duration,
   5.242 +     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
   5.243 +     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
   5.244 +     * @see #convert
   5.245 +     */
   5.246 +    public long toSeconds(long duration) {
   5.247 +        throw new AbstractMethodError();
   5.248 +    }
   5.249 +
   5.250 +    /**
   5.251 +     * Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
   5.252 +     * @param duration the duration
   5.253 +     * @return the converted duration,
   5.254 +     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
   5.255 +     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
   5.256 +     * @see #convert
   5.257 +     * @since 1.6
   5.258 +     */
   5.259 +    public long toMinutes(long duration) {
   5.260 +        throw new AbstractMethodError();
   5.261 +    }
   5.262 +
   5.263 +    /**
   5.264 +     * Equivalent to <tt>HOURS.convert(duration, this)</tt>.
   5.265 +     * @param duration the duration
   5.266 +     * @return the converted duration,
   5.267 +     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
   5.268 +     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
   5.269 +     * @see #convert
   5.270 +     * @since 1.6
   5.271 +     */
   5.272 +    public long toHours(long duration) {
   5.273 +        throw new AbstractMethodError();
   5.274 +    }
   5.275 +
   5.276 +    /**
   5.277 +     * Equivalent to <tt>DAYS.convert(duration, this)</tt>.
   5.278 +     * @param duration the duration
   5.279 +     * @return the converted duration
   5.280 +     * @see #convert
   5.281 +     * @since 1.6
   5.282 +     */
   5.283 +    public long toDays(long duration) {
   5.284 +        throw new AbstractMethodError();
   5.285 +    }
   5.286 +
   5.287 +    /**
   5.288 +     * Utility to compute the excess-nanosecond argument to wait,
   5.289 +     * sleep, join.
   5.290 +     * @param d the duration
   5.291 +     * @param m the number of milliseconds
   5.292 +     * @return the number of nanoseconds
   5.293 +     */
   5.294 +    abstract int excessNanos(long d, long m);
   5.295 +
   5.296 +    /**
   5.297 +     * Performs a timed {@link Object#wait(long, int) Object.wait}
   5.298 +     * using this time unit.
   5.299 +     * This is a convenience method that converts timeout arguments
   5.300 +     * into the form required by the <tt>Object.wait</tt> method.
   5.301 +     *
   5.302 +     * <p>For example, you could implement a blocking <tt>poll</tt>
   5.303 +     * method (see {@link BlockingQueue#poll BlockingQueue.poll})
   5.304 +     * using:
   5.305 +     *
   5.306 +     *  <pre> {@code
   5.307 +     * public synchronized Object poll(long timeout, TimeUnit unit)
   5.308 +     *     throws InterruptedException {
   5.309 +     *   while (empty) {
   5.310 +     *     unit.timedWait(this, timeout);
   5.311 +     *     ...
   5.312 +     *   }
   5.313 +     * }}</pre>
   5.314 +     *
   5.315 +     * @param obj the object to wait on
   5.316 +     * @param timeout the maximum time to wait. If less than
   5.317 +     * or equal to zero, do not wait at all.
   5.318 +     * @throws InterruptedException if interrupted while waiting
   5.319 +     */
   5.320 +    public void timedWait(Object obj, long timeout)
   5.321 +            throws InterruptedException {
   5.322 +        if (timeout > 0) {
   5.323 +            long ms = toMillis(timeout);
   5.324 +            int ns = excessNanos(timeout, ms);
   5.325 +            obj.wait(ms, ns);
   5.326 +        }
   5.327 +    }
   5.328 +
   5.329 +    /**
   5.330 +     * Performs a timed {@link Thread#join(long, int) Thread.join}
   5.331 +     * using this time unit.
   5.332 +     * This is a convenience method that converts time arguments into the
   5.333 +     * form required by the <tt>Thread.join</tt> method.
   5.334 +     *
   5.335 +     * @param thread the thread to wait for
   5.336 +     * @param timeout the maximum time to wait. If less than
   5.337 +     * or equal to zero, do not wait at all.
   5.338 +     * @throws InterruptedException if interrupted while waiting
   5.339 +     */
   5.340 +    public void timedJoin(Thread thread, long timeout)
   5.341 +            throws InterruptedException {
   5.342 +        if (timeout > 0) {
   5.343 +            long ms = toMillis(timeout);
   5.344 +            int ns = excessNanos(timeout, ms);
   5.345 +            thread.join(ms, ns);
   5.346 +        }
   5.347 +    }
   5.348 +
   5.349 +    /**
   5.350 +     * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
   5.351 +     * this time unit.
   5.352 +     * This is a convenience method that converts time arguments into the
   5.353 +     * form required by the <tt>Thread.sleep</tt> method.
   5.354 +     *
   5.355 +     * @param timeout the minimum time to sleep. If less than
   5.356 +     * or equal to zero, do not sleep at all.
   5.357 +     * @throws InterruptedException if interrupted while sleeping
   5.358 +     */
   5.359 +    public void sleep(long timeout) throws InterruptedException {
   5.360 +        if (timeout > 0) {
   5.361 +            long ms = toMillis(timeout);
   5.362 +            int ns = excessNanos(timeout, ms);
   5.363 +            Thread.sleep(ms, ns);
   5.364 +        }
   5.365 +    }
   5.366 +
   5.367 +}