Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ParserUtil.java #71

Merged
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
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public AddCommand parse(String args) throws ParseException {
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);
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());
Tags tags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Name name = Name.of(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = Phone.of(argMultimap.getValue(PREFIX_PHONE).get());
Email email = Email.of(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = Address.of(argMultimap.getValue(PREFIX_ADDRESS).get());
Tags tags = Tags.of(argMultimap.getAllValues(PREFIX_TAG));

Person person = new Person(name, phone, email, address, tags);

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.fields.Address;
import seedu.address.model.person.fields.Email;
import seedu.address.model.person.fields.Name;
import seedu.address.model.person.fields.Phone;
import seedu.address.model.person.fields.Tags;

/**
Expand Down Expand Up @@ -46,16 +50,16 @@ public EditCommand parse(String args) throws ParseException {
EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
editPersonDescriptor.setName(Name.of(argMultimap.getValue(PREFIX_NAME).get()));
}
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()));
editPersonDescriptor.setPhone(Phone.of(argMultimap.getValue(PREFIX_PHONE).get()));
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()));
editPersonDescriptor.setEmail(Email.of(argMultimap.getValue(PREFIX_EMAIL).get()));
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
editPersonDescriptor.setAddress(Address.of(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);

Expand All @@ -78,7 +82,7 @@ private Optional<Tags> parseTagsForEdit(Collection<String> tags) throws ParseExc
return Optional.empty();
}
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseTags(tagSet));
return Optional.of(Tags.of(tagSet));
}

}
85 changes: 0 additions & 85 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;

import java.util.Collection;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.fields.Address;
import seedu.address.model.person.fields.Email;
import seedu.address.model.person.fields.Name;
import seedu.address.model.person.fields.Phone;
import seedu.address.model.person.fields.Tags;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
Expand All @@ -33,80 +24,4 @@ public static Index parseIndex(String oneBasedIndex) throws ParseException {
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}

/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code name} is invalid.
*/
public static Name parseName(String name) throws ParseException {
requireNonNull(name);
String trimmedName = name.trim();
try {
return new Name(trimmedName);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

/**
* Parses a {@code String phone} into a {@code Phone}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code phone} is invalid.
*/
public static Phone parsePhone(String phone) throws ParseException {
requireNonNull(phone);
String trimmedPhone = phone.trim();
try {
return new Phone(trimmedPhone);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

/**
* Parses a {@code String address} into an {@code Address}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code address} is invalid.
*/
public static Address parseAddress(String address) throws ParseException {
requireNonNull(address);
String trimmedAddress = address.trim();
try {
return new Address(trimmedAddress);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

/**
* Parses a {@code String email} into an {@code Email}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code email} is invalid.
*/
public static Email parseEmail(String email) throws ParseException {
requireNonNull(email);
String trimmedEmail = email.trim();
try {
return new Email(trimmedEmail);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

/**
* Parses {@code Collection<String> tags} into a {@code Tags}.
*/
public static Tags parseTags(Collection<String> tags) throws ParseException {
requireNonNull(tags);
try {
return new Tags(tags.toArray(new String[0]));
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/model/person/fields/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.logic.parser.exceptions.ParseException;

/**
* Represents a Person's address in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)}
Expand Down Expand Up @@ -39,6 +41,22 @@ private static boolean isValid(String test) {
return test.matches(VALIDATION_REGEX);
}

/**
* Parses a {@code String address} into an {@code Address}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code address} is invalid.
*/
public static Address of(String address) throws ParseException {
requireNonNull(address);
String trimmedAddress = address.trim();
try {
return new Address(trimmedAddress);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

@Override
@JsonValue
public String toString() {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/model/person/fields/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.logic.parser.exceptions.ParseException;

/**
* Represents a Person's email in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
Expand Down Expand Up @@ -53,6 +55,22 @@ private static boolean isValid(String test) {
return test.matches(VALIDATION_REGEX);
}

/**
* Parses a {@code String email} into an {@code Email}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code email} is invalid.
*/
public static Email of(String email) throws ParseException {
requireNonNull(email);
String trimmedEmail = email.trim();
try {
return new Email(trimmedEmail);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

@Override
@JsonValue
public String toString() {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/model/person/fields/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.logic.parser.exceptions.ParseException;

/**
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
Expand Down Expand Up @@ -40,6 +42,22 @@ private static boolean isValid(String test) {
return test.matches(VALIDATION_REGEX);
}

/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code name} is invalid.
*/
public static Name of(String name) throws ParseException {
requireNonNull(name);
String trimmedName = name.trim();
try {
return new Name(trimmedName);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

@Override
@JsonValue
public String toString() {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/model/person/fields/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.logic.parser.exceptions.ParseException;

/**
* Represents a Person's phone number in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)}
Expand Down Expand Up @@ -34,6 +36,22 @@ private static boolean isValid(String test) {
return test.matches(VALIDATION_REGEX);
}

/**
* Parses a {@code String phone} into a {@code Phone}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code phone} is invalid.
*/
public static Phone of(String phone) throws ParseException {
requireNonNull(phone);
String trimmedPhone = phone.trim();
try {
return new Phone(trimmedPhone);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

@Override
@JsonValue
public String toString() {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/person/fields/Tags.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package seedu.address.model.person.fields;

import static java.util.Objects.requireNonNull;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -34,6 +38,18 @@ public Tags(String... tagNames) {
.collect(Collectors.toUnmodifiableSet());
}

/**
* Parses {@code Collection<String> tags} into a {@code Tags}.
*/
public static Tags of(Collection<String> tags) throws ParseException {
requireNonNull(tags);
try {
return new Tags(tags.toArray(new String[0]));
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}
}

@JsonValue
private Set<Tag> get() {
return tags;
Expand Down
Loading