Skip to content

Commit 7e52d8f

Browse files
committed
Let LastRecentlyUsed implement the full Collection contract
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent b7904e0 commit 7e52d8f

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

src/main/java/org/scijava/util/LastRecentlyUsed.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava.util;
3333

34+
import java.lang.reflect.Array;
35+
import java.util.Collection;
3436
import java.util.HashMap;
3537
import java.util.HashSet;
3638
import java.util.Iterator;
@@ -42,7 +44,7 @@
4244
*
4345
* @author Johannes Schindelin
4446
*/
45-
public class LastRecentlyUsed<T> implements Iterable<T> {
47+
public class LastRecentlyUsed<T> implements Iterable<T>, Collection<T> {
4648
private final Object[] entries;
4749
private final Map<T, Integer> map;
4850
/**
@@ -161,6 +163,96 @@ public void clear() {
161163
}
162164
}
163165

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+
164256
/**
165257
* Returns an {@link Iterator}.
166258
*

0 commit comments

Comments
 (0)