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
/
ParserUtil.java
168 lines (152 loc) · 6.88 KB
/
ParserUtil.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package seedu.address.logic.parser;
import static java.util.Objects.requireNonNull;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.commons.util.StringUtil;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
/**
* Contains utility methods used for parsing strings in the various *Parser classes.
* {@code ParserUtil} contains methods that take in {@code Optional} as parameters. However, it goes against Java's
* convention (see https://stackoverflow.com/a/39005452) as {@code Optional} should only be used a return type.
* Justification: The methods in concern receive {@code Optional} return values from other methods as parameters and
* return {@code Optional} values based on whether the parameters were present. Therefore, it is redundant to unwrap the
* initial {@code Optional} before passing to {@code ParserUtil} as a parameter and then re-wrap it into an
* {@code Optional} return value inside {@code ParserUtil} methods.
*/
public class ParserUtil {
public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";
public static final String MESSAGE_INSUFFICIENT_PARTS = "Number of parts must be more than 1.";
/**
* Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be
* trimmed.
* @throws IllegalValueException if the specified index is invalid (not non-zero unsigned integer).
*/
public static Index parseIndex(String oneBasedIndex) throws IllegalValueException {
String trimmedIndex = oneBasedIndex.trim();
if (!StringUtil.isNonZeroUnsignedInteger(trimmedIndex)) {
throw new IllegalValueException(MESSAGE_INVALID_INDEX);
}
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}
/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws IllegalValueException if the given {@code name} is invalid.
*/
public static Name parseName(String name) throws IllegalValueException {
requireNonNull(name);
String trimmedName = name.trim();
if (!Name.isValidName(trimmedName)) {
throw new IllegalValueException(Name.MESSAGE_NAME_CONSTRAINTS);
}
return new Name(trimmedName);
}
/**
* Parses a {@code Optional<String> name} into an {@code Optional<Name>} if {@code name} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Name> parseName(Optional<String> name) throws IllegalValueException {
requireNonNull(name);
return name.isPresent() ? Optional.of(parseName(name.get())) : Optional.empty();
}
/**
* Parses a {@code String phone} into a {@code Phone}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws IllegalValueException if the given {@code phone} is invalid.
*/
public static Phone parsePhone(String phone) throws IllegalValueException {
requireNonNull(phone);
String trimmedPhone = phone.trim();
if (!Phone.isValidPhone(trimmedPhone)) {
throw new IllegalValueException(Phone.MESSAGE_PHONE_CONSTRAINTS);
}
return new Phone(trimmedPhone);
}
/**
* Parses a {@code Optional<String> phone} into an {@code Optional<Phone>} if {@code phone} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Phone> parsePhone(Optional<String> phone) throws IllegalValueException {
requireNonNull(phone);
return phone.isPresent() ? Optional.of(parsePhone(phone.get())) : Optional.empty();
}
/**
* Parses a {@code String address} into an {@code Address}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws IllegalValueException if the given {@code address} is invalid.
*/
public static Address parseAddress(String address) throws IllegalValueException {
requireNonNull(address);
String trimmedAddress = address.trim();
if (!Address.isValidAddress(trimmedAddress)) {
throw new IllegalValueException(Address.MESSAGE_ADDRESS_CONSTRAINTS);
}
return new Address(trimmedAddress);
}
/**
* Parses a {@code Optional<String> address} into an {@code Optional<Address>} if {@code address} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Address> parseAddress(Optional<String> address) throws IllegalValueException {
requireNonNull(address);
return address.isPresent() ? Optional.of(parseAddress(address.get())) : Optional.empty();
}
/**
* Parses a {@code String email} into an {@code Email}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws IllegalValueException if the given {@code email} is invalid.
*/
public static Email parseEmail(String email) throws IllegalValueException {
requireNonNull(email);
String trimmedEmail = email.trim();
if (!Email.isValidEmail(trimmedEmail)) {
throw new IllegalValueException(Email.MESSAGE_EMAIL_CONSTRAINTS);
}
return new Email(trimmedEmail);
}
/**
* Parses a {@code Optional<String> email} into an {@code Optional<Email>} if {@code email} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Email> parseEmail(Optional<String> email) throws IllegalValueException {
requireNonNull(email);
return email.isPresent() ? Optional.of(parseEmail(email.get())) : Optional.empty();
}
/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws IllegalValueException if the given {@code tag} is invalid.
*/
public static Tag parseTag(String tag) throws IllegalValueException {
requireNonNull(tag);
String trimmedTag = tag.trim();
if (!Tag.isValidTagName(trimmedTag)) {
throw new IllegalValueException(Tag.MESSAGE_TAG_CONSTRAINTS);
}
return new Tag(trimmedTag);
}
/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>}.
*/
public static Set<Tag> parseTags(Collection<String> tags) throws IllegalValueException {
requireNonNull(tags);
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
tagSet.add(parseTag(tagName));
}
return tagSet;
}
}