Skip to content

Commit

Permalink
Merge pull request #520 from Eugenetanwl3881/w13/code-quality
Browse files Browse the repository at this point in the history
Improve Code Quality of Tag Commands
  • Loading branch information
RichDom2185 authored Nov 6, 2022
2 parents facee20 + 4d9d6e4 commit 13851c6
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ private TagCommandUtil() {}
*/
public static Item validateAndGetItem(Model model, Tag tag, Index index) throws CommandException {
requireNonNull(model);
requireNonNull(tag);
requireNonNull(index);

if (!model.hasTag(tag)) {
throw new CommandException("This tag does not exist");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public RenameTagCommand parse(String args) throws ParseException {
}

private boolean hasExtraPrefixesInName(String name) {
assert name != null;
Matcher matcher = CliSyntax.PREFIX_REGEX.matcher(name);
return matcher.find();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static java.util.Objects.requireNonNull;

import java.util.stream.Stream;

import seedu.foodrem.commons.core.Messages;
import seedu.foodrem.commons.core.index.Index;
import seedu.foodrem.commons.util.StringUtil;
Expand All @@ -13,7 +11,6 @@
import seedu.foodrem.logic.parser.CliSyntax;
import seedu.foodrem.logic.parser.Parser;
import seedu.foodrem.logic.parser.ParserUtil;
import seedu.foodrem.logic.parser.Prefix;
import seedu.foodrem.logic.parser.exceptions.ParseException;

/**
Expand All @@ -32,7 +29,7 @@ public TagCommand parse(String args) throws ParseException {
Index index = StringUtil.validateAndGetIndexFromString(argMultimap.getPreamble().trim(),
TagCommand.getUsage());

if (!arePrefixesPresent(argMultimap, CliSyntax.PREFIX_NAME)
if (!ParserUtil.arePrefixesPresent(argMultimap, CliSyntax.PREFIX_NAME)
|| argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, TagCommand.getUsage()));
}
Expand All @@ -42,11 +39,4 @@ public TagCommand parse(String args) throws ParseException {
return new TagCommand(name, index);
}

/**
* Returns {@code true} if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.foodrem.logic.parser.tagcommandparser;

import static java.util.Objects.requireNonNull;
import static seedu.foodrem.logic.parser.ParserUtil.arePrefixesPresent;

import seedu.foodrem.commons.core.Messages;
import seedu.foodrem.commons.core.index.Index;
Expand Down Expand Up @@ -29,7 +28,7 @@ public UntagCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, CliSyntax.PREFIX_NAME);
Index index = StringUtil.validateAndGetIndexFromString(argMultimap.getPreamble().trim(),
UntagCommand.getUsage());
if (!arePrefixesPresent(argMultimap, CliSyntax.PREFIX_NAME)
if (!ParserUtil.arePrefixesPresent(argMultimap, CliSyntax.PREFIX_NAME)
|| argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, UntagCommand.getUsage()));
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/seedu/foodrem/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ public Tag(String tagName) {
}

public String getName() {
assert tagName != null;
return this.tagName.toString();
}

/**
* @inheritDoc
*/
@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof Tag
&& tagName.equals(((Tag) other).tagName));
}

/**
* @inheritDoc
*/
@Override
public int hashCode() {
return tagName.hashCode();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/foodrem/model/tag/TagName.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,27 @@ public TagName(String name) {
fullName = name;
}

/**
* @inheritDoc
*/
@Override
public String toString() {
return fullName;
}

/**
* @inheritDoc
*/
@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof TagName
&& fullName.equals(((TagName) other).fullName));
}

/**
* @inheritDoc
*/
@Override
public int hashCode() {
return fullName.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ public TagNameContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

/**
* Tests whether the keyword is contained in the {@code tag}
*
* @param tag to be tested
* @return boolean stating whether keyword in tag name
*/
@Override
public boolean test(Tag tag) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(tag.getName(), keyword));
}

/**
* @inheritDoc
*/
@Override
public boolean equals(Object other) {
return other == this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* name).
*/
public class DuplicateTagException extends RuntimeException {
/**
* Creates a new DuplicateTagException
*/
public DuplicateTagException() {
super("Operation would result in duplicate tags");
}
Expand Down

0 comments on commit 13851c6

Please sign in to comment.