1 package it.unimi.dsi.fastutil; 2 3 23 24 import java.util.Comparator ; 25 import java.util.NoSuchElementException ; 26 27 31 32 public class IndirectPriorityQueues { 33 34 private IndirectPriorityQueues() {} 35 36 41 42 public static class EmptyIndirectPriorityQueue extends AbstractIndirectPriorityQueue { 43 44 protected EmptyIndirectPriorityQueue() {} 45 46 public void enqueue( final int i ) { throw new UnsupportedOperationException (); } 47 public int dequeue() { throw new NoSuchElementException (); } 48 public boolean isEmpty() { return true; } 49 public int size() { return 0; } 50 public void clear() {} 51 public int first() { throw new NoSuchElementException (); } 52 public int last() { throw new NoSuchElementException (); } 53 public void changed() { throw new NoSuchElementException (); } 54 public void allChanged() {} 55 public Comparator comparator() { return null; } 56 public void changed( final int i ) { throw new IllegalArgumentException ( "Index " + i + " is not in the queue" ); } 57 public void remove( final int i ) { throw new IllegalArgumentException ( "Index " + i + " is not in the queue" ); } 58 59 } 60 61 63 64 public final static EmptyIndirectPriorityQueue EMPTY_QUEUE = new EmptyIndirectPriorityQueue(); 65 66 67 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 sync; 75 76 protected SynchronizedIndirectPriorityQueue( final IndirectPriorityQueue<K> q, final Object 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 <? super K> comparator() { synchronized( sync ) { return q.comparator(); } } 98 } 99 100 101 106 public static <K> IndirectPriorityQueue<K> synchronize( final IndirectPriorityQueue<K> q ) { return new SynchronizedIndirectPriorityQueue<K>( q ); } 107 108 114 115 public static <K> IndirectPriorityQueue<K> synchronize( final IndirectPriorityQueue<K> q, final Object sync ) { return new SynchronizedIndirectPriorityQueue<K>( q, sync ); } 116 117 } 118 | Popular Tags |