`
Donald_Draper
  • 浏览: 945733 次
社区版块
存档分类
最新评论

AbstractQueue简介

    博客分类:
  • JUC
阅读更多
Queue接口定义:http://donald-draper.iteye.com/blog/2363491
/*
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 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/publicdomain/zero/1.0/
 */

package java.util;

/**
 * This class provides skeletal implementations of some {@link Queue}
 * operations. The implementations in this class are appropriate when
 * the base implementation does [i]not[/i] allow <tt>null</tt>
 * elements.  Methods {@link #add add}, {@link #remove remove}, and
 * {@link #element element} are based on {@link #offer offer}, {@link
 * #poll poll}, and {@link #peek peek}, respectively, but throw
 * exceptions instead of indicating failure via <tt>false</tt> or
 * <tt>null</tt> returns.
 AbstractQueue提供了Queue的操作实现的基础。AbstractQueue可以作为不允许
 元素为null时,Queue的简单实现。add,remove,element方法分别基于offer,
 poll,peek的实现,但是当队列为null时,抛出异常,而不是返回false或null。

 * <p>A <tt>Queue</tt> implementation that extends this class must
 * minimally define a method {@link Queue#offer} which does not permit
 * insertion of <tt>null</tt> elements, along with methods {@link
 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and
 * {@link Collection#iterator}.  Typically, additional methods will be
 * overridden as well.  If these requirements cannot be met, consider
 * instead subclassing {@link AbstractCollection}.
 *
Queue的实现必须实现offer方法,并且允许插入null;peek,poll,size方法最好
重写,如果这些要求不能满足,可以用AbstractCollection代替。
 * <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 abstract class AbstractQueue<E>
    extends AbstractCollection<E>
    implements Queue<E> {

    /**
     * Constructor for use by subclasses.
     */
    protected AbstractQueue() {
    }

    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
     * if no space is currently available.
     *
     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
     * else throws an <tt>IllegalStateException</tt>.
     *
     * @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
     */
     添加一个元素,成功返回ture,否则抛出异常
    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

    /**
     * Retrieves and removes the head of this queue.  This method differs
     * from {@link #poll poll} only in that it throws an exception if this
     * queue is empty.
     *
     * <p>This implementation returns the result of <tt>poll</tt>
     * unless the queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    消费队列头元素,有则返回元素,为null,则抛出异常
    public E remove() {
        E x = poll();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    /**
     * Retrieves, but does not remove, the head of this queue.  This method
     * differs from {@link #peek peek} only in that it throws an exception if
     * this queue is empty.
     *
     * <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();
    }

    /**
     * Removes all of the elements from this queue.
     * The queue will be empty after this call returns.
     *
     * <p>This implementation repeatedly invokes {@link #poll poll} until it
     * returns <tt>null</tt>.
     */
     清空,循环poll,直到为空
    public void clear() {
        while (poll() != null)
            ;
    }

    /**
     * Adds all of the elements in the specified collection to this
     * queue.  Attempts to addAll of a queue to itself result in
     * <tt>IllegalArgumentException</tt>. Further, the behavior of
     * this operation is undefined if the specified collection is
     * modified while the operation is in progress.
     *
     * <p>This implementation iterates over the specified collection,
     * and adds each element returned by the iterator to this
     * queue, in turn.  A runtime exception encountered while
     * trying to add an element (including, in particular, a
     * <tt>null</tt> element) may result in only some of the elements
     * having been successfully added when the associated exception is
     * thrown.
     *
     * @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)
     */
    //循环遍历集合元素,add到队列
    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;
    }
}

总结:
AbstractQueue的add,remove,element方法分别基于offer,poll,peek的实现,但是当队列为null时,抛出异常,而不是返回false或null。offer,poll,peek,并没有实现待子类扩展。清空,循环poll,直到为空。addAll为循环遍历集合元素,add到队列;
附:AbstractCollection



  • 大小: 44.5 KB
0
0
分享到:
评论

相关推荐

    超全Java集合框架讲解.md

    超全Java集合框架讲解 - 超全Java集合框架讲解 - 集合框架总览 - Iterator Iterable ListIterator - Map 和 Collection 接口 - Map 集合体系详解 - HashMap ... - AbstractQueue 抽象类 - Lin

    Java 基础核心总结 +经典算法大全.rar

    AbstractQueue 抽象类LinkedList ArrayDeque PriorityQueue 反射的思想及作用 反射的基本使用 获取类的 Class 对象构造类的实例化对象获取-个类的所有信息 获取类中的变量(Field) 获取类中的方法(Method) 获取类的...

    jtable增删改查和jtree连接

    import java.util.AbstractQueue; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree....

    基于Tensorflow的iOS图像处理工程,效果类似Primsa.zip

    人工智能-深度学习-tensorflow

    该仓库用于存储项目“基于云边深度学习融合的COVID-19智能检测系统”的识别模型测检测代码部分.zip

    人工智能毕业设计&课程设计

    基于ssm的中小型企业财务管理录系统.zip

    基于ssm的中小型企业财务管理录系统.zip

    JAVA毕业设计之springboot057洗衣店订单管理系统(springboot+mysql)完整源码.zip

    这个资源是一个基于Spring Boot和MySQL的洗衣店订单管理系统的完整源码。它包括了所有的源代码文件,以及一个详细的文档,可以帮助你理解和运行这个系统。这个系统的主要功能包括:用户注册和登录,下单,查看订单,修改订单,删除订单等。用户可以在系统中选择洗衣服务,然后提交订单。系统会自动计算订单的总价,并将其显示在用户的订单列表中。用户还可以查看自己的历史订单,以及每个订单的详细信息。此外,系统还包括了一个管理员模块。管理员可以查看所有的订单,以及对订单进行管理。他们可以修改订单的状态,例如将订单标记为已完成,或者取消订单。这个系统使用了Spring Boot框架,这是一个非常流行的Java开发框架,它可以帮助你快速地开发和部署应用程序。同时,系统也使用了MySQL数据库,这是一个广泛使用的关系型数据库,它可以存储大量的数据,并提供高效的查询功能。总的来说,这个资源是一个非常完整的洗衣店订单管理系统的源码,它可以帮助你理解如何使用Spring Boot和MySQL来开发一个实际的应用程序。无论你是正在学习Java编程,还是已经有一定的开发经验,都可以从这个资源中学到很多有用的知识和技能。

    W9825G6KH-6I 产品规格书pdf

    W9825G6KH-6I SDRAM,256Mb(32MB,16Mbx16),3.3v 动态随机存取存储器

    WeRoBot-1.10.1-py2.py3-none-any.whl

    Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。

    (后端)本科毕业设计-基于深度学习的人脸识别考勤系统.zip

    人工智能毕业设计&课程设计

    asp代码ASP基于web的学校新闻发布系统开发(论文+源代码+开题报告+文献综述+外文翻译)

    asp代码ASP基于web的学校新闻发布系统开发(论文+源代码+开题报告+文献综述+外文翻译)本资源系百度网盘分享地址

    三菱PLC例程源码PLC气压程式

    三菱PLC例程源码PLC 气压程式本资源系百度网盘分享地址

    三菱PLC例程源码PLC通过RS485对FR系列变频的控制

    三菱PLC例程源码PLC通过RS485 对FR系列变频的控制本资源系百度网盘分享地址

    基于ssm的学生档案管理系统.zip

    基于ssm的学生档案管理系统.zip

    基于Java的elfinder 2.x版本Web文件管理器后端设计源码

    这是一个基于Java语言开发的elfinder 2.x版本Web文件管理器后端设计,包含63个文件,其中主要文件类型包括49个Java源文件、3个XML文件、2个PNG图片文件、2个Markdown文档、1个gitattributes文件、1个gitignore文件、1个LICENSE文件、1个Properties文件、1个types文件和1个未知类型的文件。该项目提供了丰富的文件管理功能,包括自定义文件视图和自定义文件操作,为用户提供了高效、便捷的文件管理体验。

    使用DS Client在PPT中动态展示分子三维结构.pdf

    使用DS Client在PPT中动态展示分子三维结构

    基于ssm+vue的汽车站车辆运管系统.zip

    基于ssm+vue的汽车站车辆运管系统.zip

    tensorflow_model_optimization-0.3.0.dev0-py2.py3-none-any.whl

    算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。

    DS在生物药物领域的解决方案.pdf

    DS在生物药物领域的解决方案

    三菱PLC例程源码SBR废水处理

    三菱PLC例程源码SBR废水处理本资源系百度网盘分享地址

Global site tag (gtag.js) - Google Analytics