package cop3530; public class BogusSet implements Set { public BogusSet( ) { items = (AnyType []) new Object[ 5 ]; size = 0; } public boolean isEmpty( ) { return getSize( ) == 0; } public int getSize( ) { return size; } /* * Does not maintain sorted order! */ public boolean add( AnyType x ) { if( size == items.length ) { AnyType [ ] old = items; items = (AnyType[]) new Object[ old.length * 2 + 1 ]; for( int i = 0; i < old.length; i++ ) items[ i ] = old[ i ]; } items[ size++ ] = x; return true; } public boolean contains( AnyType x ) { throw new UnsupportedOperationException( ); } public boolean remove( AnyType x ) { throw new UnsupportedOperationException( ); } public String toString( ) { StringBuilder result = new StringBuilder( "[" ); for( int i = 0; i < size; i++ ) result.append( " " + items[ i ] ); result.append( " ]" ); return new String( result ); } public java.util.Iterator iterator( ) { return new MyIterator( ); } private AnyType [ ] items; private int size; private class MyIterator implements java.util.Iterator { public boolean hasNext( ) { return currentPos != size; } public AnyType next( ) { if( !hasNext( ) ) throw new IndexOutOfBoundsException( ); okToRemove = true; return items[ currentPos++ ]; } public void remove( ) { if( !okToRemove ) throw new IllegalStateException( ); BogusSet.this.remove( items[ --currentPos ] ); okToRemove = false; } private int currentPos = 0; private boolean okToRemove = false; } }