forked from nus-cs2103-AY1617S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tag.java
61 lines (49 loc) · 1.57 KB
/
Tag.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
package seedu.taskman.model.tag;
import seedu.taskman.commons.exceptions.IllegalValueException;
/**
* Represents a Tag in the task man.
* Guarantees: immutable; name is valid as declared in {@link #isValidTagName(String)}
*/
public class Tag {
public static final String MESSAGE_TAG_CONSTRAINTS = "Tag name should form a word. " +
"All punctuation except '/' is accepted";
public static final String TAG_VALIDATION_REGEX = "[\\w\\p{Punct}&&[^/]]+";
public String tagName;
public Tag() {
}
/**
* Validates given tag name.
*
* @throws IllegalValueException if the given tag name string is invalid.
*/
public Tag(String name) throws IllegalValueException {
assert name != null;
name = name.trim();
if (!isValidTagName(name)) {
throw new IllegalValueException(MESSAGE_TAG_CONSTRAINTS);
}
this.tagName = name;
}
/**
* Returns true if a given string is a valid tag name.
*/
public static boolean isValidTagName(String test) {
return test.matches(TAG_VALIDATION_REGEX);
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Tag // instanceof handles nulls
&& this.tagName.equals(((Tag) other).tagName)); // state check
}
@Override
public int hashCode() {
return tagName.hashCode();
}
/**
* Format state as text for viewing.
*/
public String toString() {
return '[' + tagName + ']';
}
}