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 CommandSanitizer.java with the revised class. Assume Java 17.
Current code:
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
final class CommandSanitizer {
private static final Set<String> SECRET_OPTIONS = Set.of("--token", "--key", "--workflow", "--config-dir", "--state-home", "--output");
String sanitize(List<String> args) {
List<String> sanitized = new ArrayList<>();
boolean redactNext = false;
for (String arg : args) {
if (redactNext) {
sanitized.add("<redacted>");
redactNext = false;
continue;
}
Optional<String> option = SECRET_OPTIONS.stream()
.filter(secret -> arg.equals(secret))
.findFirst();
if (option.isPresent()) {
sanitized.add(option.orElseThrow());
redactNext = true;
} else {
sanitized.add(arg);
}
}
return String.join(" ", sanitized);
}
}Required changes:
--token abc becomes --token <redacted>.option=value behavior: --key=abc becomes --key=<redacted>.String.join(" ", sanitized).