Skip to content

Commit dc1a4f7

Browse files
committed
Refactor using Stream.reduce()
1 parent 8c7bbf3 commit dc1a4f7

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -710,22 +710,21 @@ private Optional<Constructor<?>> findConstructorForAutomapping(final Class<?> re
710710
if (constructors.length == 1) {
711711
return Optional.of(constructors[0]);
712712
}
713-
List<Constructor<?>> automapConstructors = Arrays.stream(constructors).filter(x -> x.isAnnotationPresent(AutomapConstructor.class)).toList();
714-
if (automapConstructors.size() > 1) {
715-
throw new ExecutorException("@AutomapConstructor should be used in only one constructor.");
716-
}
717-
if (automapConstructors.size() == 1) {
718-
return Optional.of(automapConstructors.get(0));
719-
}
720-
if (configuration.isArgNameBasedConstructorAutoMapping()) {
721-
// Finding-best-match type implementation is possible,
722-
// but using @AutomapConstructor seems sufficient.
723-
throw new ExecutorException(MessageFormat.format(
724-
"'argNameBasedConstructorAutoMapping' is enabled and the class ''{0}'' has multiple constructors, so @AutomapConstructor must be added to one of the constructors.",
725-
resultType.getName()));
726-
} else {
727-
return Arrays.stream(constructors).filter(x -> findUsableConstructorByArgTypes(x, rsw.getJdbcTypes())).findAny();
728-
}
713+
return Arrays.stream(constructors)
714+
.filter(x -> x.isAnnotationPresent(AutomapConstructor.class))
715+
.reduce((x, y) -> {
716+
throw new ExecutorException("@AutomapConstructor should be used in only one constructor.");
717+
}).or(() -> {
718+
if (configuration.isArgNameBasedConstructorAutoMapping()) {
719+
// Finding-best-match type implementation is possible,
720+
// but using @AutomapConstructor seems sufficient.
721+
throw new ExecutorException(MessageFormat.format(
722+
"'argNameBasedConstructorAutoMapping' is enabled and the class ''{0}'' has multiple constructors, so @AutomapConstructor must be added to one of the constructors.",
723+
resultType.getName()));
724+
} else {
725+
return Arrays.stream(constructors).filter(x -> findUsableConstructorByArgTypes(x, rsw.getJdbcTypes())).findAny();
726+
}
727+
});
729728
}
730729

731730
private boolean findUsableConstructorByArgTypes(final Constructor<?> constructor, final List<JdbcType> jdbcTypes) {

0 commit comments

Comments
 (0)