forked from CS2113-AY1819S1-F10-3/main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressBook.java
86 lines (73 loc) · 2.4 KB
/
AddressBook.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package seedu.addressbook.data;
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.
*/
public class AddressBook {
private final UniquePersonList allPersons;
public static AddressBook empty() {
return new AddressBook();
}
/**
* Creates an empty address book.
*/
public AddressBook() {
allPersons = new UniquePersonList();
}
/**
* Constructs an address book with the given data.
*
* @param persons external changes to this will not affect this address book
*/
public AddressBook(UniquePersonList persons) {
this.allPersons = new UniquePersonList(persons);
}
/**
* Adds a person to the address book.
*
* @throws DuplicatePersonException if an equivalent person already exists.
*/
public void addPerson(Person toAdd) throws UniquePersonList.DuplicateNricException {
allPersons.add(toAdd);
}
/**
* Checks if an equivalent person exists in the address book.
*/
public boolean containsPerson(ReadOnlyPerson key) {
return allPersons.contains(key);
}
/**
* Removes the equivalent person from the address book.
*
* @throws PersonNotFoundException if no such Person could be found.
*/
public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException {
allPersons.remove(toRemove);
}
/**
* Clears all persons from the address book.
*/
public void clear() {
allPersons.clear();
}
/**
* Defensively copied UniquePersonList of all persons in the address book at the time of the call.
*/
public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);
}
@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));
}
@Override
public int hashCode() {
return allPersons.hashCode();
}
}