KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** A class providing static methods and objects that do useful things with indirect priority queues.
28  *
29  * @see IndirectPriorityQueue
30  */

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

41
42     public static class EmptyIndirectPriorityQueue extends AbstractIndirectPriorityQueue {
43
44         protected EmptyIndirectPriorityQueue() {}
45
46         public void enqueue( final int i ) { throw new UnsupportedOperationException JavaDoc(); }
47         public int dequeue() { throw new NoSuchElementException JavaDoc(); }
48         public boolean isEmpty() { return true; }
49         public int size() { return 0; }
50         public void clear() {}
51         public int first() { throw new NoSuchElementException JavaDoc(); }
52         public int last() { throw new NoSuchElementException JavaDoc(); }
53         public void changed() { throw new NoSuchElementException JavaDoc(); }
54         public void allChanged() {}
55         public Comparator JavaDoc comparator() { return null; }
56         public void changed( final int i ) { throw new IllegalArgumentException JavaDoc( "Index " + i + " is not in the queue" ); }
57         public void remove( final int i ) { throw new IllegalArgumentException JavaDoc( "Index " + i + " is not in the queue" ); }
58         
59     }
60
61     /** An empty indirect priority queue (immutable).
62      */

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

106     public static <K> IndirectPriorityQueue<K> synchronize( final IndirectPriorityQueue<K> q ) { return new SynchronizedIndirectPriorityQueue<K>( q ); }
107
108     /** Returns a synchronized type-specific indirect priority queue backed by the specified type-specific indirect priority queue, using an assigned object to synchronize.
109      *
110      * @param q the indirect priority queue to be wrapped in a synchronized indirect priority queue.
111      * @param sync an object that will be used to synchronize the access to the indirect priority queue.
112      * @return a synchronized view of the specified indirect priority queue.
113      */

114
115     public static <K> IndirectPriorityQueue<K> synchronize( final IndirectPriorityQueue<K> q, final Object JavaDoc sync ) { return new SynchronizedIndirectPriorityQueue<K>( q, sync ); }
116
117 }
118
Popular Tags