|
31 | 31 |
|
32 | 32 | package org.scijava.util; |
33 | 33 |
|
| 34 | +import java.lang.reflect.Array; |
| 35 | +import java.util.Collection; |
34 | 36 | import java.util.HashMap; |
35 | 37 | import java.util.HashSet; |
36 | 38 | import java.util.Iterator; |
|
42 | 44 | * |
43 | 45 | * @author Johannes Schindelin |
44 | 46 | */ |
45 | | -public class LastRecentlyUsed<T> implements Iterable<T> { |
| 47 | +public class LastRecentlyUsed<T> implements Iterable<T>, Collection<T> { |
46 | 48 | private final Object[] entries; |
47 | 49 | private final Map<T, Integer> map; |
48 | 50 | /** |
@@ -161,6 +163,96 @@ public void clear() { |
161 | 163 | } |
162 | 164 | } |
163 | 165 |
|
| 166 | + @Override |
| 167 | + public boolean addAll(final Collection<? extends T> values) { |
| 168 | + for (final T value : values) { |
| 169 | + add(value); |
| 170 | + } |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public boolean contains(final Object value) { |
| 176 | + return map.containsKey(value); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public boolean containsAll(final Collection<?> values) { |
| 181 | + return map.keySet().containsAll(values); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean isEmpty() { |
| 186 | + return top == 0; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public boolean remove(Object value) { |
| 191 | + final Integer index = map.get(value); |
| 192 | + if (index == null) return false; |
| 193 | + remove(index.intValue()); |
| 194 | + return true; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public boolean removeAll(Collection<?> values) { |
| 199 | + boolean result = true; |
| 200 | + for (final Object value : values) { |
| 201 | + result = remove(value) && result; |
| 202 | + } |
| 203 | + return result; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public boolean retainAll(Collection<?> values) { |
| 208 | + for (int index = top - 1; index >= 0; ) { |
| 209 | + final int prev = previous[index] - 1; |
| 210 | + if (!values.contains(get(index))) { |
| 211 | + remove(index); |
| 212 | + } |
| 213 | + index = prev; |
| 214 | + } |
| 215 | + return containsAll(values); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public int size() { |
| 220 | + return map.size(); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public Object[] toArray() { |
| 225 | + final Object[] result = new Object[size()]; |
| 226 | + for (int i = 0, index = top - 1; index >= 0; i++, index = |
| 227 | + previous[index] - 1) |
| 228 | + { |
| 229 | + result[i] = get(index); |
| 230 | + } |
| 231 | + return result; |
| 232 | + } |
| 233 | + |
| 234 | + @SuppressWarnings("unchecked") |
| 235 | + @Override |
| 236 | + public <S> S[] toArray(final S[] array) { |
| 237 | + final int size = size(); |
| 238 | + if (array.length >= size) { |
| 239 | + for (int i = 0, index = top - 1; index >= 0; i++, index = |
| 240 | + previous[index] - 1) |
| 241 | + { |
| 242 | + array[i] = (S) get(index); |
| 243 | + } |
| 244 | + return array; |
| 245 | + } |
| 246 | + final S[] result = |
| 247 | + (S[]) Array.newInstance(array.getClass().getComponentType(), size); |
| 248 | + for (int i = 0, index = top - 1; index >= 0; i++, index = |
| 249 | + previous[index] - 1) |
| 250 | + { |
| 251 | + result[i] = (S) get(index); |
| 252 | + } |
| 253 | + return result; |
| 254 | + } |
| 255 | + |
164 | 256 | /** |
165 | 257 | * Returns an {@link Iterator}. |
166 | 258 | * |
|
0 commit comments