1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections;
  17. import java.util.NoSuchElementException;
  18. /**
  19. * A thread safe version of the PriorityQueue.
  20. * Provides synchronized wrapper methods for all the methods
  21. * defined in the PriorityQueue interface.
  22. *
  23. * @deprecated PriorityQueue is replaced by the Buffer interface, see buffer subpackage.
  24. * Due to be removed in v4.0.
  25. * @since Commons Collections 1.0
  26. * @version $Revision: 1.13 $ $Date: 2004/02/18 01:15:42 $
  27. *
  28. * @author Ram Chidambaram
  29. */
  30. public final class SynchronizedPriorityQueue implements PriorityQueue {
  31. /**
  32. * The underlying priority queue.
  33. */
  34. protected final PriorityQueue m_priorityQueue;
  35. /**
  36. * Constructs a new synchronized priority queue.
  37. *
  38. * @param priorityQueue the priority queue to synchronize
  39. */
  40. public SynchronizedPriorityQueue(final PriorityQueue priorityQueue) {
  41. m_priorityQueue = priorityQueue;
  42. }
  43. /**
  44. * Clear all elements from queue.
  45. */
  46. public synchronized void clear() {
  47. m_priorityQueue.clear();
  48. }
  49. /**
  50. * Test if queue is empty.
  51. *
  52. * @return true if queue is empty else false.
  53. */
  54. public synchronized boolean isEmpty() {
  55. return m_priorityQueue.isEmpty();
  56. }
  57. /**
  58. * Insert an element into queue.
  59. *
  60. * @param element the element to be inserted
  61. */
  62. public synchronized void insert(final Object element) {
  63. m_priorityQueue.insert(element);
  64. }
  65. /**
  66. * Return element on top of heap but don't remove it.
  67. *
  68. * @return the element at top of heap
  69. * @throws NoSuchElementException if isEmpty() == true
  70. */
  71. public synchronized Object peek() throws NoSuchElementException {
  72. return m_priorityQueue.peek();
  73. }
  74. /**
  75. * Return element on top of heap and remove it.
  76. *
  77. * @return the element at top of heap
  78. * @throws NoSuchElementException if isEmpty() == true
  79. */
  80. public synchronized Object pop() throws NoSuchElementException {
  81. return m_priorityQueue.pop();
  82. }
  83. /**
  84. * Returns a string representation of the underlying queue.
  85. *
  86. * @return a string representation of the underlying queue
  87. */
  88. public synchronized String toString() {
  89. return m_priorityQueue.toString();
  90. }
  91. }