/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
<%@ jet package="org.apache.uima.tools.jcasgen" imports="java.util.Iterator org.apache.uima.resource.metadata.TypeDescription org.apache.uima.resource.metadata.FeatureDescription" class="JCasTypeTemplate" implements="Jg.IJCasTypeTemplate" %>

<% 
/* ************************************************************************************************* 
 * File generated from uimaj-tools project: /src/main/javajet/jcasgen/templates/JCasType.javajet
 * Edit that file, and rerun the jet expander, found in the uimaj-jet-expander project in svn
 *   Install that project into Eclipse using 
 *     File -- import -- Projects from Folder or Archive  then select the uimaj-jet-expander folder
 ***************************************************************************************************/

    Object [] args = (Object [])argument;
    Jg jg = (Jg)args[0];
    TypeDescription td = (TypeDescription)args[1]; 
   jg.packageName = jg.getJavaPkg(td); %>
   
/* Apache UIMA v3 - First created by JCasGen <%=jg.getDate()%> */

<% if (0 != jg.packageName.length()) {%>
package <%=jg.packageName%>;
 
<% } 
   else 
     jg.error.newError(IError.WARN, 
		jg.getString("pkgMissing", new Object[] {td.getName()}), null); %>

<% FeatureDescription[] fds = td.getFeatures();
   if(fds.length > 0) { %>
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
<% } %>

import org.apache.uima.cas.impl.CASImpl;
import org.apache.uima.cas.impl.TypeImpl;
<%if (fds.length > 0) { %>
import org.apache.uima.cas.impl.TypeSystemImpl;
<% } %>
import org.apache.uima.jcas.JCas; 
import org.apache.uima.jcas.JCasRegistry;


<% for(Iterator<String> i=jg.collectImports(td, false).iterator(); i.hasNext();) { %>
import <%=i.next()%>;
<% } %>


<% String typeName = jg.getJavaName(td);
%>
/** <%=jg.nullBlank(td.getDescription())%>
 * Updated by JCasGen <%=jg.getDate()%>
 * XML source: <%=jg.xmlSourceFileName%>
 * @generated */
public class <%=typeName%> extends <%=jg.getJavaName(td.getSupertypeName())%> {
 
  /** @generated
   * @ordered 
   */
  @SuppressWarnings ("hiding")
  public final static String _TypeName = "<%=jg.getJavaNameWithPkg(td.getName())%>";
  
  /** @generated
   * @ordered 
   */
  @SuppressWarnings ("hiding")
  public final static int typeIndexID = JCasRegistry.register(<%=typeName%>.class);
  /** @generated
   * @ordered 
   */
  @SuppressWarnings ("hiding")
  public final static int type = typeIndexID;
  /** @generated
   * @return index of the type  
   */
  @Override
  public              int getTypeIndexID() {return typeIndexID;}
 
 <% 
   StringBuilder localData = new StringBuilder();
   StringBuilder featRegistry = new StringBuilder();
    
   featRegistry.append("  /* Feature Adjusted Offsets */\n");
   
   for (FeatureDescription fd : fds) { 

     String featName = fd.getName();
     String featUName = jg.uc1(featName);  // upper case first letter
     if (Jg.reservedFeatureNames.contains(featUName)) {
       jg.error.newError(IError.ERROR, 
                         jg.getString("reservedNameUsed", new Object[] { featName, td.getName() }),
                         null);
     }

     localData   .append("  public final static String _FeatName_").append(featName).append(" = \"").append(featName).append("\";\n");
     
     featRegistry.append("  private final static CallSite _FC_").append(featName)
                 .append(" = TypeSystemImpl.createCallSite(").append(typeName).append(".class, ")
                 .append("\"").append(featName).append("\");\n"); 
     featRegistry.append("  private final static MethodHandle _FH_").append(featName)
                 .append(" = _FC_").append(featName).append(".dynamicInvoker();\n"); 
      
   } /* of Features iteration */ %>

  /* *******************
   *   Feature Offsets *
   * *******************/ 
   
<%=localData%>

<%=featRegistry%>
   
