KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > unimi > dsi > fastutil > PriorityQueues


1 package it.unimi.dsi.fastutil;
2
3 /*
4  * fastutil: Fast & compact type-specific collections for Java
5  *
6  * Copyright (C) 2003, 2004, 2005, 2006 Sebastiano Vigna
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */

23
24 import java.util.Comparator JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26
27 import it.unimi.dsi.fastutil.PriorityQueue;
28
29 /** A class providing static methods and objects that do useful things with priority queues.
30  *
31  * @see it.unimi.dsi.fastutil.PriorityQueue
32  */

33
34 public class PriorityQueues {
35
36     private PriorityQueues() {}
37
38     /** An immutable class representing the empty priority queue.
39      *
40      * <P>This class may be useful to implement your own in case you subclass
41      * {@link PriorityQueue}.
42      */

43
44     public static class EmptyPriorityQueue extends AbstractPriorityQueue {
45
46         protected EmptyPriorityQueue() {}
47
48         public void enqueue( Object JavaDoc o ) { throw new UnsupportedOperationException JavaDoc(); }
49         public Object JavaDoc dequeue() { throw new NoSuchElementException JavaDoc(); }
50         public boolean isEmpty() { return true; }
51         public int size() { return 0; }
52         public void clear() {}
53         public Object JavaDoc first() { throw new NoSuchElementException JavaDoc(); }
54         public Object JavaDoc last() { throw new NoSuchElementException JavaDoc(); }
55         public void changed() { throw new NoSuchElementException JavaDoc(); }
56         public Comparator JavaDoc comparator() { return null; }
57         
58     }
59
60     /** An empty indirect priority queue (immutable).
61      */

62
63     public final static EmptyPriorityQueue EMPTY_QUEUE = new EmptyPriorityQueue();
64
65
66     /** A synchronized wrapper class for priority queues. */
67
68     public static class SynchronizedPriorityQueue<K> implements PriorityQueue<K> {
69         
70         public static final long serialVersionUID = -7046029254386353129L;
71
72         final protected PriorityQueue <K> q;
73         final protected Object JavaDoc sync;
74
75         protected SynchronizedPriorityQueue( final PriorityQueue <K> q, final Object JavaDoc sync ) {
76             this.q = q;
77             this.sync = sync;
78         }
79
80         protected SynchronizedPriorityQueue( final PriorityQueue <K> q ) {
81             this.q = q;
82             this.sync = this;
83         }
84
85         public void enqueue( K x ) { synchronized( sync ) { q.enqueue( x ); } }
86         public K dequeue() { synchronized( sync ) { return q.dequeue(); } }
87         public K first() { synchronized( sync ) { return q.first(); } }
88         public K last() { synchronized( sync ) { return q.last(); } }
89         public boolean isEmpty() { synchronized( sync ) { return q.isEmpty(); } }
90         public int size() { synchronized( sync ) { return q.size(); } }
91         public void clear() { synchronized( sync ) { q.clear(); } }
92         public void changed() { synchronized( sync ) { q.changed(); } }
93         public Comparator JavaDoc <? super K> comparator() { synchronized( sync ) { return q.comparator(); } }
94     }
95
96
97     /** Returns a synchronized priority queue backed by the specified priority queue.
98      *
99      * @param q the priority queue to be wrapped in a synchronized priority queue.
100      * @return a synchronized view of the specified priority queue.
101      */

102     public static <K> PriorityQueue <K> synchronize( final PriorityQueue <K> q ) { return new SynchronizedPriorityQueue<K>( q ); }
103
104     /** Returns a synchronized priority queue backed by the specified priority queue, using an assigned object to synchronize.
105      *
106      * @param q the priority queue to be wrapped in a synchronized priority queue.
107      * @param sync an object that will be used to synchronize the access to the priority queue.
108      * @return a synchronized view of the specified priority queue.
109      */

110
111     public static <K> PriorityQueue <K> synchronize( final PriorityQueue <K> q, final Object JavaDoc sync ) { return new SynchronizedPriorityQueue<K>( q, sync ); }
112 }
113
Popular Tags