Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
ParserUtil: remove Optional variants of parse methods
Browse files Browse the repository at this point in the history
As the comment[1] at the top of ParserUtil says, methods that take
`Optional` as a parameter are bad.

So, let's remove them.

(It could be argued that leaving these methods in the code base could
serve as an example for students of what not to do, but really, (1) the
number of things that are forbidden under "best practice" is quite
numerous, and trying to add an example of every single one of them in
our code base would just lead to an annoyingly smelly code base, and (2)
students will need to spend time cleaning up all of these negative
examples, which is an unnecessary burden. These things would be better
documented in style guides.)

[1] Comment added in 84d6516 ([#557] ParserUtil: Update header comments
    on Optional usage (#558), 2017-07-18)
  • Loading branch information
pyokagan committed Aug 8, 2018
1 parent e186102 commit 6ae9591
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public AddCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME)).get();
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE)).get();
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL)).get();
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS)).get();
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Person person = new Person(name, phone, email, address, tagList);
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ public EditCommand parse(String args) throws ParseException {
}

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();
ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME)).ifPresent(editPersonDescriptor::setName);
ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE)).ifPresent(editPersonDescriptor::setPhone);
ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL)).ifPresent(editPersonDescriptor::setEmail);
ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS)).ifPresent(editPersonDescriptor::setAddress);
if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
}
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()));
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()));
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);

