KickJava   Java API By Example, From Geeks To Geeks.

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


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 IndirectDoublePriorityQueue
30  */

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

41
42     public static class EmptyIndirectDoublePriorityQueue extends IndirectPriorityQueues.EmptyIndirectPriorityQueue {
43
44         protected EmptyIndirectDoublePriorityQueue() {}
45
46         public int secondaryFirst() { throw new NoSuchElementException JavaDoc(); }
47         public int secondaryLast() { throw new NoSuchElementException JavaDoc(); }
48         public Comparator JavaDoc secondaryComparator() { return null; }
49         
50     }
51
52     /** An empty indirect double priority queue (immutable).
53      */

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

100     public static <K> IndirectDoublePriorityQueue<K> synchronize( final IndirectDoublePriorityQueue<K> q ) { return new SynchronizedIndirectDoublePriorityQueue<K>( q ); }
101
102     /** Returns a synchronized type-specific indirect double priority queue backed by the specified type-specific indirect double priority queue, using an assigned object to synchronize.
103      *
104      * @param q the indirect double priority queue to be wrapped in a synchronized indirect double priority queue.
105      * @param sync an object that will be used to synchronize the access to the indirect double priority queue.
106      * @return a synchronized view of the specified indirect double priority queue.
107      */

108
109     public static <K> IndirectDoublePriorityQueue<K> synchronize( final IndirectDoublePriorityQueue<K> q, final Object JavaDoc sync ) { return new SynchronizedIndirectDoublePriorityQueue<K>( q, sync ); }
110
111 }
112
Popular Tags