您的位置:首页 > 产品设计 > UI/UE

AbstractQueue抽象类源码解析

2016-07-16 09:30 423 查看
继承Queue

对一些方法增加抛出异常

package java.util;

public abstract class AbstractQueue<E>
extends AbstractCollection<E>
implements Queue<E> {

/**
* 空构造器
*/
protected AbstractQueue() {
}

/**
* 插入元素
*
* @param e the element to add
* @return <tt>true</tt> (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this
*         time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
*         prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
*         this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
*         prevents it from being added to this queue
*/
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}

/**
* 删除元素
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}

/**
* 查看队头元素
*
* <p>This implementation returns the result of <tt>peek</tt>
* unless the queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
public E element() {
E x = peek();
if (x != null)
return x;
else
throw new NoSuchElementException();
}

/**
* 清空
*
* <p>This implementation repeatedly invokes {@link #poll poll} until it
* returns <tt>null</tt>.
*/
public void clear() {
while (poll() != null)
;
}

/**
* 集合c中元素,加入到队列中
* @param c collection containing elements to be added to this queue
* @return <tt>true</tt> if this queue changed as a result of the call
* @throws ClassCastException if the class of an element of the specified
*         collection prevents it from being added to this queue
* @throws NullPointerException if the specified collection contains a
*         null element and this queue does not permit null elements,
*         or if the specified collection is null
* @throws IllegalArgumentException if some property of an element of the
*         specified collection prevents it from being added to this
*         queue, or if the specified collection is this queue
* @throws IllegalStateException if not all the elements can be added at
*         this time due to insertion restrictions
* @see #add(Object)
*/
public boolean addAll(Collection<? extends E> c) {
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: