Skip to content

Commit

Permalink
[#383] AddressBook: remove master tag list (#385)
Browse files Browse the repository at this point in the history
We keep track of a master tag list which holds all the tags in
AddressBook.

As the master tag list was deemed unnecessary and may be too
complex for new developers to grasp, as discussed in
se-edu/addressbook-level4#753 and se-edu/addressbook-level4#794,
the master tag list has already been removed in AB4
by se-edu/addressbook-level4#825.

UniqueTagList was also deemed unnecessary and removed in
addressbook-level4 after the master tag list is removed.

As such, the master tag list and UniqueTagList class should also be
removed in the lower level projects.

Let's remove the master tag list in AddressBook and AdaptedAddressBook,
as well as the UniqueTagList class.

  [1/3] AddressBook: remove master tag list
  [2/3] Remove UniqueTagList class
  [3/3] docs: update component on UniqueTagList
  • Loading branch information
pyokagan authored Aug 8, 2018
2 parents 49cdf25 + 756dccc commit ba89e36
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 348 deletions.
Binary file modified docs/Diagrams.pptx
Binary file not shown.
7 changes: 3 additions & 4 deletions docs/LearningOutcomes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ e.g. `Main.VERSION`, `Name.EXAMPLE`, `Utils.isAnyNull(...)`

* Convert the `Parser::parseCommand(...)` method (i.e. the `parseCommand()` method of the `Parser` class) to a
class-level method. Note how this method can be either class-level or instance-level.
* Note how the `setTags` method of the `Person` class cannot be converted to a class-level method.
* Note how some instance-level methods, such as the `setTags` method of the `Person` class, cannot be converted to a class-level method.
* Add an instance-level member `int sequenceNumber` and a class-level variable `int nextSequenceNumber`
to the `Person` class. Using these two variables, ensure that each `Person` object has a unique sequence number
that indicates the order in which `Person` objects were created. e.g.
Expand All @@ -168,8 +168,8 @@ Note the following examples of _composition_ (filled diamond):
[cols="<,<",options="header",]
|=================================================
|Whole |Parts
|`AddressBook` |`UniquePersonList` `UniqueTagList`
|`Person` |`Name` `Phone` `Email` `Address`
|`AddressBook` |`UniquePersonList`
|`Person` |`Name` `Phone` `Email` `Address` `Tag`
|=================================================

Contrast with these examples of _aggregration_ (empty diamond):
Expand All @@ -178,7 +178,6 @@ Contrast with these examples of _aggregration_ (empty diamond):
|============================
|Container |Contained
|`UniquePersonList` |`Person`
|`UuniqueTagList` |`Tag`
|============================

=== References
Expand Down
Binary file modified docs/images/TaggingClass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/TaggingsInTagging.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/mainClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

/**
* Adds a person to the address book.
Expand Down Expand Up @@ -51,7 +50,7 @@ public AddCommand(String name,
new Phone(phone, isPhonePrivate),
new Email(email, isEmailPrivate),
new Address(address, isAddressPrivate),
new UniqueTagList(tagSet)
tagSet
);
}

Expand Down
58 changes: 2 additions & 56 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
@@ -1,86 +1,41 @@
package seedu.addressbook.data;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

/**
* Represents the entire address book. Contains the data of the address book.
*
* Guarantees:
* - Every tag found in every person will also be found in the tag list.
* - The tags in each person point to tag objects in the master list. (== equality)
*/
public class AddressBook {

private final UniquePersonList allPersons;
private final UniqueTagList allTags; // can contain tags not attached to any person

/**
* Creates an empty address book.
*/
public AddressBook() {
allPersons = new UniquePersonList();
allTags = new UniqueTagList();
}

/**
* Constructs an address book with the given data.
* Also updates the tag list with any missing tags found in any person.
*
* @param persons external changes to this will not affect this address book
* @param tags external changes to this will not affect this address book
*/
public AddressBook(UniquePersonList persons, UniqueTagList tags) {
public AddressBook(UniquePersonList persons) {
this.allPersons = new UniquePersonList(persons);
this.allTags = new UniqueTagList(tags);
for (Person p : allPersons) {
syncTagsWithMasterList(p);
}
}

/**
* Ensures that every tag in this person:
* - exists in the master list {@link #allTags}
* - points to a Tag object in the master list
*/
private void syncTagsWithMasterList(Person person) {
final UniqueTagList personTags = person.getTags();
allTags.mergeFrom(personTags);

// Create map with values = tag object references in the master list
final Map<Tag, Tag> masterTagObjects = new HashMap<>();
for (Tag tag : allTags) {
masterTagObjects.put(tag, tag);
}

// Rebuild the list of person tags using references from the master list
final Set<Tag> commonTagReferences = new HashSet<>();
for (Tag tag : personTags) {
commonTagReferences.add(masterTagObjects.get(tag));
}
person.setTags(new UniqueTagList(commonTagReferences));
}

/**
* Adds a person to the address book.
* Also checks the new person's tags and updates {@link #allTags} with any new tags found,
* and updates the Tag objects in the person to point to those in {@link #allTags}.
*
* @throws DuplicatePersonException if an equivalent person already exists.
*/
public void addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
syncTagsWithMasterList(toAdd);
}

/**
Expand All @@ -104,7 +59,6 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException
*/
public void clear() {
allPersons.clear();
allTags.clear();
}

/**
Expand All @@ -114,18 +68,10 @@ public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

/**
* Returns a new UniqueTagList of all tags in the address book at the time of the call.
*/
public UniqueTagList getAllTags() {
return new UniqueTagList(allTags);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddressBook // instanceof handles nulls
&& this.allPersons.equals(((AddressBook) other).allPersons)
&& this.allTags.equals(((AddressBook) other).allTags));
&& this.allPersons.equals(((AddressBook) other).allPersons));
}
}
24 changes: 14 additions & 10 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package seedu.addressbook.data.person;

