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 models component: abstract out Set<Tag> into Tags.java #8

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public static String format(Person person) {
.append(person.getEmail())
.append("; Address: ")
.append(person.getAddress())
.append("; Tags: ");
person.getTags().forEach(builder::append);
.append("; Tags: ")
.append(person.getTags());
return builder.toString();
}

Expand Down
34 changes: 11 additions & 23 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.person.fields.mandatory.Address;
import seedu.address.model.person.fields.mandatory.Email;
import seedu.address.model.person.fields.mandatory.Name;
import seedu.address.model.person.fields.mandatory.Phone;
import seedu.address.model.person.fields.optional.Tags;

/**
* Edits the details of an existing person in the address book.
Expand Down Expand Up @@ -99,7 +96,7 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Tags updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
}
Expand Down Expand Up @@ -137,7 +134,7 @@ public static class EditPersonDescriptor {
private Phone phone;
private Email email;
private Address address;
private Set<Tag> tags;
private Tags tags;

public EditPersonDescriptor() {}

Expand Down Expand Up @@ -192,21 +189,12 @@ public Optional<Address> getAddress() {
return Optional.ofNullable(address);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
public void setTags(Tags tags) {
this.tags = tags;
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
public Optional<Tags> getTags() {
return Optional.ofNullable(tags);
}

@Override
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

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

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.person.fields.mandatory.Address;
import seedu.address.model.person.fields.mandatory.Email;
import seedu.address.model.person.fields.mandatory.Name;
import seedu.address.model.person.fields.mandatory.Phone;
import seedu.address.model.person.fields.optional.Tags;

/**
* Parses input arguments and creates a new AddCommand object
Expand All @@ -43,9 +42,9 @@ public AddCommand parse(String args) throws ParseException {
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));
Tags tags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

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

return new AddCommand(person);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;
import seedu.address.model.person.fields.optional.Tags;

/**
* Parses input arguments and creates a new EditCommand object
Expand Down Expand Up @@ -72,7 +71,7 @@ public EditCommand parse(String args) throws ParseException {
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
*/
private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws ParseException {
private Optional<Tags> parseTagsForEdit(Collection<String> tags) throws ParseException {
assert tags != null;

if (tags.isEmpty()) {
Expand Down
37 changes: 11 additions & 26 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import static java.util.Objects.requireNonNull;

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

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.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.fields.mandatory.Address;
import seedu.address.model.person.fields.mandatory.Email;
import seedu.address.model.person.fields.mandatory.Name;
import seedu.address.model.person.fields.mandatory.Phone;
import seedu.address.model.person.fields.optional.Tags;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -96,29 +95,15 @@ public static Email parseEmail(String email) throws ParseException {
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code tag} is invalid.
* Parses {@code Collection<String> tags} into a {@code Tags}.
*/
public static Tag parseTag(String tag) throws ParseException {
requireNonNull(tag);
String trimmedTag = tag.trim();
if (!Tag.isValidTagName(trimmedTag)) {
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(Tag.MESSAGE_CONSTRAINTS);
}
return new Tag(trimmedTag);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>}.
*/
public static Set<Tag> parseTags(Collection<String> tags) throws ParseException {
requireNonNull(tags);
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
tagSet.add(parseTag(tagName));
}
return tagSet;
}
}
33 changes: 13 additions & 20 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.tag.Tag;
import seedu.address.model.person.fields.mandatory.Address;
import seedu.address.model.person.fields.mandatory.Email;
import seedu.address.model.person.fields.mandatory.Name;
import seedu.address.model.person.fields.mandatory.Phone;
import seedu.address.model.person.fields.optional.Tags;

/**
* Represents a Person in the address book.
Expand All @@ -27,33 +27,30 @@ public class Person {

// Data fields
private final Address address;
private final Set<Tag> tags = new HashSet<>();
private final Tags tags;

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
public Person(Name name, Phone phone, Email email, Address address, Tags tags) {
requireAllNonNull(name, phone, email, address, tags);
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.tags = tags;
}

@JsonCreator
private Person(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<String> tagNames) {
@JsonProperty("tags") String[] tagNames) {
requireAllNonNull(name, phone, email, address, tagNames);
this.name = new Name(name);
this.phone = new Phone(phone);
this.email = new Email(email);
this.address = new Address(address);
if (tagNames != null) {
for (String tagName : tagNames) {
this.tags.add(new Tag(tagName));
}
}
this.tags = new Tags(tagNames);
}

public Name getName() {
Expand All @@ -72,12 +69,8 @@ public Address getAddress() {
return address;
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
public Tags getTags() {
return tags;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/model/person/fields/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package seedu.address.model.person.fields;

/**
* Represents an interface for Person fields.
*/
public interface Field {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.fields.mandatory;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
Expand All @@ -9,7 +9,7 @@
* Represents a Person's address in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)}
*/
public class Address {
public class Address implements MandatoryField {

public static final String MESSAGE_CONSTRAINTS = "Addresses can take any values, and it should not be blank";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.fields.mandatory;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
Expand All @@ -9,7 +9,7 @@
* Represents a Person's email in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
*/
public class Email {
public class Email implements MandatoryField {

private static final String SPECIAL_CHARACTERS = "+_.-";
public static final String MESSAGE_CONSTRAINTS = "Emails should be of the format local-part@domain "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package seedu.address.model.person.fields.mandatory;

import seedu.address.model.person.fields.Field;

/**
* Represents an interface for mandatory Person fields.
*/
public interface MandatoryField extends Field {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.fields.mandatory;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
Expand All @@ -9,7 +9,7 @@
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
*/
public class Name {
public class Name implements MandatoryField {

public static final String MESSAGE_CONSTRAINTS =
"Names should only contain alphanumeric characters and spaces, and it should not be blank";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.fields.mandatory;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
Expand All @@ -9,7 +9,7 @@
* Represents a Person's phone number in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)}
*/
public class Phone {
public class Phone implements MandatoryField {

public static final String MESSAGE_CONSTRAINTS =
"Phone numbers should only contain numbers, and it should be at least 3 digits long";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package seedu.address.model.person.fields.optional;

/**
* Represents an interface for optional Person fields
*/
public interface OptionalField {
}
Loading