Use Java streams where applicable - #4
Open
jingyucodes wants to merge 5 commits into
Open
jingyucodes wants to merge 5 commits into
jingyucodes wants to merge 5 commits into
Conversation
The "on" and "find" cases in executeCommand() each built a matches list by looping over every task and manually adding the ones that passed a single predicate (occursOn()/matchesKeyword()) — the classic filter-then-collect shape streams exist for, with no early exit, no branching, and no side effects beyond building the result. Replace each loop with tasks.getAll().stream().filter(...) .collect(Collectors.toList()), which states the intent (keep tasks matching this predicate) directly instead of via a manual accumulator loop. Other loops in the codebase (e.g. Ui's numbered task listings, Echo's console read loop) are intentionally left as-is: they either need the running index for output (streams don't index cleanly) or involve stateful, sequential I/O, neither of which streams express more clearly than a plain loop.
save() built its list of save-file lines by looping over tasks and adding task.toFileFormat() for each one — a plain 1-to-1 transformation with no filtering, branching, or side effects, which is exactly what Stream#map exists to express. Replace the loop with tasks.stream().map(Task::toFileFormat) .collect(Collectors.toList()), naming the operation (map) instead of spelling out the accumulator loop that implements it.
load() looped over every line, skipped blanks with a bare continue, parsed the rest, then conditionally added successfully-parsed tasks. That's a filter (skip blank lines) -> map (parse) -> filter (drop failed parses) pipeline expressed as a loop with a mutable accumulator and a continue, which streams state more directly. Rewrite it as Files.readAllLines(filePath).stream() .filter(line -> !line.isBlank()).map(this::parseLine) .filter(Objects::nonNull).collect(Collectors.toList()), returning the stream's result directly instead of building it up in a pre-declared list. parseLine() still prints its own warning for a corrupted line as a side effect inside map(); that's unchanged from before and kept for the same reason (surface the problem without aborting the whole load).
# Conflicts: # src/main/java/echo/storage/Storage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Echo#executeCommand()'son/findcases: replace manual filter-and-accumulate loops withstream().filter(...).collect(Collectors.toList()).Storage#save(): replace the manual task-to-line loop withstream().map(Task::toFileFormat).collect(Collectors.toList()).Storage#load(): replace the filter/parse/filter loop with a singlefilter -> map -> filterstream pipeline.Streams were applied only where the existing loop was a plain filter/map/collect with no early exit, branching, or meaningful side effects. Left as plain loops (and noted in the first commit's message):
Ui's numbered task listings (need the running index),Echo#run()'s console read loop (stateful I/O with an early break), andDialogBox#flip()'sCollections.reverse()call (already a single clean statement).Test plan
./gradlew compileJava checkstyleMainpassestodo/deadline/event/mark/find/on, then reload and confirm the save file round-trips correctly