import seedu.addressbook.data.tag.UniqueTagList;

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

import seedu.addressbook.data.tag.Tag;

/**
* Represents a Person in the address book.
Expand All @@ -15,16 +17,17 @@ public class Person implements ReadOnlyPerson {
private Email email;
private Address address;

private final UniqueTagList tags;
private final Set<Tag> tags = new HashSet<>();

/**
* Assumption: Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, UniqueTagList tags) {
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags = new UniqueTagList(tags); // protect internal tags from changes in the arg list
this.tags.addAll(tags);
}

/**
Expand Down Expand Up @@ -55,15 +58,16 @@ public Address getAddress() {
}

@Override
public UniqueTagList getTags() {
return new UniqueTagList(tags);
public Set<Tag> getTags() {
return new HashSet<>(tags);
}

/**
* Replaces this person's tags with the tags in the argument tag list.
* Replaces this person's tags with the tags in the argument tag set.
*/
public void setTags(UniqueTagList replacement) {
tags.setTags(replacement);
public void setTags(Set<Tag> replacement) {
tags.clear();
tags.addAll(replacement);
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions src/seedu/addressbook/data/person/ReadOnlyPerson.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package seedu.addressbook.data.person;

import java.util.Set;

import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

/**
* A read-only immutable interface for a Person in the addressbook.
Expand All @@ -15,10 +16,10 @@ public interface ReadOnlyPerson {
Address getAddress();

/**
* Returns a new TagList that is a deep copy of the internal TagList,
* changes on the returned list will not affect the person's internal tags.
* Returns a new TagSet that is a deep copy of the internal TagSet,
* changes on the returned set will not affect the person's internal tags.
*/
UniqueTagList getTags();
Set<Tag> getTags();

/**
* Returns true if both persons have the same identity fields (name and telephone).
Expand Down
141 changes: 0 additions & 141 deletions src/seedu/addressbook/data/tag/UniqueTagList.java

This file was deleted.

Loading

0 comments on commit ba89e36

Please sign in to comment.