"Disable" iterator's remove( ) method?

K

Ken

Hi. I noticed that the iterator interface says that remove() is
optional. I have an ArrayList Iterator that I'm getting, then passing
around to clients through the Iterator interface. The goal is to not
have clients care whether I have a LinkedList, ArrayList, etc. My
concern is that I don't want to allow remove() to be available through
the iterator. However, when I call "iterator" on the list, the
iterator I get allows "remove" to be called. How do I "disable" this
method?

Thanks!

Ken
 
C

Chris Smith

Ken said:
Hi. I noticed that the iterator interface says that remove() is
optional. I have an ArrayList Iterator that I'm getting, then passing
around to clients through the Iterator interface. The goal is to not
have clients care whether I have a LinkedList, ArrayList, etc. My
concern is that I don't want to allow remove() to be available through
the iterator. However, when I call "iterator" on the list, the
iterator I get allows "remove" to be called. How do I "disable" this
method?

You can wrap the Iterator in something like this:

public class UnmodifiableIterator
{
private Iterator base;

public UnmodifiableIterator(Iterator base)
{
this.base = base;
}

public boolean hasNext()
{
return base.hasNext();
}

public Object next()
{
return base.next();
}

public void remove()
{
throw new UnsupportedOperationException();
}
}

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Christophe Vanfleteren

Ken said:
Hi. I noticed that the iterator interface says that remove() is
optional. I have an ArrayList Iterator that I'm getting, then passing
around to clients through the Iterator interface. The goal is to not
have clients care whether I have a LinkedList, ArrayList, etc. My
concern is that I don't want to allow remove() to be available through
the iterator. However, when I call "iterator" on the list, the
iterator I get allows "remove" to be called. How do I "disable" this
method?

Thanks!

Ken

Return a unmodifiableList instead of the actual list:
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#unmodifiableList(java.util.List)>
 
X

xarax

Chris Smith said:
You can wrap the Iterator in something like this:

public class UnmodifiableIterator

implements Iterator
{
private Iterator base;

public UnmodifiableIterator(Iterator base)
{
this.base = base;
}

public boolean hasNext()
{
return base.hasNext();
}

public Object next()
{
return base.next();
}

public void remove()
{
throw new UnsupportedOperationException();
}
}

Then pass around the UnmodifiableIterator as a
simple Iterator reference, because it implements
Iterator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top