  /** Never called.  Disable default constructor
   * @generated */
  @Deprecated
  @SuppressWarnings ("deprecation")
  protected <%=typeName%>() {/* intentionally empty block */}
    
  /** Internal - constructor used by generator 
   * @generated
   * @param casImpl the CAS this Feature Structure belongs to
   * @param type the type of this Feature Structure 
   */
  public <%=typeName%>(TypeImpl type, CASImpl casImpl) {
    super(type, casImpl);
    readObject();
  }
  
  /** @generated
   * @param jcas JCas to which this Feature Structure belongs 
   */
  public <%=typeName%>(JCas jcas) {
    super(jcas);
    readObject();   
  } 

<%if (jg.isSubTypeOfAnnotation(td)) { %>

  /** @generated
   * @param jcas JCas to which this Feature Structure belongs
   * @param begin offset to the begin spot in the SofA
   * @param end offset to the end spot in the SofA 
  */  
  public <%=typeName%>(JCas jcas, int begin, int end) {
    super(jcas);
    setBegin(begin);
    setEnd(end);
    readObject();
  }   
<%} %>

  /** 
   * <!-- begin-user-doc -->
   * Write your own initialization here
   * <!-- end-user-doc -->
   *
   * @generated modifiable 
   */
  private void readObject() {/*default - does nothing empty block */}
     
<% for (FeatureDescription fd : td.getFeatures()) { 
   
     String featName = fd.getName();
     String featUName = jg.uc1(featName);  // upper case first letter
	 if (Jg.reservedFeatureNames.contains(featUName))
	   jg.error.newError(IError.ERROR, 
		 jg.getString("reservedNameUsed", new Object[] { featName, td.getName() }),
		 null);

     String featDesc = jg.nullBlank(fd.getDescription());
     String featDescCmt = featDesc;

     String rangeType = jg.getJavaRangeType2(fd);
     String elemType = jg.getJavaRangeArrayElementType2(fd); 
     boolean isRangeTypeGeneric = jg.isRangeTypeGeneric(fd);
     boolean isElemTypeGeneric = jg.isElementTypeGeneric(fd);
     

%> 
    
  //*--------------*
  //* Feature: <%=featName%>

  /** getter for <%=featName%> - gets <%=featDescCmt%>
   * @generated
   * @return value of the feature 
   */
<%if (isRangeTypeGeneric) { %>
  @SuppressWarnings("unchecked")
<% } %>
  public <%=rangeType%> get<%=featUName%>() { 
    return <%=jg.getFeatureValue(fd, td)%>;
  }
    
  /** setter for <%=featName%> - sets <%=featDescCmt%> 
   * @generated
   * @param v value to set into the feature 
   */
  public void set<%=featUName%>(<%=rangeType%> v) {
    <%=jg.setFeatureValue(fd, td)%>;
  }    
    
  <%if (jg.hasArrayRange(fd)) {%>
  
  /** indexed getter for <%=featName%> - gets an indexed value - <%=featDescCmt%>
   * @generated
   * @param i index in the array to get
   * @return value of the element at index i 
   */
<%if (isRangeTypeGeneric || isElemTypeGeneric) {%>
  @SuppressWarnings("unchecked")
<%}%>
  public <%=elemType%> get<%=featUName%>(int i) {
     return <%=jg.getArrayFeatureValue(fd, td)%>;
  } 

  /** indexed setter for <%=featName%> - sets an indexed value - <%=featDescCmt%>
   * @generated
   * @param i index in the array to set
   * @param v value to set into the array 
   */
<%if (isRangeTypeGeneric || isElemTypeGeneric) {%>
  @SuppressWarnings("unchecked")
  <% } %>
  public void set<%=featUName%>(int i, <%=elemType%> v) {
    <%=jg.setArrayFeatureValue(fd, td)%>;
  }  
  <% } /* of hasArray */ %>
<% } /* of Features iteration */ %>
<% if (td.getName().equals("uima.cas.Annotation")) { %>
  <%@ include file="../includedTemplates/AnnotationMethods.javajet" %>
<% } /* of Annotation if-statement */ %>
}

    