package relop;

/**
 * The projection operator extracts columns from a relation; unlike in
 * relational algebra, this operator does NOT eliminate duplicate tuples.
 */
public class Projection extends Iterator {
	private Iterator iter;
	private Integer[] fields;

	/**
	 * Constructs a projection, given the underlying iterator and field numbers.
	 */
	public Projection(Iterator iter, Integer... fields) {
		this.iter = iter;
		this.fields = fields;
		this.setSchema(this.iter.getSchema());

		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Gives a one-line explaination of the iterator, repeats the call on any
	 * child iterators, and increases the indent depth along the way.
	 */
	public void explain(int depth) {
		// TODO: Done
		this.indent(depth++);
		System.out.println("Projection depth: " + depth);
		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Restarts the iterator, i.e. as if it were just constructed.
	 */
	public void restart() {
		// TODO: Done
		this.iter.restart();
		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Returns true if the iterator is open; false otherwise.
	 */
	public boolean isOpen() {
		// TODO: Done
		if (this.iter.isOpen())
			return true;
		return false;

		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Closes the iterator, releasing any resources (i.e. pinned pages).
	 */
	public void close() {
		// TODO: Done
		this.iter.close();
		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Returns true if there are more tuples, false otherwise.
	 */
	public boolean hasNext() {
		// TODO: projection has next
		if (this.iter.hasNext())
			return true;
		return false;
		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Gets the next tuple in the iteration.
	 * 
	 * @throws IllegalStateException
	 *             if no more tuples
	 */
	public Tuple getNext() {
		// TODO: projection get next tuple

		throw new UnsupportedOperationException("Not implemented");
	}

} // public class Projection extends Iterator

@@ -464,46 +464,222 @@
 s;%0D%0A
-%09%09this.setSchema(this.iter.getSchema()
+%0D%0A%09%09// new schema for projection%0D%0A%09%09Schema new_schema = new Schema(fields.length);%0D%0A%09%09for (int i = 0; i %3C fields.length; i++) %7B%0D%0A%09%09%09new_schema.initField(i, iter.getSchema(), fields%5Bi%5D);%0D%0A%09%09%7D%0D%0A%09%09setSchema(new_schema
 );%0D%0A
@@ -2260,20 +2260,260 @@
  tuple%0D%0A
-%0D%0A
 %09%09
+Tuple parent = iter.getNext();%0D%0A%09%09Tuple t = new Tuple(this.getSchema());%0D%0A%09%09for (int i = 0; i %3C fields.length; i++) %7B%0D%0A%09%09%09t.setField(i, parent.getField(i));%0D%0A%09%09%7D%0D%0A%09%09return t;%0D%0A%0D%0A%09%09// throw new IllegalStateException(%22No more tuple!%22);%0D%0A%0D%0A%09%09// 
 throw ne

Update projection - create new schema & update getNext()