Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/main/java/ted/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ public void save(TaskList tasks) throws TedException {
Files.createDirectories(parentDir);
}

List<String> lines = new ArrayList<>();
for (Task task : tasks.asList()) {
lines.add(task.toSaveFormat());
}
List<String> lines = tasks.asList().stream()
.map(Task::toSaveFormat)
.toList();
Files.write(dataFile, lines);
} catch (IOException e) {
throw new TedException("Unable to save tasks to " + dataFile
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/ted/command/CommandType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ted.command;

import java.util.Arrays;
import java.util.stream.Collectors;

import ted.TedException;

/**
Expand Down Expand Up @@ -64,14 +67,11 @@ public String getKeyword() {
* @throws TedException if no command uses that keyword.
*/
public static CommandType fromKeyword(String keyword) throws TedException {
for (CommandType command : values()) {
if (command.keyword.equals(keyword)) {
return command;
}
}

throw new TedException("I don't recognise \"" + keyword + "\". "
+ "I understand: " + listKeywords() + ".");
return Arrays.stream(values())
.filter(command -> command.keyword.equals(keyword))
.findFirst()
.orElseThrow(() -> new TedException("I don't recognise \"" + keyword + "\". "
+ "I understand: " + listKeywords() + "."));
}

/**
Expand All @@ -82,10 +82,8 @@ public static CommandType fromKeyword(String keyword) throws TedException {
* @return the keywords, separated by commas.
*/
private static String listKeywords() {
String[] keywords = new String[values().length];
for (int i = 0; i < values().length; i++) {
keywords[i] = values()[i].keyword;
}
return String.join(", ", keywords);
return Arrays.stream(values())
.map(CommandType::getKeyword)
.collect(Collectors.joining(", "));
}
}
10 changes: 3 additions & 7 deletions src/main/java/ted/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ public boolean isEmpty() {
*/
public TaskList find(String keyword) {
String lowerCaseKeyword = keyword.toLowerCase();
TaskList matches = new TaskList();
for (Task task : tasks) {
if (task.getDescription().toLowerCase().contains(lowerCaseKeyword)) {
matches.add(task);
}
}
return matches;
return new TaskList(tasks.stream()
.filter(task -> task.getDescription().toLowerCase().contains(lowerCaseKeyword))
.toList());
}

/**
Expand Down