Skip to content

Commit

Permalink
Sarthak | Replaces loop with stream in CommandType
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Jan 10, 2025
1 parent cf62897 commit afcb279
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/codurance/training/commands/CommandType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.codurance.training.commands;

import java.util.Arrays;

enum CommandType {
ADD("add"), SHOW("show"), CHECK("check"), UNCHECK("uncheck");

Expand All @@ -10,11 +12,10 @@ enum CommandType {
}

static CommandType from(String commandName) {
for (CommandType type : CommandType.values()) {
if (type.commandName.equalsIgnoreCase(commandName)) {
return type;
}
}
throw new IllegalArgumentException("Unknown command: " + commandName);
return Arrays
.stream(CommandType.values())
.filter(commandType -> commandType.commandName.equalsIgnoreCase(commandName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown command type: " + commandName));
}
}

0 comments on commit afcb279

Please sign in to comment.