Skip to content

Commit

Permalink
Abstract out Set<Tag> into new Tags class
Browse files Browse the repository at this point in the history
  • Loading branch information
aureliony committed Feb 27, 2024
1 parent db5664d commit 94f582c
Show file tree
Hide file tree
Showing 27 changed files with 207 additions and 176 deletions.
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
25 changes: 11 additions & 14 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 @@ -196,17 +193,17 @@ public Optional<Address> getAddress() {
* 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
25 changes: 3 additions & 22 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

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;

/**
* Parses input arguments and creates a new EditCommand object
Expand Down Expand Up @@ -58,7 +52,9 @@ public EditCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
if (argMultimap.getValue(PREFIX_TAG).isPresent()) {
editPersonDescriptor.setTags(ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)));
}

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand All @@ -67,19 +63,4 @@ public EditCommand parse(String args) throws ParseException {
return new EditCommand(index, editPersonDescriptor);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* 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 {
assert tags != null;

if (tags.isEmpty()) {
return Optional.empty();
}
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseTags(tagSet));
}

}
19 changes: 7 additions & 12 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 @@ -113,12 +112,8 @@ public static Tag parseTag(String tag) throws ParseException {
/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>}.
*/
public static Set<Tag> parseTags(Collection<String> tags) throws ParseException {
public static Tags parseTags(Collection<String> tags) throws ParseException {
requireNonNull(tags);
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
tagSet.add(parseTag(tagName));
}
return tagSet;
return new Tags(tags.toArray(new String[0]));
}
}
32 changes: 12 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,29 @@ 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) {
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 +68,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: 7 additions & 1 deletion src/main/java/seedu/address/model/person/fields/Field.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
package seedu.address.model.person.fields;public interface Field {
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
@@ -1,2 +1,10 @@
package seedu.address.model.person.fields.mandatory;public interface MandatoryField {
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
@@ -1,2 +1,7 @@
package seedu.address.model.person.fields.optional;public interface OptionalField {
package seedu.address.model.person.fields.optional;

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

0 comments on commit 94f582c

Please sign in to comment.