Skip to content
This repository has been archived by the owner on Jul 26, 2019. It is now read-only.

Commit

Permalink
AddressBook: remove master tag list (#149)
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.
  • Loading branch information
yamidark authored Aug 13, 2018
2 parents 9a15456 + 7219c54 commit ab72b1f
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 285 deletions.
Binary file modified docs/Diagrams.pptx
Binary file not shown.
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 @@ -3,7 +3,6 @@
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.*;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -45,7 +44,7 @@ public AddCommand(String name,
new Phone(phone, isPhonePrivate),
new Email(email, isEmailPrivate),
new Address(address, isAddressPrivate),
new UniqueTagList(tagSet)
tagSet
);
}

Expand Down
68 changes: 9 additions & 59 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package seedu.addressbook.data;

import seedu.addressbook.data.person.*;
import seedu.addressbook.data.person.UniquePersonList.*;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.data.tag.UniqueTagList.*;

import java.util.*;
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;

/**
* 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

public static AddressBook empty() {
return new AddressBook();
Expand All @@ -29,56 +22,23 @@ public static AddressBook empty() {
*/
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 {
syncTagsWithMasterList(toAdd);
allPersons.add(toAdd);
}

Expand All @@ -99,11 +59,10 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException
}

/**
* Clears all persons and tags from the address book.
* Clears all persons from the address book.
*/
public void clear() {
allPersons.clear();
allTags.clear();
}

/**
Expand All @@ -113,24 +72,15 @@ public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}

/**
* Defensively copied 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));
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(allPersons, allTags);
return allPersons.hashCode();
}
}
23 changes: 13 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,16 @@ 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 +57,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 {@code replacement}.
*/
public void setTags(UniqueTagList replacement) {
tags.setTags(replacement);
public void setTags(Set<Tag> replacement) {
tags.clear();
tags.addAll(replacement);
}

@Override
Expand Down
7 changes: 4 additions & 3 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();

/**
* The returned TagList is a deep copy of the internal TagList,
* The returned {@code Set} is a deep copy of the internal {@code Set},
* changes on the returned list will not affect the person's internal tags.
*/
UniqueTagList getTags();
Set<Tag> getTags();

/**
* Returns true if the values inside this object is same as those of the other (Note: interfaces cannot override .equals)
Expand Down
167 changes: 0 additions & 167 deletions src/seedu/addressbook/data/tag/UniqueTagList.java

This file was deleted.

Loading

0 comments on commit ab72b1f

Please sign in to comment.