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; } }
相关文章推荐
- Deque接口源码解析
- 解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
- codeforces 360 E - The Values You Can Make
- Buildroot-2012.08交叉编译器制作
- Queue接口源码解析
- android studio中requestWindowFeature(Window.FEATURE_NO_TITLE)隐藏标题栏失效的解决方案
- iOS开发之UITextField 左侧设置小图标 leftview
- 62. Unique Paths
- MySQL提示:The server quit without updating PID file(…)失败
- Callable与Futrue创建线程
- EasyUI基础知识
- PyQt5 pyuic
- EasyUI基础知识
- EasyUI基础知识
- Longest Consecutive Sequence
- Intuit Quicken Home & Business 2016(Manage your business and personal finances)
- ${pageContext.request.contextPath} 表达式的分析及作用
- [leetcode] 374. Guess Number Higher or Lower 解题报告
- 隐藏uuid1的mac信息
- 实战 Android中的UI过度绘制