This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathAddressBook.java
118 lines (96 loc) · 3.56 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package seedu.address.model;
import static java.util.Objects.requireNonNull;
import java.util.List;
import javafx.collections.ObservableList;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniquePersonList;
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;
/**
* Wraps all data at the address-book level
* Duplicates are not allowed (by .isSamePerson comparison)
*/
public class AddressBook implements ReadOnlyAddressBook {
private final UniquePersonList persons;
/*
* The 'unusual' code block below is an non-static initialization block, sometimes used to avoid duplication
* between constructors. See https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
*
* Note that non-static init blocks are not recommended to use. There are other ways to avoid duplication
* among constructors.
*/
{
persons = new UniquePersonList();
}
public AddressBook() {}
/**
* Creates an AddressBook using the Persons in the {@code toBeCopied}
*/
public AddressBook(ReadOnlyAddressBook toBeCopied) {
this();
resetData(toBeCopied);
}
//// list overwrite operations
public void setPersons(List<Person> persons) throws DuplicatePersonException {
this.persons.setPersons(persons);
}
/**
* Resets the existing data of this {@code AddressBook} with {@code newData}.
*/
public void resetData(ReadOnlyAddressBook newData) {
requireNonNull(newData);
try {
setPersons(newData.getPersonList());
} catch (DuplicatePersonException e) {
throw new AssertionError("AddressBooks should not have duplicate persons", e);
}
}
//// person-level operations
/**
* Adds a person to the address book.
*
* @throws DuplicatePersonException if an equivalent person already exists.
*/
public void addPerson(Person p) throws DuplicatePersonException {
persons.add(p);
}
/**
* Replaces the given person {@code target} in the list with {@code editedPerson}.
*
* @throws DuplicatePersonException if updating the person's details causes the person to be equivalent to
* another existing person in the list.
* @throws PersonNotFoundException if {@code target} could not be found in the list.
*/
public void updatePerson(Person target, Person editedPerson)
throws DuplicatePersonException, PersonNotFoundException {
requireNonNull(editedPerson);
persons.setPerson(target, editedPerson);
}
/**
* Removes {@code key} from this {@code AddressBook}.
* @throws PersonNotFoundException if the {@code key} is not in this {@code AddressBook}.
*/
public void removePerson(Person key) throws PersonNotFoundException {
persons.remove(key);
}
//// util methods
@Override
public String toString() {
return persons.asUnmodifiableObservableList().size() + " persons";
// TODO: refine later
}
@Override
public ObservableList<Person> getPersonList() {
return persons.asUnmodifiableObservableList();
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddressBook // instanceof handles nulls
&& this.persons.equals(((AddressBook) other).persons));
}
@Override
public int hashCode() {
return persons.hashCode();
}
}