KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > scheduler > SynchronizedPriorityQueue


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.blocks.scheduler;
19
20 import java.util.NoSuchElementException JavaDoc;
21
22 /**
23  * A thread safe version of the PriorityQueue.
24  * Provides synchronized wrapper methods for all the methods
25  * defined in the PriorityQueue interface.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version CVS $Revision: 1.1 $ $Date: 2004/03/16 12:49:52 $
29  * @since 4.0
30  */

31 public final class SynchronizedPriorityQueue
32     implements PriorityQueue
33 {
34     private final PriorityQueue m_priorityQueue;
35
36     public SynchronizedPriorityQueue( final PriorityQueue priorityQueue )
37     {
38         if( null == priorityQueue )
39         {
40             throw new NullPointerException JavaDoc( "priorityQueue" );
41         }
42         m_priorityQueue = priorityQueue;
43     }
44
45     /**
46      * Clear all elements from queue.
47      */

48     public void clear()
49     {
50         synchronized( m_priorityQueue )
51         {
52             m_priorityQueue.clear();
53         }
54     }
55
56     /**
57      * Test if queue is empty.
58      *
59      * @return true if queue is empty else false.
60      */

61     public boolean isEmpty()
62     {
63         synchronized( m_priorityQueue )
64         {
65             return m_priorityQueue.isEmpty();
66         }
67     }
68
69     /**
70      * Insert an element into queue.
71      *
72      * @param element the element to be inserted
73      */

74     public void insert( final Object JavaDoc element )
75     {
76         synchronized( m_priorityQueue )
77         {
78             m_priorityQueue.insert( element );
79         }
80     }
81
82     /**
83      * Return element on top of heap but don't remove it.
84      *
85      * @return the element at top of heap
86      * @throws NoSuchElementException if isEmpty() == true
87      */

88     public Object JavaDoc peek() throws NoSuchElementException JavaDoc
89     {
90         synchronized( m_priorityQueue )
91         {
92             return m_priorityQueue.peek();
93         }
94     }
95
96     /**
97      * Return element on top of heap and remove it.
98      *
99      * @return the element at top of heap
100      * @throws NoSuchElementException if isEmpty() == true
101      */

102     public Object JavaDoc pop() throws NoSuchElementException JavaDoc
103     {
104         synchronized( m_priorityQueue )
105         {
106             return m_priorityQueue.pop();
107         }
108     }
109
110     public String JavaDoc toString()
111     {
112         synchronized( m_priorityQueue )
113         {
114             return m_priorityQueue.toString();
115         }
116     }
117 }
118
119
Popular Tags