package <%=compileHelper.getPackageName()%>;

import java.io.ObjectInputStream;
import java.util.List;
import java.util.Map;

import com.hammurapi.extract.CompiledExtractorBase;
import com.hammurapi.extract.Extractor;
import com.hammurapi.extract.Predicate;

@SuppressWarnings("unchecked")
class <%=compileHelper.getClassName()%> extends CompiledExtractorBase<<%=compileHelper.getT()%>, <%=compileHelper.getV()%>, <%=compileHelper.getC()%>> <% if (compileHelper.isPredicate()) { %> implements Predicate<<%=compileHelper.getT()%>,<%=compileHelper.getC()%>> <% } %> {
	
	/**
	 * Class identity for comparison with other classes.
	 */
	private static final List<Object> IDENTITY;
	
	static {
		try {
			ObjectInputStream ois = new ObjectInputStream(<%=compileHelper.getClassName()%>.class.getResourceAsStream("<%=compileHelper.getClassName()%>.identity"));
			try {
				IDENTITY = (List<Object>) ois.readObject();
			} finally {
				ois.close();
			}
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	<%=compileHelper.getClassName()%>(int[] map) {
		super(map, <%=compileHelper.isContextDependent()%>, <%=compileHelper.getParameterIndicesCSV()%>); 
	}

	@Override
	protected <%=compileHelper.getV()%> extractInternal(<%=compileHelper.getC()%> eCtx, Map<<%=compileHelper.getC()%>, Map<Extractor<<%=compileHelper.getT()%>, ? super <%=compileHelper.getV()%>, <%=compileHelper.getC()%>>, ? super <%=compileHelper.getV()%>>> cache, <%=compileHelper.getT()%>... obj) {		
		<% for (Object parameter: compileHelper.getParameters()) { %>
		
		<%=parameter.getType().getCanonicalName()%> <%=parameter.getName()%> = <%=compileHelper.maybeCastParameter(parameter)%> obj[mapArg(<%=parameter.getIndex()%>)];
		<% } %>
		
		return <%=compileHelper.getExpression()%>;
	}

	@Override
	protected List<Object> getIdentity() {
		return IDENTITY;
	}

	@Override
	protected CompiledExtractorBase<<%=compileHelper.getT()%>, <%=compileHelper.getV()%>, <%=compileHelper.getC()%>> newInstance(int[] map) {
		return new <%=compileHelper.getClassName()%>(map);
	}
	
	public String toString() {
		return getClass().getName()+" [Expression = <%=compileHelper.getEscapedExpression()%>, Parameters = <%=compileHelper.getParameters()%>, Parameter indices = "+parameterIndices()+"]";
	}
	
}
