Skip to content

Commit

Permalink
Refactor Messages and StringUtil in commons
Browse files Browse the repository at this point in the history
  • Loading branch information
benedictchuajj committed Oct 14, 2021
1 parent f67488c commit 70da259
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
4 changes: 1 addition & 3 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_TOO_MANY_FIELDS = "Too many fields inputted into the command! \n%1$s";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_PERSON_CLIENTID = "The person client id provided is invalid";
public static final String MESSAGE_NONEXISTENT_CLIENT_ID = "There's no contact with client ID %s";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_VIEW_SUCCESS = "Viewing person: %1$s";
public static final String MESSAGE_VIEW_INVALID_CLIENT_ID = "There's no contact with client ID %s";
public static final String MESSAGE_SORT_SUCCESS = "List sorted by %s";

}
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/commons/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ public static boolean containsWordIgnoreCase(String sentence, String word) {
}

/**
* Returns true if the {@code sentence} contains the {@code word}.
* Returns true if the {@code sentence} contains the {@code string}.
* Ignores case, a full word match is not required.
* <br>examples:<pre>
* containsWordIgnoreCase("ABc def", "abc") == true
* containsWordIgnoreCase("ABc def", "DEF") == true
* containsWordIgnoreCase("A Bc def", "A B") == true
* </pre>
* @param sentence cannot be null
* @param word cannot be null, cannot be empty
* @param string cannot be null, cannot be empty
*/
public static boolean containsIgnoreCase(String sentence, String word) {
public static boolean containsStringIgnoreCase(String sentence, String string) {
requireNonNull(sentence);
requireNonNull(word);
requireNonNull(string);

String preppedWord = word;
String preppedWord = string;
checkArgument(!preppedWord.isEmpty(), "Word parameter cannot be empty");

return sentence.toLowerCase().contains(word.toLowerCase());
return sentence.toLowerCase().contains(string.toLowerCase());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public EditCommand(ClientId clientId, EditPersonDescriptor editPersonDescriptor)
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (!model.hasClientId(clientId)) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_CLIENTID);
throw new CommandException(String.format(Messages.MESSAGE_NONEXISTENT_CLIENT_ID, clientId));
}

Person personToEdit = model.getAddressBook().getPerson(clientId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.updatePersonToView(predicate);
if (!model.isPersonExistToView()) {
return new CommandResult(String.format(Messages.MESSAGE_VIEW_INVALID_CLIENT_ID, clientId.toString()));
return new CommandResult(String.format(Messages.MESSAGE_NONEXISTENT_CLIENT_ID, clientId.toString()));
}
return new CommandResult(String.format(Messages.MESSAGE_VIEW_SUCCESS, model.getNameOfPersonToView()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package seedu.address.model.person;

import static seedu.address.commons.util.StringUtil.containsIgnoreCase;
import static seedu.address.commons.util.StringUtil.containsStringIgnoreCase;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CURRENTPLAN;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DISPOSABLEINCOME;
Expand Down Expand Up @@ -34,32 +34,32 @@ public boolean test(Person person) {
boolean checkAttribute = Stream.of(person.getName(), person.getPhone(),
person.getEmail(), person.getAddress(), person.getRiskAppetite(),
person.getDisposableIncome(), person.getLastMet(), person.getCurrentPlan())
.map(Object::toString).anyMatch(y -> containsIgnoreCase(y, x));
.map(Object::toString).anyMatch(y -> containsStringIgnoreCase(y, x));

boolean checkAttributeTag = person.getTags().stream()
.anyMatch(y -> containsIgnoreCase(y.tagName, x));
.anyMatch(y -> containsStringIgnoreCase(y.tagName, x));
return checkAttribute || checkAttributeTag;
}
);

boolean checkName = keywords.getValue(PREFIX_NAME)
.map(x -> containsIgnoreCase(person.getName().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getName().toString(), x)).orElse(true);
boolean checkPhone = keywords.getValue(PREFIX_PHONE)
.map(x -> containsIgnoreCase(person.getPhone().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getPhone().toString(), x)).orElse(true);
boolean checkEmail = keywords.getValue(PREFIX_EMAIL)
.map(x -> containsIgnoreCase(person.getEmail().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getEmail().toString(), x)).orElse(true);
boolean checkAddress = keywords.getValue(PREFIX_ADDRESS)
.map(x -> containsIgnoreCase(person.getAddress().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getAddress().toString(), x)).orElse(true);
boolean checkRiskAppetite = keywords.getValue(PREFIX_RISKAPPETITE)
.map(x -> containsIgnoreCase(person.getRiskAppetite().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getRiskAppetite().toString(), x)).orElse(true);
boolean checkDisposableIncome = keywords.getValue(PREFIX_DISPOSABLEINCOME)
.map(x -> containsIgnoreCase(person.getDisposableIncome().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getDisposableIncome().toString(), x)).orElse(true);
boolean checkLastMet = keywords.getValue(PREFIX_LASTMET)
.map(x -> containsIgnoreCase(person.getLastMet().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getLastMet().toString(), x)).orElse(true);
boolean checkCurrentPlan = keywords.getValue(PREFIX_CURRENTPLAN)
.map(x -> containsIgnoreCase(person.getCurrentPlan().toString(), x)).orElse(true);
.map(x -> containsStringIgnoreCase(person.getCurrentPlan().toString(), x)).orElse(true);
boolean checkTags = keywords.getValue(PREFIX_TAG)
.map(x -> person.getTags().stream().anyMatch(y -> containsIgnoreCase(y.tagName, x))).orElse(true);
.map(x -> person.getTags().stream().anyMatch(y -> containsStringIgnoreCase(y.tagName, x))).orElse(true);

return checkGeneral && checkName && checkPhone && checkEmail && checkAddress && checkRiskAppetite
&& checkDisposableIncome && checkLastMet && checkCurrentPlan && checkTags;
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/seedu/address/commons/util/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,37 +167,37 @@ public void containsWordIgnoreCase_validInputs_correctResult() {

@Test
public void containIgnoreCase_nullSentence_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> StringUtil.containsIgnoreCase(null, "a"));
assertThrows(NullPointerException.class, () -> StringUtil.containsStringIgnoreCase(null, "a"));
}

@Test
public void containIgnoreCase_nullWord_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> StringUtil.containsIgnoreCase("a", null));
assertThrows(NullPointerException.class, () -> StringUtil.containsStringIgnoreCase("a", null));
}

