Skip to content

Commit e7581a8

Browse files
authored
Merge pull request #1477 from kezhenxu94/polishing/cleanup
extract duplicate code blocks
2 parents 9365a20 + c788fd7 commit e7581a8

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/main/java/org/apache/ibatis/type/EnumOrdinalTypeHandler.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,33 @@ public E getNullableResult(ResultSet rs, String columnName) throws SQLException
4949
int i = rs.getInt(columnName);
5050
if (i == 0 && rs.wasNull()) {
5151
return null;
52-
} else {
53-
try {
54-
return enums[i];
55-
} catch (Exception ex) {
56-
throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
57-
}
5852
}
53+
return tryConvertIndexToEnum(i);
5954
}
6055

6156
@Override
6257
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
6358
int i = rs.getInt(columnIndex);
6459
if (i == 0 && rs.wasNull()) {
6560
return null;
66-
} else {
67-
try {
68-
return enums[i];
69-
} catch (Exception ex) {
70-
throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
71-
}
7261
}
62+
return tryConvertIndexToEnum(i);
7363
}
7464

7565
@Override
7666
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
7767
int i = cs.getInt(columnIndex);
7868
if (i == 0 && cs.wasNull()) {
7969
return null;
80-
} else {
81-
try {
82-
return enums[i];
83-
} catch (Exception ex) {
84-
throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
85-
}
8670
}
71+
return tryConvertIndexToEnum(i);
8772
}
8873

74+
private E tryConvertIndexToEnum(int i) {
75+
try {
76+
return enums[i];
77+
} catch (Exception ex) {
78+
throw new IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
79+
}
80+
}
8981
}

0 commit comments

Comments
 (0)