1 package it.unimi.dsi.fastutil; 2 3 23 24 import java.util.Comparator ; 25 import java.util.NoSuchElementException ; 26 27 import it.unimi.dsi.fastutil.PriorityQueue; 28 29 33 34 public class PriorityQueues { 35 36 private PriorityQueues() {} 37 38 43 44 public static class EmptyPriorityQueue extends AbstractPriorityQueue { 45 46 protected EmptyPriorityQueue() {} 47 48 public void enqueue( Object o ) { throw new UnsupportedOperationException (); } 49 public Object dequeue() { throw new NoSuchElementException (); } 50 public boolean isEmpty() { return true; } 51 public int size() { return 0; } 52 public void clear() {} 53 public Object first() { throw new NoSuchElementException (); } 54 public Object last() { throw new NoSuchElementException (); } 55 public void changed() { throw new NoSuchElementException (); } 56 public Comparator comparator() { return null; } 57 58 } 59 60 62 63 public final static EmptyPriorityQueue EMPTY_QUEUE = new EmptyPriorityQueue(); 64 65 66 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 sync; 74 75 protected SynchronizedPriorityQueue( final PriorityQueue <K> q, final Object 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 <? super K> comparator() { synchronized( sync ) { return q.comparator(); } } 94 } 95 96 97 102 public static <K> PriorityQueue <K> synchronize( final PriorityQueue <K> q ) { return new SynchronizedPriorityQueue<K>( q ); } 103 104 110 111 public static <K> PriorityQueue <K> synchronize( final PriorityQueue <K> q, final Object sync ) { return new SynchronizedPriorityQueue<K>( q, sync ); } 112 } 113 | Popular Tags |