
package cop3530;

public class BogusSet<AnyType> implements Set<AnyType>
{
    public BogusSet( )
    {
        items = (AnyType []) new Object[ 5 ];
        size  = 0;
    }
    
    public Object [ ] toArray( )
    {
        Object [ ] copy = new Object[ size ];
        for( int i = 0; i < size; i++ )
            copy[ i ] = items[ i ];
        return copy;
    }
    
    public AnyType [ ] toArray( AnyType [ ] copy )
    {
        if( copy.length < size )
            throw new IllegalArgumentException( );
            
        for( int i = 0; i < size; i++ )
            copy[ i ] = items[ i ];
        return copy;
    }
    
    public boolean isEmpty( )
    {
        return getSize( ) == 0;
    }
    
    public int getSize( )
    {
        return size;
    }
    
    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<AnyType> iterator( )
    {
        return new MyIterator( );
    }

    private AnyType [ ] items;
    private int size;
    
    private class MyIterator implements java.util.Iterator<AnyType>
    {
        public boolean hasNext( )
        {
            return currentPos != size;
        }
        
        public AnyType next( )
        {
            if( !hasNext( ) )
                throw new IndexOutOfBoundsException( );
            return items[ currentPos++ ];
        }
        
        public void remove( )
        {
            BogusSet.this.remove( items[ --currentPos ] );
        }
        
        private int currentPos = 0;
    }

}

