src/share/classes/java/util/PriorityQueue.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 313      * @throws ClassCastException if the specified element cannot be
 314      *         compared with elements currently in this priority queue
 315      *         according to the priority queue's ordering
 316      * @throws NullPointerException if the specified element is null
 317      */
 318     public boolean offer(E e) {
 319         if (e == null)
 320             throw new NullPointerException();
 321         modCount++;
 322         int i = size;
 323         if (i >= queue.length)
 324             grow(i + 1);
 325         size = i + 1;
 326         if (i == 0)
 327             queue[0] = e;
 328         else
 329             siftUp(i, e);
 330         return true;
 331     }
 332 

 333     public E peek() {
 334         if (size == 0)
 335             return null;
 336         return (E) queue[0];
 337     }
 338 
 339     private int indexOf(Object o) {
 340         if (o != null) {
 341             for (int i = 0; i < size; i++)
 342                 if (o.equals(queue[i]))
 343                     return i;
 344         }
 345         return -1;
 346     }
 347 
 348     /**
 349      * Removes a single instance of the specified element from this queue,
 350      * if it is present.  More formally, removes an element {@code e} such
 351      * that {@code o.equals(e)}, if this queue contains one or more such
 352      * elements.  Returns {@code true} if and only if this queue contained


   1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 313      * @throws ClassCastException if the specified element cannot be
 314      *         compared with elements currently in this priority queue
 315      *         according to the priority queue's ordering
 316      * @throws NullPointerException if the specified element is null
 317      */
 318     public boolean offer(E e) {
 319         if (e == null)
 320             throw new NullPointerException();
 321         modCount++;
 322         int i = size;
 323         if (i >= queue.length)
 324             grow(i + 1);
 325         size = i + 1;
 326         if (i == 0)
 327             queue[0] = e;
 328         else
 329             siftUp(i, e);
 330         return true;
 331     }
 332 
 333     @SuppressWarnings("unchecked")
 334     public E peek() {
 335         if (size == 0)
 336             return null;
 337         return (E) queue[0];
 338     }
 339 
 340     private int indexOf(Object o) {
 341         if (o != null) {
 342             for (int i = 0; i < size; i++)
 343                 if (o.equals(queue[i]))
 344                     return i;
 345         }
 346         return -1;
 347     }
 348 
 349     /**
 350      * Removes a single instance of the specified element from this queue,
 351      * if it is present.  More formally, removes an element {@code e} such
 352      * that {@code o.equals(e)}, if this queue contains one or more such
 353      * elements.  Returns {@code true} if and only if this queue contained