java: List wrapper where get()/set() is allowed but add/remove is not
Posted
by Jason S
on Stack Overflow
See other posts from Stack Overflow
or by Jason S
Published on 2010-04-05T17:15:11Z
Indexed on
2010/04/05
17:23 UTC
Read the original article
Hit count: 499
I need to wrap a List<T>
with some class that allows calls to set/get but does not allow add/remove calls, so that the list remains "stuck" at a fixed length. I think I have a thin wrapper class (below) that will work, but I'm not 100% positive.
Did I miss anything obvious?
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
class RestrictedListWrapper<T> implements List<T>
{
static <T> T fail() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
static private class IteratorWrapper<T> implements ListIterator<T>
{
final private ListIterator<T> iter;
private IteratorWrapper(ListIterator<T> iter) { this.iter = iter; }
static public <T> RestrictedListWrapper.IteratorWrapper<T> wrap(ListIterator<T> target) {
return new RestrictedListWrapper.IteratorWrapper<T>(target);
}
@Override public void add(T e) { fail(); }
@Override public boolean hasNext() { return this.iter.hasNext(); }
@Override public boolean hasPrevious() { return this.iter.hasPrevious(); }
@Override public T next() { return this.iter.next(); }
@Override public int nextIndex() { return this.iter.nextIndex(); }
@Override public T previous() { return this.iter.previous(); }
@Override public int previousIndex() { return this.iter.previousIndex(); }
@Override public void remove() { fail(); }
@Override public void set(T e) { this.iter.set(e); }
}
final private List<T> list;
private RestrictedListWrapper(List<T> list) {
this.list = list;
}
static public <T> RestrictedListWrapper<T> wrap(List<T> target) {
return new RestrictedListWrapper<T>(target);
}
@Override public boolean add(T arg0) { return fail(); }
@Override public void add(int index, T element) { fail(); }
@Override public boolean addAll(Collection<? extends T> arg0) {
return fail();
}
@Override public boolean addAll(int arg0, Collection<? extends T> arg1) {
return fail();
}
/**
* clear() allows setting all members of the list to null
*/
@Override public void clear() {
ListIterator<T> it = this.list.listIterator();
while (it.hasNext())
{
it.set(null);
it.next();
}
}
@Override public boolean contains(Object o) {
return this.list.contains(o);
}
@Override public boolean containsAll(Collection<?> c) {
return this.list.containsAll(c);
}
@Override public T get(int index) { return this.list.get(index); }
@Override public int indexOf(Object o) { return this.list.indexOf(o); }
@Override public boolean isEmpty() { return false; }
@Override public Iterator<T> iterator() {
return listIterator();
}
@Override public int lastIndexOf(Object o) { return this.list.lastIndexOf(o); }
@Override public ListIterator<T> listIterator() {
return IteratorWrapper.wrap(this.list.listIterator());
}
@Override public ListIterator<T> listIterator(int index) {
return IteratorWrapper.wrap(this.list.listIterator(index));
}
@Override public boolean remove(Object o) { return fail(); }
@Override public T remove(int index) { fail(); return fail(); }
@Override public boolean removeAll(Collection<?> c) { return fail(); }
@Override public boolean retainAll(Collection<?> c) { return fail(); }
@Override public T set(int index, T element) { return this.list.set(index, element); }
@Override public int size() { return this.list.size(); }
@Override public List<T> subList(int fromIndex, int toIndex) {
return new RestrictedListWrapper<T>(this.list.subList(fromIndex, toIndex));
}
@Override public Object[] toArray() { return this.list.toArray(); }
@Override public <T> T[] toArray(T[] a) { return this.list.toArray(a); }
}
© Stack Overflow or respective owner