forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
/
XmlAdaptedRestaurant.java
138 lines (118 loc) · 4.64 KB
/
XmlAdaptedRestaurant.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package seedu.address.storage;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.xml.bind.annotation.XmlElement;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.restaurant.Address;
import seedu.address.model.restaurant.Name;
import seedu.address.model.restaurant.Phone;
import seedu.address.model.restaurant.Restaurant;
import seedu.address.model.restaurant.Reviews;
import seedu.address.model.tag.Tag;
/**
* JAXB-friendly version of the Restaurant.
*/
public class XmlAdaptedRestaurant {
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Restaurant's %s field is missing!";
@XmlElement(required = true)
private String name;
@XmlElement(required = true)
private String phone;
@XmlElement(required = true)
private String address;
@XmlElement
private XmlAdaptedReviews reviews;
@XmlElement
private List<XmlAdaptedTag> tagged = new ArrayList<>();
/**
* Constructs an XmlAdaptedRestaurant.
* This is the no-arg constructor that is required by JAXB.
*/
public XmlAdaptedRestaurant() {}
/**
* Constructs an {@code XmlAdaptedRestaurant} with the given restaurant details.
*/
public XmlAdaptedRestaurant(String name, String phone, String address, List<XmlAdaptedTag> tagged,
XmlAdaptedReviews reviews) {
this.name = name;
this.phone = phone;
this.address = address;
if (tagged != null) {
this.tagged = new ArrayList<>(tagged);
}
this.reviews = reviews;
}
/**
* Converts a given Restaurant into this class for JAXB use.
*
* @param source future changes to this will not affect the created XmlAdaptedRestaurant
*/
public XmlAdaptedRestaurant(Restaurant source) {
name = source.getName().fullName;
phone = source.getPhone().value;
address = source.getAddress().value;
tagged = source.getTags().stream()
.map(XmlAdaptedTag::new)
.collect(Collectors.toList());
reviews = new XmlAdaptedReviews(source.getReviews());
}
/**
* Converts this jaxb-friendly adapted restaurant object into the model's Restaurant object.
*
* @throws IllegalValueException if there were any data constraints violated in the adapted restaurant
*/
public Restaurant toModelType() throws IllegalValueException {
final List<Tag> restaurantTags = new ArrayList<>();
for (XmlAdaptedTag tag : tagged) {
restaurantTags.add(tag.toModelType());
}
if (name == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName()));
}
if (!Name.isValidName(name)) {
throw new IllegalValueException(Name.MESSAGE_NAME_CONSTRAINTS);
}
final Name modelName = new Name(name);
if (phone == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName()));
}
if (!Phone.isValidPhone(phone)) {
throw new IllegalValueException(Phone.MESSAGE_PHONE_CONSTRAINTS);
}
final Phone modelPhone = new Phone(phone);
if (address == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Address.class.getSimpleName()));
}
if (!Address.isValidAddress(address)) {
throw new IllegalValueException(Address.MESSAGE_ADDRESS_CONSTRAINTS);
}
final Address modelAddress = new Address(address);
final Set<Tag> modelTags = new HashSet<>(restaurantTags);
Reviews checkReviews;
if (reviews == null) {
checkReviews = new Reviews();
}
checkReviews = reviews.toModelType();
final Reviews modelReviews = checkReviews;
return new Restaurant(modelName, modelPhone, modelAddress, modelTags, modelReviews);
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof XmlAdaptedRestaurant)) {
return false;
}
XmlAdaptedRestaurant otherRestaurant = (XmlAdaptedRestaurant) other;
return Objects.equals(name, otherRestaurant.name)
&& Objects.equals(phone, otherRestaurant.phone)
&& Objects.equals(address, otherRestaurant.address)
&& tagged.equals(otherRestaurant.tagged)
&& reviews.equals(otherRestaurant.reviews);
}
}