Use a static import.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Profile.java
1 /*
2  * Sone - Profile.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import static com.google.common.base.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Optional.of;
23 import static com.google.common.base.Preconditions.checkArgument;
24 import static com.google.common.base.Preconditions.checkNotNull;
25 import static java.lang.Math.max;
26 import static java.lang.Math.min;
27 import static java.util.UUID.randomUUID;
28
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32
33 import com.google.common.base.Optional;
34 import com.google.common.hash.Hasher;
35 import com.google.common.hash.Hashing;
36
37 /**
38  * A profile stores personal information about a {@link Sone}. All information
39  * is optional and can be {@code null}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class Profile implements Fingerprintable {
44
45         /** The Sone this profile belongs to. */
46         private final Sone sone;
47
48         private volatile Name name = new Name();
49         private volatile BirthDate birthDate = new BirthDate();
50
51         /** The ID of the avatar image. */
52         private volatile String avatar;
53
54         /** Additional fields in the profile. */
55         private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
56
57         /**
58          * Creates a new empty profile.
59          *
60          * @param sone
61          *            The Sone this profile belongs to
62          */
63         public Profile(Sone sone) {
64                 this.sone = sone;
65         }
66
67         /**
68          * Creates a copy of a profile.
69          *
70          * @param profile
71          *            The profile to copy
72          */
73         public Profile(Profile profile) {
74                 this.sone = profile.sone;
75                 this.name = profile.name;
76                 this.birthDate = profile.birthDate;
77                 this.avatar = profile.avatar;
78                 this.fields.addAll(profile.fields);
79         }
80
81         //
82         // ACCESSORS
83         //
84
85         /**
86          * Returns the Sone this profile belongs to.
87          *
88          * @return The Sone this profile belongs to
89          */
90         public Sone getSone() {
91                 return sone;
92         }
93
94         public String getFirstName() {
95                 return name.getFirst().orNull();
96         }
97
98         public String getMiddleName() {
99                 return name.getMiddle().orNull();
100         }
101
102         public String getLastName() {
103                 return name.getLast().orNull();
104         }
105
106         public Integer getBirthDay() {
107                 return birthDate.getDay().orNull();
108         }
109
110         public Integer getBirthMonth() {
111                 return birthDate.getMonth().orNull();
112         }
113
114         public Integer getBirthYear() {
115                 return birthDate.getYear().orNull();
116         }
117
118         public String getAvatar() {
119                 return avatar;
120         }
121
122         public Profile setAvatar(Optional<String> avatarId) {
123                 this.avatar = avatarId.orNull();
124                 return this;
125         }
126
127         public List<Field> getFields() {
128                 return new ArrayList<Field>(fields);
129         }
130
131         public boolean hasField(Field field) {
132                 return fields.contains(field);
133         }
134
135         public Optional<Field> getFieldById(String fieldId) {
136                 checkNotNull(fieldId, "fieldId must not be null");
137                 for (Field field : fields) {
138                         if (field.getId().equals(fieldId)) {
139                                 return of(field);
140                         }
141                 }
142                 return absent();
143         }
144
145         public Optional<Field> getFieldByName(String fieldName) {
146                 for (Field field : fields) {
147                         if (field.getName().equals(fieldName)) {
148                                 return of(field);
149                         }
150                 }
151                 return absent();
152         }
153
154         public Field addField(String fieldName) throws IllegalArgumentException {
155                 checkNotNull(fieldName, "fieldName must not be null");
156                 checkArgument(fieldName.length() > 0, "fieldName must not be empty");
157                 checkArgument(!getFieldByName(fieldName).isPresent(), "fieldName must be unique");
158                 @SuppressWarnings("synthetic-access")
159                 Field field = new Field(fieldName);
160                 fields.add(field);
161                 return field;
162         }
163
164         public void renameField(Field field, String newName) {
165                 int indexOfField = getFieldIndex(field);
166                 if (indexOfField == -1) {
167                         return;
168                 }
169                 fields.set(indexOfField, new Field(field.getId(), newName, field.getValue()));
170         }
171
172         public void setField(Field field, String newValue) {
173                 int indexOfField = getFieldIndex(field);
174                 if (indexOfField == -1) {
175                         return;
176                 }
177                 fields.set(indexOfField, new Field(field.getId(), field.getName(), newValue));
178         }
179
180         public void moveFieldUp(Field field) {
181                 checkNotNull(field, "field must not be null");
182                 checkArgument(hasField(field), "field must belong to this profile");
183                 int fieldIndex = getFieldIndex(field);
184                 fields.remove(field);
185                 fields.add(max(fieldIndex - 1, 0), field);
186         }
187
188         public void moveFieldDown(Field field) {
189                 checkNotNull(field, "field must not be null");
190                 checkArgument(hasField(field), "field must belong to this profile");
191                 int fieldIndex = getFieldIndex(field);
192                 fields.remove(field);
193                 fields.add(min(fieldIndex + 1, fields.size()), field);
194         }
195
196         public void removeField(Field field) {
197                 checkNotNull(field, "field must not be null");
198                 fields.remove(field);
199         }
200
201         public Modifier modify() {
202                 return new Modifier() {
203                         private Optional<String> firstName = name.getFirst();
204                         private Optional<String> middleName = name.getMiddle();
205                         private Optional<String> lastName = name.getLast();
206                         private Optional<Integer> birthYear = birthDate.getYear();
207                         private Optional<Integer> birthMonth = birthDate.getMonth();
208                         private Optional<Integer> birthDay = birthDate.getDay();
209
210                         @Override
211                         public Modifier setFirstName(String firstName) {
212                                 this.firstName = fromNullable(firstName);
213                                 return this;
214                         }
215
216                         @Override
217                         public Modifier setMiddleName(String middleName) {
218                                 this.middleName = fromNullable(middleName);
219                                 return this;
220                         }
221
222                         @Override
223                         public Modifier setLastName(String lastName) {
224                                 this.lastName = fromNullable(lastName);
225                                 return this;
226                         }
227
228                         @Override
229                         public Modifier setBirthYear(Integer birthYear) {
230                                 this.birthYear = fromNullable(birthYear);
231                                 return this;
232                         }
233
234                         @Override
235                         public Modifier setBirthMonth(Integer birthMonth) {
236                                 this.birthMonth = fromNullable(birthMonth);
237                                 return this;
238                         }
239
240                         @Override
241                         public Modifier setBirthDay(Integer birthDay) {
242                                 this.birthDay = fromNullable(birthDay);
243                                 return this;
244                         }
245
246                         @Override
247                         public Profile update() {
248                                 Profile.this.name = new Name(firstName, middleName, lastName);
249                                 Profile.this.birthDate = new BirthDate(birthYear, birthMonth, birthDay);
250                                 return Profile.this;
251                         }
252                 };
253         }
254
255         public interface Modifier {
256
257                 Modifier setFirstName(String firstName);
258                 Modifier setMiddleName(String middleName);
259                 Modifier setLastName(String lastName);
260                 Modifier setBirthYear(Integer birthYear);
261                 Modifier setBirthMonth(Integer birthMonth);
262                 Modifier setBirthDay(Integer birthDay);
263                 Profile update();
264
265         }
266
267         //
268         // PRIVATE METHODS
269         //
270
271         /**
272          * Returns the index of the field with the given name.
273          *
274          * @param field
275          *            The name of the field
276          * @return The index of the field, or {@code -1} if there is no field with
277          *         the given name
278          */
279         private int getFieldIndex(Field field) {
280                 return fields.indexOf(field);
281         }
282
283         //
284         // INTERFACE Fingerprintable
285         //
286
287         @Override
288         public String getFingerprint() {
289                 Hasher hash = Hashing.sha256().newHasher();
290                 hash.putString("Profile(");
291                 hash.putString(name.getFingerprint());
292                 hash.putString(birthDate.getFingerprint());
293                 if (avatar != null) {
294                         hash.putString("Avatar(").putString(avatar).putString(")");
295                 }
296                 hash.putString("ContactInformation(");
297                 for (Field field : fields) {
298                         if (field.getValue() != null) {
299                                 hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
300                         }
301                 }
302                 hash.putString(")");
303                 hash.putString(")");
304
305                 return hash.hash().toString();
306         }
307
308         /**
309          * Container for a profile field.
310          *
311          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
312          */
313         public static class Field {
314
315                 private final String id;
316                 private final String name;
317                 private final String value;
318
319                 public Field(String name) {
320                         this(name, null);
321                 }
322
323                 public Field(String name, String value) {
324                         this(randomUUID().toString(), name, value);
325                 }
326
327                 public Field(String id, String name, String value) {
328                         this.id = checkNotNull(id, "id must not be null");
329                         this.name = name;
330                         this.value = value;
331                 }
332
333                 public String getId() {
334                         return id;
335                 }
336
337                 public String getName() {
338                         return name;
339                 }
340
341                 public String getValue() {
342                         return value;
343                 }
344
345                 @Override
346                 public boolean equals(Object object) {
347                         if (!(object instanceof Field)) {
348                                 return false;
349                         }
350                         Field field = (Field) object;
351                         return id.equals(field.id);
352                 }
353
354                 @Override
355                 public int hashCode() {
356                         return id.hashCode();
357                 }
358
359         }
360
361         public static class Name implements Fingerprintable {
362
363                 private final Optional<String> first;
364                 private final Optional<String> middle;
365                 private final Optional<String> last;
366
367                 public Name() {
368                         this(Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent());
369                 }
370
371                 public Name(Optional<String> first, Optional<String> middle, Optional<String> last) {
372                         this.first = first;
373                         this.middle = middle;
374                         this.last = last;
375                 }
376
377                 public Optional<String> getFirst() {
378                         return first;
379                 }
380
381                 public Optional<String> getMiddle() {
382                         return middle;
383                 }
384
385                 public Optional<String> getLast() {
386                         return last;
387                 }
388
389                 @Override
390                 public String getFingerprint() {
391                         Hasher hash = Hashing.sha256().newHasher();
392                         hash.putString("Name(");
393                         if (first.isPresent()) {
394                                 hash.putString("First(").putString(first.get()).putString(")");
395                         }
396                         if (middle.isPresent()) {
397                                 hash.putString("Middle(").putString(middle.get()).putString(")");
398                         }
399                         if (last.isPresent()) {
400                                 hash.putString("Last(").putString(last.get()).putString(")");
401                         }
402                         hash.putString(")");
403                         return hash.hash().toString();
404                 }
405
406         }
407
408         public static class BirthDate implements Fingerprintable {
409
410                 private final Optional<Integer> year;
411                 private final Optional<Integer> month;
412                 private final Optional<Integer> day;
413
414                 public BirthDate() {
415                         this(Optional.<Integer>absent(), Optional.<Integer>absent(), Optional.<Integer>absent());
416                 }
417
418                 public BirthDate(Optional<Integer> year, Optional<Integer> month, Optional<Integer> day) {
419                         this.year = year;
420                         this.month = month;
421                         this.day = day;
422                 }
423
424                 public Optional<Integer> getYear() {
425                         return year;
426                 }
427
428                 public Optional<Integer> getMonth() {
429                         return month;
430                 }
431
432                 public Optional<Integer> getDay() {
433                         return day;
434                 }
435
436                 @Override
437                 public String getFingerprint() {
438                         Hasher hash = Hashing.sha256().newHasher();
439                         hash.putString("Birthdate(");
440                         if (year.isPresent()) {
441                                 hash.putString("Year(").putInt(year.get()).putString(")");
442                         }
443                         if (month.isPresent()) {
444                                 hash.putString("Month(").putInt(month.get()).putString(")");
445                         }
446                         if (day.isPresent()) {
447                                 hash.putString("Day(").putInt(day.get()).putString(")");
448                         }
449                         hash.putString(")");
450                         return hash.hash().toString();
451                 }
452
453         }
454
455 }