Skip to content

Commit

Permalink
Converter refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jul 17, 2024
1 parent db06596 commit 6541658
Showing 1 changed file with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,20 @@ private static List<NodeFactory> createDefaultFactories() {

@NotNull
Node convertToNode(@Nullable Object source, String label, boolean lenient) {
for (int i = 0; i < factories.size(); i++) {
NodeFactory factory = factories.get(i);
if (isLastFactory(i) || factory.isPreferredFor(source)) {
return factory.convertToNode(source, label, lenient);
}
}
throw new IllegalStateException("Should not happen");
return findBestFactory(source).convertToNode(source, label, lenient);
}

@NotNull
Node valueToNode(Object source) {
for (int i = 0; i < factories.size(); i++) {
NodeFactory factory = factories.get(i);
if (isLastFactory(i) || factory.isPreferredFor(source)) {
return factory.valueToNode(source);
}
}
throw new IllegalStateException("Should not happen");
return findBestFactory(source).valueToNode(source);
}

private boolean isLastFactory(int i) {
return factories.size() - 1 == i;
private NodeFactory findBestFactory(Object source) {
if (factories.size() == 1) return factories.get(0);

return factories.stream()
.filter(factory -> factory.isPreferredFor(source))
.findFirst()
.orElseGet(() -> factories.get(factories.size() - 1));
}
}

0 comments on commit 6541658

Please sign in to comment.