/***********************************************************************************************************************
 *
 * Copyright (C) 2010 by the Stratosphere project (http://stratosphere.eu)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 **********************************************************************************************************************/

package eu.stratosphere.nephele.io;

import java.io.IOException;

import eu.stratosphere.nephele.event.task.AbstractTaskEvent;
import eu.stratosphere.nephele.execution.Environment;
import eu.stratosphere.nephele.template.AbstractInvokable;
import eu.stratosphere.nephele.types.Record;

/**
 * This is an abstract base class for a record reader, either dealing with mutable or immutable records.
 * 
 * @param <T> The type of the record that can be read from this record reader.
 */
public abstract class AbstractSingleGateRecordReader<T extends Record> extends AbstractRecordReader {
	
	/**
	 * The input gate associated with the record reader.
	 */
	protected final InputGate<T> inputGate;
	
	// --------------------------------------------------------------------------------------------

	protected AbstractSingleGateRecordReader(AbstractInvokable invokable, RecordDeserializerFactory<T> deserializerFactory, int inputGateID) {
		Environment environment = invokable.getEnvironment();
		GateID gateID = environment.getNextUnboundInputGateID();
		if (gateID == null) {
			gateID = new GateID();
		}

		this.inputGate = environment.createInputGate(gateID, deserializerFactory);
		environment.registerInputGate(this.inputGate);
	}

	/**
	 * Returns the number of input channels wired to this reader's input gate.
	 * 
	 * @return the number of input channels wired to this reader's input gate
	 */
	public int getNumberOfInputChannels() {
		return this.inputGate.getNumberOfInputChannels();
	}

	/**
	 * Checks if the input channel with the given index is closed.
	 * 
	 * @param index
	 *        the index of the input channel
	 * @return <code>true</code> if the respective input channel is already closed, otherwise <code>false</code>
	 * @throws IOException
	 *         thrown if an error occurred while closing the input channel
	 * @throws InterruptedException
	 *         thrown if the channel is interrupted while processing this call
	 */
	public boolean isInputChannelClosed(final int index) throws IOException, InterruptedException {
		if (index < this.inputGate.getNumberOfInputChannels()) {
			return this.inputGate.getInputChannel(index).isClosed();
		}
		return false;
	}

	/**
	 * Publishes an event.
	 * 
	 * @param event
	 *        the event to be published
	 * @throws IOException
	 *         thrown if an error occurs while transmitting the event
	 * @throws InterruptedException
	 *         thrown if the thread is interrupted while waiting for the event to be published
	 */
	public void publishEvent(final AbstractTaskEvent event) throws IOException, InterruptedException {
		// Delegate call to input gate to send events
		this.inputGate.publishEvent(event);
	}
	
	
	InputGate<T> getInputGate() {
		return this.inputGate;
	}
}
