Skip to content

Commit

Permalink
AddressBook: remove master tag list
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 addressbook-level4
in se-edu/addressbook-level4#825. As such, the master tag list
should also be removed here for consistency.

Let's remove the master tag list in AddressBook.

Similarly, it is now unnecessary for AdaptedAddressBook to store
a master tag list as well.

Let's remove the master tag list in AdaptedAddressBook.
  • Loading branch information
yamidark committed Aug 9, 2018
1 parent 63b6213 commit ac15304
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 82 deletions.
66 changes: 9 additions & 57 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
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 +23,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 +60,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 +73,16 @@ 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();
}
}
30 changes: 7 additions & 23 deletions src/seedu/addressbook/storage/jaxb/AdaptedAddressBook.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package seedu.addressbook.storage.jaxb;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

/**
* JAXB-friendly adapted address book data holder class.
Expand All @@ -21,8 +20,6 @@ public class AdaptedAddressBook {

@XmlElement
private List<AdaptedPerson> persons = new ArrayList<>();
@XmlElement
private List<AdaptedTag> tags = new ArrayList<>();

/**
* No-arg constructor for JAXB use.
Expand All @@ -36,13 +33,9 @@ public AdaptedAddressBook() {}
*/
public AdaptedAddressBook(AddressBook source) {
persons = new ArrayList<>();
tags = new ArrayList<>();
for (ReadOnlyPerson person : source.getAllPersons()) {
persons.add(new AdaptedPerson(person));
}
for (Tag tag : source.getAllTags()) {
tags.add(new AdaptedTag(tag));
}
}


Expand All @@ -55,11 +48,6 @@ public AdaptedAddressBook(AddressBook source) {
* so we check for that.
*/
public boolean isAnyRequiredFieldMissing() {
for (AdaptedTag tag : tags) {
if (tag.isAnyRequiredFieldMissing()) {
return true;
}
}
for (AdaptedPerson person : persons) {
if (person.isAnyRequiredFieldMissing()) {
return true;
Expand All @@ -74,14 +62,10 @@ public boolean isAnyRequiredFieldMissing() {
* @throws IllegalValueException if there were any data constraints violated in the adapted person
*/
public AddressBook toModelType() throws IllegalValueException {
final List<Tag> tagList = new ArrayList<>();
final List<Person> personList = new ArrayList<>();
for (AdaptedTag tag : tags) {
tagList.add(tag.toModelType());
}
for (AdaptedPerson person : persons) {
personList.add(person.toModelType());
}
return new AddressBook(new UniquePersonList(personList), new UniqueTagList(tagList));
return new AddressBook(new UniquePersonList(personList));
}
}
2 changes: 0 additions & 2 deletions test/data/StorageFileTest/ValidData.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@
<tagged>friend</tagged>
<tagged>criminal</tagged>
</persons>
<tags>friend</tags>
<tags>criminal</tags>
</AddressBook>

0 comments on commit ac15304

Please sign in to comment.