if (!editPersonDescriptor.isAnyFieldEdited()) {
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
Expand All @@ -18,12 +17,6 @@

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
* {@code ParserUtil} contains methods that take in {@code Optional} as parameters. However, it goes against Java's
* convention (see https://stackoverflow.com/a/39005452) as {@code Optional} should only be used a return type.
* Justification: The methods in concern receive {@code Optional} return values from other methods as parameters and
* return {@code Optional} values based on whether the parameters were present. Therefore, it is redundant to unwrap the
* initial {@code Optional} before passing to {@code ParserUtil} as a parameter and then re-wrap it into an
* {@code Optional} return value inside {@code ParserUtil} methods.
*/
public class ParserUtil {

Expand Down Expand Up @@ -57,15 +50,6 @@ public static Name parseName(String name) throws ParseException {
return new Name(trimmedName);
}

/**
* Parses a {@code Optional<String> name} into an {@code Optional<Name>} if {@code name} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Name> parseName(Optional<String> name) throws ParseException {
requireNonNull(name);
return name.isPresent() ? Optional.of(parseName(name.get())) : Optional.empty();
}

/**
* Parses a {@code String phone} into a {@code Phone}.
* Leading and trailing whitespaces will be trimmed.
Expand All @@ -81,15 +65,6 @@ public static Phone parsePhone(String phone) throws ParseException {
return new Phone(trimmedPhone);
}

/**
* Parses a {@code Optional<String> phone} into an {@code Optional<Phone>} if {@code phone} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Phone> parsePhone(Optional<String> phone) throws ParseException {
requireNonNull(phone);
return phone.isPresent() ? Optional.of(parsePhone(phone.get())) : Optional.empty();
}

/**
* Parses a {@code String address} into an {@code Address}.
* Leading and trailing whitespaces will be trimmed.
Expand All @@ -105,15 +80,6 @@ public static Address parseAddress(String address) throws ParseException {
return new Address(trimmedAddress);
}

/**
* Parses a {@code Optional<String> address} into an {@code Optional<Address>} if {@code address} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Address> parseAddress(Optional<String> address) throws ParseException {
requireNonNull(address);
return address.isPresent() ? Optional.of(parseAddress(address.get())) : Optional.empty();
}

/**
* Parses a {@code String email} into an {@code Email}.
* Leading and trailing whitespaces will be trimmed.
Expand All @@ -129,15 +95,6 @@ public static Email parseEmail(String email) throws ParseException {
return new Email(trimmedEmail);
}

/**
* Parses a {@code Optional<String> email} into an {@code Optional<Email>} if {@code email} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Email> parseEmail(Optional<String> email) throws ParseException {
requireNonNull(email);
return email.isPresent() ? Optional.of(parseEmail(email.get())) : Optional.empty();
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
38 changes: 0 additions & 38 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.address.logic.parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import static seedu.address.logic.parser.ParserUtil.MESSAGE_INVALID_INDEX;
Expand All @@ -10,7 +9,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import org.junit.Rule;
Expand Down Expand Up @@ -69,129 +67,93 @@ public void parseIndex_validInput_success() throws Exception {
@Test
public void parseName_null_throwsNullPointerException() {
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseName((String) null));
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseName((Optional<String>) null));
}

@Test
public void parseName_invalidValue_throwsParseException() {
Assert.assertThrows(ParseException.class, () -> ParserUtil.parseName(INVALID_NAME));
Assert.assertThrows(ParseException.class, () -> ParserUtil.parseName(Optional.of(INVALID_NAME)));
}

@Test
public void parseName_optionalEmpty_returnsOptionalEmpty() throws Exception {
assertFalse(ParserUtil.parseName(Optional.empty()).isPresent());
}

@Test
public void parseName_validValueWithoutWhitespace_returnsName() throws Exception {
Name expectedName = new Name(VALID_NAME);
assertEquals(expectedName, ParserUtil.parseName(VALID_NAME));
assertEquals(Optional.of(expectedName), ParserUtil.parseName(Optional.of(VALID_NAME)));
}

@Test
public void parseName_validValueWithWhitespace_returnsTrimmedName() throws Exception {
String nameWithWhitespace = WHITESPACE + VALID_NAME + WHITESPACE;
Name expectedName = new Name(VALID_NAME);
assertEquals(expectedName, ParserUtil.parseName(nameWithWhitespace));
assertEquals(Optional.of(expectedName), ParserUtil.parseName(Optional.of(nameWithWhitespace)));
}

@Test
public void parsePhone_null_throwsNullPointerException() {
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parsePhone((String) null));
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parsePhone((Optional<String>) null));
}

@Test
public void parsePhone_invalidValue_throwsParseException() {
Assert.assertThrows(ParseException.class, () -> ParserUtil.parsePhone(INVALID_PHONE));
Assert.assertThrows(ParseException.class, () -> ParserUtil.parsePhone(Optional.of(INVALID_PHONE)));
}

@Test
public void parsePhone_optionalEmpty_returnsOptionalEmpty() throws Exception {
assertFalse(ParserUtil.parsePhone(Optional.empty()).isPresent());
}

@Test
public void parsePhone_validValueWithoutWhitespace_returnsPhone() throws Exception {
Phone expectedPhone = new Phone(VALID_PHONE);
assertEquals(expectedPhone, ParserUtil.parsePhone(VALID_PHONE));
assertEquals(Optional.of(expectedPhone), ParserUtil.parsePhone(Optional.of(VALID_PHONE)));
}

@Test
public void parsePhone_validValueWithWhitespace_returnsTrimmedPhone() throws Exception {
String phoneWithWhitespace = WHITESPACE + VALID_PHONE + WHITESPACE;
Phone expectedPhone = new Phone(VALID_PHONE);
assertEquals(expectedPhone, ParserUtil.parsePhone(phoneWithWhitespace));
assertEquals(Optional.of(expectedPhone), ParserUtil.parsePhone(Optional.of(phoneWithWhitespace)));
}

@Test
public void parseAddress_null_throwsNullPointerException() {
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseAddress((String) null));
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseAddress((Optional<String>) null));
}

@Test
public void parseAddress_invalidValue_throwsParseException() {
Assert.assertThrows(ParseException.class, () -> ParserUtil.parseAddress(INVALID_ADDRESS));
Assert.assertThrows(ParseException.class, () -> ParserUtil.parseAddress(Optional.of(INVALID_ADDRESS)));
}

@Test
public void parseAddress_optionalEmpty_returnsOptionalEmpty() throws Exception {
assertFalse(ParserUtil.parseAddress(Optional.empty()).isPresent());
}

@Test
public void parseAddress_validValueWithoutWhitespace_returnsAddress() throws Exception {
Address expectedAddress = new Address(VALID_ADDRESS);
assertEquals(expectedAddress, ParserUtil.parseAddress(VALID_ADDRESS));
assertEquals(Optional.of(expectedAddress), ParserUtil.parseAddress(Optional.of(VALID_ADDRESS)));
}

@Test
public void parseAddress_validValueWithWhitespace_returnsTrimmedAddress() throws Exception {
String addressWithWhitespace = WHITESPACE + VALID_ADDRESS + WHITESPACE;
Address expectedAddress = new Address(VALID_ADDRESS);
assertEquals(expectedAddress, ParserUtil.parseAddress(addressWithWhitespace));
assertEquals(Optional.of(expectedAddress), ParserUtil.parseAddress(Optional.of(addressWithWhitespace)));
}

@Test
public void parseEmail_null_throwsNullPointerException() {
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseEmail((String) null));
Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseEmail((Optional<String>) null));
}

@Test
public void parseEmail_invalidValue_throwsParseException() {
Assert.assertThrows(ParseException.class, () -> ParserUtil.parseEmail(INVALID_EMAIL));
Assert.assertThrows(ParseException.class, () -> ParserUtil.parseEmail(Optional.of(INVALID_EMAIL)));
}

@Test
public void parseEmail_optionalEmpty_returnsOptionalEmpty() throws Exception {
assertFalse(ParserUtil.parseEmail(Optional.empty()).isPresent());
}

@Test
public void parseEmail_validValueWithoutWhitespace_returnsEmail() throws Exception {
Email expectedEmail = new Email(VALID_EMAIL);
assertEquals(expectedEmail, ParserUtil.parseEmail(VALID_EMAIL));
assertEquals(Optional.of(expectedEmail), ParserUtil.parseEmail(Optional.of(VALID_EMAIL)));
}

@Test
public void parseEmail_validValueWithWhitespace_returnsTrimmedEmail() throws Exception {
String emailWithWhitespace = WHITESPACE + VALID_EMAIL + WHITESPACE;
Email expectedEmail = new Email(VALID_EMAIL);
assertEquals(expectedEmail, ParserUtil.parseEmail(emailWithWhitespace));
assertEquals(Optional.of(expectedEmail), ParserUtil.parseEmail(Optional.of(emailWithWhitespace)));
}

@Test
Expand Down

0 comments on commit 6ae9591

Please sign in to comment.