jdk/src/share/classes/java/util/AbstractQueue.java
author yhuang
Wed, 14 Mar 2012 23:07:21 -0700
changeset 12135 078775fa4b30
parent 9242 ef138d47df58
permissions -rw-r--r--
7085757: Currency Data: ISO 4217 Amendment 152 Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class provides skeletal implementations of some {@link Queue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * operations. The implementations in this class are appropriate when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the base implementation does <em>not</em> allow <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * elements.  Methods {@link #add add}, {@link #remove remove}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * {@link #element element} are based on {@link #offer offer}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * #poll poll}, and {@link #peek peek}, respectively, but throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * exceptions instead of indicating failure via <tt>false</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <tt>null</tt> returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>A <tt>Queue</tt> implementation that extends this class must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * minimally define a method {@link Queue#offer} which does not permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * insertion of <tt>null</tt> elements, along with methods {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@link Collection#iterator}.  Typically, additional methods will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * overridden as well.  If these requirements cannot be met, consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * instead subclassing {@link AbstractCollection}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public abstract class AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    extends AbstractCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    implements Queue<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Constructor for use by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected AbstractQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Inserts the specified element into this queue if it is possible to do so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * immediately without violating capacity restrictions, returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * if no space is currently available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * else throws an <tt>IllegalStateException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @throws IllegalStateException if the element cannot be added at this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *         time due to capacity restrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @throws ClassCastException if the class of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *         prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @throws NullPointerException if the specified element is null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *         this queue does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @throws IllegalArgumentException if some property of this element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *         prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (offer(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalStateException("Queue full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Retrieves and removes the head of this queue.  This method differs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * from {@link #poll poll} only in that it throws an exception if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <p>This implementation returns the result of <tt>poll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * unless the queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return the head of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @throws NoSuchElementException if this queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        E x = poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (x != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Retrieves, but does not remove, the head of this queue.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * differs from {@link #peek peek} only in that it throws an exception if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * this queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>This implementation returns the result of <tt>peek</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * unless the queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return the head of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws NoSuchElementException if this queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        E x = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (x != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>This implementation repeatedly invokes {@link #poll poll} until it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * returns <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        while (poll() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Adds all of the elements in the specified collection to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * queue.  Attempts to addAll of a queue to itself result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <tt>IllegalArgumentException</tt>. Further, the behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * this operation is undefined if the specified collection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * modified while the operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <p>This implementation iterates over the specified collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * and adds each element returned by the iterator to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * queue, in turn.  A runtime exception encountered while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * trying to add an element (including, in particular, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <tt>null</tt> element) may result in only some of the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * having been successfully added when the associated exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param c collection containing elements to be added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return <tt>true</tt> if this queue changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @throws ClassCastException if the class of an element of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *         collection prevents it from being added to this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @throws NullPointerException if the specified collection contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *         null element and this queue does not permit null elements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @throws IllegalArgumentException if some property of an element of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *         specified collection prevents it from being added to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *         queue, or if the specified collection is this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws IllegalStateException if not all the elements can be added at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *         this time due to insertion restrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see #add(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        boolean modified = false;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   186
        for (E e : c)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   187
            if (add(e))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}