Make OidList Iterable

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6426 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Tom Conkling
2011-01-10 22:47:09 +00:00
parent 0e8f5e4da4
commit 5c3e0ed825
@@ -21,6 +21,9 @@
package com.threerings.presents.dobj;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.threerings.io.Streamable;
/**
@@ -35,7 +38,8 @@ import com.threerings.io.Streamable;
* <li> When an object is destroyed, its oid is automagically removed from any OidLists.
* </ul>
*/
public class OidList implements Streamable
public class OidList
implements Streamable, Iterable<Integer>
{
/**
* Creates an empty oid list.
@@ -148,6 +152,11 @@ public class OidList implements Streamable
return buf.toString();
}
public Iterator<Integer> iterator ()
{
return new OidIterator();
}
private void expand ()
{
int[] oids = new int[_oids.length*2];
@@ -155,6 +164,30 @@ public class OidList implements Streamable
_oids = oids;
}
protected class OidIterator
implements Iterator<Integer>
{
public boolean hasNext ()
{
return _index < size();
}
public Integer next ()
{
if (!hasNext()) {
throw new NoSuchElementException();
}
return get(_index++);
}
public void remove ()
{
throw new UnsupportedOperationException();
}
protected int _index = 0;
}
private int[] _oids;
private int _size;