001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.collections; 018 019 import java.util.NoSuchElementException; 020 021 /** 022 * A thread safe version of the PriorityQueue. 023 * Provides synchronized wrapper methods for all the methods 024 * defined in the PriorityQueue interface. 025 * 026 * @deprecated PriorityQueue is replaced by the Buffer interface, see buffer subpackage. 027 * Due to be removed in v4.0. 028 * @since Commons Collections 1.0 029 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 030 * 031 * @author Ram Chidambaram 032 */ 033 public final class SynchronizedPriorityQueue implements PriorityQueue { 034 035 /** 036 * The underlying priority queue. 037 */ 038 protected final PriorityQueue m_priorityQueue; 039 040 /** 041 * Constructs a new synchronized priority queue. 042 * 043 * @param priorityQueue the priority queue to synchronize 044 */ 045 public SynchronizedPriorityQueue(final PriorityQueue priorityQueue) { 046 m_priorityQueue = priorityQueue; 047 } 048 049 /** 050 * Clear all elements from queue. 051 */ 052 public synchronized void clear() { 053 m_priorityQueue.clear(); 054 } 055 056 /** 057 * Test if queue is empty. 058 * 059 * @return true if queue is empty else false. 060 */ 061 public synchronized boolean isEmpty() { 062 return m_priorityQueue.isEmpty(); 063 } 064 065 /** 066 * Insert an element into queue. 067 * 068 * @param element the element to be inserted 069 */ 070 public synchronized void insert(final Object element) { 071 m_priorityQueue.insert(element); 072 } 073 074 /** 075 * Return element on top of heap but don't remove it. 076 * 077 * @return the element at top of heap 078 * @throws NoSuchElementException if isEmpty() == true 079 */ 080 public synchronized Object peek() throws NoSuchElementException { 081 return m_priorityQueue.peek(); 082 } 083 084 /** 085 * Return element on top of heap and remove it. 086 * 087 * @return the element at top of heap 088 * @throws NoSuchElementException if isEmpty() == true 089 */ 090 public synchronized Object pop() throws NoSuchElementException { 091 return m_priorityQueue.pop(); 092 } 093 094 /** 095 * Returns a string representation of the underlying queue. 096 * 097 * @return a string representation of the underlying queue 098 */ 099 public synchronized String toString() { 100 return m_priorityQueue.toString(); 101 } 102 103 }