package simpledb;

import java.util.Collection;
import java.util.NoSuchElementException;

public class Drop extends Operator {
	private final Collection<String> dropFields;
	private final DbIterator child;

	public Drop(Collection<String> dropFields, DbIterator child) {
		this.dropFields = dropFields;
		this.child = child;
	}

	@Override
	public void open() throws DbException, NoSuchElementException, TransactionAbortedException {
		super.open();
		child.open();
	}

	@Override
	public void close() {
		super.close();
		child.close();
	}

	@Override
	public void rewind() throws DbException, TransactionAbortedException {
		child.rewind();
	}

	@Override
	protected Tuple fetchNext() throws DbException, TransactionAbortedException {
		TupleDesc td = child.getTupleDesc();
		while (child.hasNext()) {
        	Tuple t = child.next();
        	boolean drop = false;
        	for (String field : dropFields) {
        		if (t.getField(td.fieldNameToIndex(field)).isMissing()) {
        			drop = true;
        			break;
        		}
        	}
        	if (!drop) {
        		return t;
        	}
        }
        return null;
	}

	@Override
	public DbIterator[] getChildren() {
		return new DbIterator[] { child };
	}

	@Override
	public void setChildren(DbIterator[] children) {
		throw new RuntimeException("Not implemented.");
	}

	@Override
	public TupleDesc getTupleDesc() {
		return child.getTupleDesc();
	}
}

@@ -45,86 +45,95 @@
 on;%0A
-import java.util.NoSuchElementException;%0A%0Apublic class Drop extends Operator %7B
+%0Apublic class Drop extends Impute %7B%0A%0A%09private static final long serialVersionUID = 1L;%0A
 %0A%09pr
@@ -179,41 +179,181 @@
 ds;%0A
-%09private final DbIterator
+%0A%09/**%0A%09 * Deal with missing data on a set of column by simply dropping the rows%0A%09 * with missing data.%0A%09 * @param dropFields set of columns to consider%0A%09 * @param
  child
-;
 %0A
+%09 */
 %0A%09pu
@@ -409,24 +409,40 @@
 or child) %7B%0A
+%09%09super(child);%0A
 %09%09this.dropF
@@ -465,243 +465,8 @@
 ds;%0A
-%09%09this.child = child;%0A%09%7D%0A%0A%09@Override%0A%09public void open() throws DbException, NoSuchElementException, TransactionAbortedException %7B%0A%09%09super.open();%0A%09%09child.open();%0A%09%7D%0A%0A%09@Override%0A%09public void close() %7B%0A%09%09super.close();%0A%09%09child.close();%0A
 %09%7D%0A%0A
@@ -677,22 +677,16 @@
 sc td = 
-child.
 getTuple
@@ -1052,291 +1052,5 @@
 %0A%09%7D%0A
-%0A%09@Override%0A%09public DbIterator%5B%5D getChildren() %7B%0A%09%09return new DbIterator%5B%5D %7B child %7D;%0A%09%7D%0A%0A%09@Override%0A%09public void setChildren(DbIterator%5B%5D children) %7B%0A%09%09throw new RuntimeException(%22Not implemented.%22);%0A%09%7D%0A%0A%09@Override%0A%09public TupleDesc getTupleDesc() %7B%0A%09%09return child.getTupleDesc();%0A%09%7D%0A%7D%0A
+%7D

Refactor Drop to extend abstract Impute class