|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2017 Thibault Debatty. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package info.debatty.java.util; |
| 26 | + |
| 27 | +import java.io.Serializable; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Iterator; |
| 30 | +import java.util.Queue; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * @author Thibault Debatty |
| 35 | + * @param <E> |
| 36 | + */ |
| 37 | +public class SynchronizedBoundedPriorityQueue<E> |
| 38 | + implements Serializable, Iterable<E>, Collection<E>, Queue<E> { |
| 39 | + |
| 40 | + private final BoundedPriorityQueue<E> queue; |
| 41 | + |
| 42 | + public SynchronizedBoundedPriorityQueue(final int capacity) { |
| 43 | + queue = new BoundedPriorityQueue<E>(capacity); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Iterator<E> iterator() { |
| 48 | + return queue.iterator(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public int size() { |
| 53 | + synchronized (this) { |
| 54 | + return queue.size(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public boolean offer(E e) { |
| 59 | + synchronized (this) { |
| 60 | + return queue.offer(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public E poll() { |
| 65 | + synchronized (this) { |
| 66 | + return queue.poll(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public E peek() { |
| 71 | + synchronized (this) { |
| 72 | + return queue.peek(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public boolean isEmpty() { |
| 77 | + synchronized (this) { |
| 78 | + return queue.isEmpty(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public boolean contains(Object o) { |
| 83 | + synchronized (this) { |
| 84 | + return queue.contains(o); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public Object[] toArray() { |
| 89 | + synchronized (this) { |
| 90 | + return queue.toArray(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public <T> T[] toArray(T[] a) { |
| 95 | + synchronized (this) { |
| 96 | + return queue.toArray(a); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public boolean add(E e) { |
| 101 | + synchronized (this) { |
| 102 | + return queue.add(e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public boolean remove(Object o) { |
| 107 | + synchronized (this) { |
| 108 | + return queue.remove(o); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public boolean containsAll(Collection<?> c) { |
| 113 | + synchronized (this) { |
| 114 | + return queue.containsAll(c); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public boolean addAll(Collection<? extends E> c) { |
| 119 | + synchronized (this) { |
| 120 | + return queue.addAll(c); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public boolean removeAll(Collection<?> c) { |
| 125 | + synchronized (this) { |
| 126 | + return queue.removeAll(c); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public boolean retainAll(Collection<?> c) { |
| 131 | + synchronized (this) { |
| 132 | + return queue.retainAll(c); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public void clear() { |
| 137 | + synchronized (this) { |
| 138 | + queue.clear(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public E remove() { |
| 143 | + synchronized (this) { |
| 144 | + return queue.remove(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + public E element() { |
| 149 | + synchronized (this) { |
| 150 | + return queue.element(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public boolean equals(Object other) { |
| 155 | + if (other == null) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + if (!other.getClass().isInstance(this)) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + SynchronizedBoundedPriorityQueue<E> other_synced = |
| 164 | + (SynchronizedBoundedPriorityQueue<E>) other; |
| 165 | + |
| 166 | + synchronized (this) { |
| 167 | + return queue.equals(other_synced.queue); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public String toString() { |
| 172 | + synchronized (this) { |
| 173 | + return queue.toString(); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments