| 1 | |
package ca.uhn.hl7v2.concurrent; |
| 2 | |
|
| 3 | |
import java.util.Collection; |
| 4 | |
import java.util.Map; |
| 5 | |
import java.util.Set; |
| 6 | |
import java.util.concurrent.Callable; |
| 7 | |
import java.util.concurrent.ConcurrentHashMap; |
| 8 | |
import java.util.concurrent.ConcurrentMap; |
| 9 | |
import java.util.concurrent.CountDownLatch; |
| 10 | |
import java.util.concurrent.ExecutorService; |
| 11 | |
import java.util.concurrent.Executors; |
| 12 | |
import java.util.concurrent.Future; |
| 13 | |
import java.util.concurrent.TimeUnit; |
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
public class BlockingHashMap<K, V> implements BlockingMap<K, V> { |
| 27 | |
|
| 28 | 489 | private final ConcurrentMap<K, V> map = new ConcurrentHashMap<K, V>(); |
| 29 | 489 | private final ConcurrentMap<K, CountDownLatch> latches = new ConcurrentHashMap<K, CountDownLatch>(); |
| 30 | |
private final ExecutorService executor; |
| 31 | |
|
| 32 | |
public BlockingHashMap() { |
| 33 | 0 | this(Executors.newCachedThreadPool()); |
| 34 | 0 | } |
| 35 | |
|
| 36 | |
public BlockingHashMap(ExecutorService executor) { |
| 37 | 489 | super(); |
| 38 | 489 | this.executor = executor; |
| 39 | 489 | } |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public Set<K> keySet() { |
| 47 | 0 | return map.keySet(); |
| 48 | |
} |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public V get(Object key) { |
| 56 | 10 | return map.get(key); |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public boolean containsKey(Object key) { |
| 65 | 70 | return map.containsKey(key); |
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
synchronized public V put(K key, V value) { |
| 72 | 5608 | V result = map.put(key, value); |
| 73 | 5608 | latchFor(key).countDown(); |
| 74 | 5608 | return result; |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
synchronized public boolean give(K key, V value) { |
| 82 | 5588 | if (!latches.containsKey(key)) { |
| 83 | 5 | return false; |
| 84 | |
} |
| 85 | 5583 | put(key, value); |
| 86 | 5583 | return true; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public V take(K key) throws InterruptedException { |
| 90 | 25 | latchFor(key).await(); |
| 91 | 25 | latches.remove(key); |
| 92 | 25 | return map.remove(key); |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
public Future<V> asyncTake(final K key) throws InterruptedException { |
| 97 | 25 | latchFor(key); |
| 98 | 25 | return executor.submit(new Callable<V>() { |
| 99 | |
|
| 100 | |
public V call() throws Exception { |
| 101 | 25 | return take(key); |
| 102 | |
} |
| 103 | |
}); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public V poll(K key, long timeout, TimeUnit unit) |
| 107 | |
throws InterruptedException { |
| 108 | 5603 | if (latchFor(key).await(timeout, unit)) { |
| 109 | 5588 | latches.remove(key); |
| 110 | 5588 | return map.remove(key); |
| 111 | |
} |
| 112 | 15 | return null; |
| 113 | |
} |
| 114 | |
|
| 115 | |
public Future<V> asyncPoll(final K key, final long timeout, final TimeUnit unit) { |
| 116 | 5603 | latchFor(key); |
| 117 | 5603 | return executor.submit(new Callable<V>() { |
| 118 | |
|
| 119 | |
public V call() throws Exception { |
| 120 | 5603 | return poll(key, timeout, unit); |
| 121 | |
} |
| 122 | |
}); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public boolean isEmpty() { |
| 132 | 25 | return map.isEmpty(); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public int size() { |
| 141 | 5 | return map.size(); |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
synchronized public V remove(Object key) { |
| 151 | 10 | V result = map.remove(key); |
| 152 | 10 | CountDownLatch latch = latches.remove(key); |
| 153 | 10 | if (latch != null) |
| 154 | 10 | latch.countDown(); |
| 155 | 10 | return result; |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public void clear() { |
| 165 | 5 | for (K key : latches.keySet()) { |
| 166 | 5 | remove(key); |
| 167 | 5 | } |
| 168 | 5 | } |
| 169 | |
|
| 170 | |
public Collection<V> values() { |
| 171 | 0 | return map.values(); |
| 172 | |
} |
| 173 | |
|
| 174 | |
public Set<java.util.Map.Entry<K, V>> entrySet() { |
| 175 | 0 | return map.entrySet(); |
| 176 | |
} |
| 177 | |
|
| 178 | |
public void putAll(Map<? extends K, ? extends V> t) { |
| 179 | 0 | for (Entry<? extends K, ? extends V> entry : t.entrySet()) { |
| 180 | 0 | put(entry.getKey(), entry.getValue()); |
| 181 | 0 | } |
| 182 | 0 | } |
| 183 | |
|
| 184 | |
public boolean containsValue(Object value) { |
| 185 | 5 | return map.containsValue(value); |
| 186 | |
} |
| 187 | |
|
| 188 | |
private synchronized CountDownLatch latchFor(K key) { |
| 189 | 16864 | CountDownLatch latch = latches.get(key); |
| 190 | 16864 | if (latch == null) { |
| 191 | 5628 | latch = new CountDownLatch(1); |
| 192 | 5628 | latches.put(key, latch); |
| 193 | |
} |
| 194 | 16864 | return latch; |
| 195 | |
} |
| 196 | |
|
| 197 | |
} |