/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    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 org.apache.ibatis.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Clinton Begin
 */
public class EnumOrdinalTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {

  private final Class<E> type;
  private final E[] enums;

  public EnumOrdinalTypeHandler(Class<E> type) {
    if (type == null) {
      throw new IllegalArgumentException("Type argument cannot be null");
    }
    this.type = type;
    this.enums = type.getEnumConstants();
    if (this.enums == null) {
      throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
    }
  }

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
    ps.setInt(i, parameter.ordinal());
  }

  @Override
  public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
    int i = rs.getInt(columnName);
    if (i == 0 && rs.wasNull()) {
      return null;
    } else {
      try {
        return enums[i];
      } catch (Exception ex) {
        throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
      }
    }
  }

  @Override
  public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    int i = rs.getInt(columnIndex);
    if (i == 0 && rs.wasNull()) {
      return null;
    } else {
      try {
        return enums[i];
      } catch (Exception ex) {
        throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
      }
    }
  }

  @Override
  public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    int i = cs.getInt(columnIndex);
    if (i == 0 && cs.wasNull()) {
      return null;
    } else {
      try {
        return enums[i];
      } catch (Exception ex) {
        throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
      }
    }
  }

}

@@ -1707,632 +1707,276 @@
    %7D
- else %7B%0A      try %7B%0A        return enums%5Bi%5D;%0A      %7D catch (Exception ex) %7B%0A        throw new IllegalArgumentException(%22Cannot convert %22 + i + %22 to %22 + type.getSimpleName() + %22 by ordinal value.%22, ex);%0A      %7D%0A    %7D%0A  %7D%0A%0A  @Override%0A  public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException %7B%0A    int i = rs.getInt(columnIndex);%0A    if (i == 0 && rs.wasNull()) %7B%0A      return null;%0A    %7D else %7B%0A      try %7B%0A        return enums%5Bi%5D;%0A      %7D catch (Exception ex) %7B%0A        throw new IllegalArgumentException(%22Cannot convert %22 + i + %22 to %22 + type.getSimpleName() + %22 by ordinal value.%22, ex);%0A      %7D%0A    %7D
+%0A    return tryConvertIndexToEnum(i);%0A  %7D%0A%0A  @Override%0A  public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException %7B%0A    int i = rs.getInt(columnIndex);%0A    if (i == 0 && rs.wasNull()) %7B%0A      return null;%0A    %7D%0A    return tryConvertIndexToEnum(i);
 %0A  %7D
@@ -2177,36 +2177,110 @@
    %7D
- else %7B%0A      try
+%0A    return tryConvertIndexToEnum(i);%0A  %7D%0A%0A  private E tryConvertIndexToEnum(int i)
  %7B%0A    
+try %7B%0A
     
+  
 retu
@@ -2292,18 +2292,16 @@
 ums%5Bi%5D;%0A
-  
     %7D ca
@@ -2317,26 +2317,24 @@
 ption ex) %7B%0A
-  
       throw 
@@ -2449,16 +2449,8 @@
 x);%0A
-      %7D%0A
     
@@ -2455,11 +2455,10 @@
   %7D%0A  %7D%0A
-%0A
 %7D%0A

extract duplicate code blocks