Help AI coding agents use Java Optional well in new code and cleanups, without replacing one antipattern with another.
100
100%
Does it follow best practices?
Impact
100%
2.08xAverage score across 4 eval scenarios
Passed
No known issues
Create WorkflowPortLookup.java with the revised class. Assume Java 8.
The current method was written in a quick pass; add the new parsing behavior and leave the method
cleaner than you found it.
Current code:
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
final class WorkflowPortLookup {
Optional<Integer> workflowServerPortReservation(Optional<Map<String, Object>> frontMatter, Path workflowPath) {
if (!frontMatter.isPresent()) {
return Optional.empty();
}
Object value = frontMatter.get().get("server_port");
if (value instanceof Number) {
Number number = (Number) value;
return Optional.of(number.intValue());
}
return Optional.empty();
}
}Required changes:
Number values.String values.Optional.empty() for absent front matter, missing values, blank strings, malformed strings, and unsupported types.workflowPath in output; it is included for parity with the real method.