@Test
public void containIgnoreCase_emptyWord_throwsIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> StringUtil.containsIgnoreCase("a", ""));
assertThrows(IllegalArgumentException.class, () -> StringUtil.containsStringIgnoreCase("a", ""));
}

@Test
public void containsIgnoreCase_validInputs_correctResult() {
assertFalse(StringUtil.containsIgnoreCase("", "aaa"));
assertFalse(StringUtil.containsIgnoreCase("abd", "g"));
assertFalse(StringUtil.containsIgnoreCase("abd", " a"));
assertFalse(StringUtil.containsStringIgnoreCase("", "aaa"));
assertFalse(StringUtil.containsStringIgnoreCase("abd", "g"));
assertFalse(StringUtil.containsStringIgnoreCase("abd", " a"));

assertTrue(StringUtil.containsIgnoreCase("abc", "a"));
assertTrue(StringUtil.containsIgnoreCase("ab c", "c"));
assertTrue(StringUtil.containsStringIgnoreCase("abc", "a"));
assertTrue(StringUtil.containsStringIgnoreCase("ab c", "c"));

//match uppercase and lowercase
assertTrue(StringUtil.containsIgnoreCase("abC", "C"));
assertTrue(StringUtil.containsIgnoreCase("abfCa", "fCa"));
assertTrue(StringUtil.containsStringIgnoreCase("abC", "C"));
assertTrue(StringUtil.containsStringIgnoreCase("abfCa", "fCa"));

// match for word with spaces in between
assertTrue(StringUtil.containsIgnoreCase("b a a d", "a a"));
assertTrue(StringUtil.containsStringIgnoreCase("b a a d", "a a"));

// match multiple
assertTrue(StringUtil.containsIgnoreCase("acadffac", "ac"));
assertTrue(StringUtil.containsStringIgnoreCase("acadffac", "ac"));
}

//---------------- Tests for getDetails --------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public void execute_invalidPerson_failure() {
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build();
EditCommand editCommand = new EditCommand(outOfBound, descriptor);

assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_CLIENTID);
assertCommandFailure(editCommand, model,
String.format(Messages.MESSAGE_NONEXISTENT_CLIENT_ID, CLIENTID_OUTOFBOUND)
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.commons.core.Messages.MESSAGE_VIEW_INVALID_CLIENT_ID;
import static seedu.address.commons.core.Messages.MESSAGE_NONEXISTENT_CLIENT_ID;
import static seedu.address.commons.core.Messages.MESSAGE_VIEW_SUCCESS;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.ALICE;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void execute_validClientId_success() {
@Test
public void execute_invalidClientId_failure() {
String userInput = "10";
String expectedMessage = String.format(MESSAGE_VIEW_INVALID_CLIENT_ID, userInput);
String expectedMessage = String.format(MESSAGE_NONEXISTENT_CLIENT_ID, userInput);
PersonHasId predicate = preparePredicate(userInput);
ViewCommand command = new ViewCommand(new ClientId(userInput), predicate);
expectedModel.updatePersonToView(predicate);
Expand Down

0 comments on commit 70da259

Please sign in to comment.