forked from nus-cs2103-AY1819S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Source.java
163 lines (137 loc) · 5.05 KB
/
Source.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
package seedu.address.model.source;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.PatternSyntaxException;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.ModelManager;
import seedu.address.model.tag.Tag;
/**
* Represents a Source in the address book.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Source {
private static final Logger logger = LogsCenter.getLogger(ModelManager.class);
private final Title title;
private final Author author;
private final Type type;
private final Detail detail;
private final Set<Tag> tags = new HashSet<>();
private final BiblioFields biblioFields;
private boolean isPinned = false;
public Source(Title title, Author author, Type type, Detail detail, Set<Tag> tags, BiblioFields biblioFields) {
requireAllNonNull(title, author, type, detail, tags, biblioFields);
this.title = title;
this.author = author;
this.type = type;
this.detail = detail;
this.tags.addAll(tags);
this.biblioFields = biblioFields;
}
/**
* A static utility method to validate a source attribute with a regex expression.
*/
/**
* A private utility method to validate a source attribute with a regex expression.
* @param attribute An attribute of the source class, e.g. title.
* @param regex A regex expression with which to validate `attribute`.
* @return true if `attribute` is valid, and false if either `attribute` or `regex` is invalid.
*/
public static boolean validateSourceAttribute(String attribute, String regex) {
boolean validationResult;
try {
validationResult = attribute.matches(regex);
} catch (PatternSyntaxException e) {
logger.severe(e.toString());
return false;
}
return validationResult;
}
public Title getTitle() {
return title;
}
public Author getAuthor() {
return author;
}
public Type getType() {
return type;
}
public Detail getDetail() {
return detail;
}
public BiblioFields getBiblioFields() {
return biblioFields;
}
public boolean getPinnedState() {
return isPinned;
}
public void setPinnedState(boolean isPinned) {
this.isPinned = isPinned;
}
/**
* Returns an immutable tag set which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}
/**
* Returns true if both sources of the have the same title and same detail.
* Two sources will only be the same if they have the same title and same details.
* This defines a weaker notion of equality between two sources.
*/
public boolean isSameSource(Source otherSource) {
if (otherSource.getTitle().title.equals(this.getTitle())
&& otherSource.getDetail().detail.equals(this.getDetail())
&& otherSource.getAuthor().author.equals(this.getAuthor())) {
return true;
}
return otherSource != null
&& otherSource.getTitle().equals(getTitle())
&& otherSource.getDetail().equals(getDetail())
&& otherSource.getAuthor().equals(getAuthor());
}
/**
* Returns true if both sources have the same identity and data fields.
* This defines a stronger notion of equality between two sources.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Source)) {
return false;
}
Source otherSource = (Source) other;
return otherSource.getTitle().equals(getTitle())
&& otherSource.getType().equals(getType())
&& otherSource.getAuthor().equals(getAuthor())
&& otherSource.getDetail().equals(getDetail())
&& otherSource.getTags().equals(getTags())
&& otherSource.getBiblioFields().equals(getBiblioFields());
}
@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(title, type, author, detail, tags, biblioFields);
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Title: ")
.append(getTitle() + "\n")
.append("Type: ")
.append(getType() + "\n")
.append("Author: ")
.append(getAuthor() + "\n")
.append("Detail: ")
.append(getDetail() + "\n")
.append("Tags: ");
getTags().forEach(builder::append);
return builder.toString();
}
}