Fix duplicate-field detection.
[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 com.google.common.base.Preconditions.checkState;
26 import static java.util.UUID.randomUUID;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31
32 import com.google.common.base.Optional;
33 import com.google.common.hash.Hasher;
34 import com.google.common.hash.Hashing;
35
36 /**
37  * A profile stores personal information about a {@link Sone}. All information
38  * is optional and can be {@code null}.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class Profile implements Fingerprintable {
43
44         /** The Sone this profile belongs to. */
45         private final Sone sone;
46
47         private volatile Name name = new Name();
48         private volatile BirthDate birthDate = new BirthDate();
49
50         /** The ID of the avatar image. */
51         private volatile String avatar;
52
53         /** Additional fields in the profile. */
54         private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
55
56         /**
57          * Creates a new empty profile.
58          *
59          * @param sone
60          *            The Sone this profile belongs to
61          */
62         public Profile(Sone sone) {
63                 this.sone = sone;
64         }
65
66         /**
67          * Creates a copy of a profile.
68          *
69          * @param profile
70          *            The profile to copy
71          */
72         public Profile(Profile profile) {
73                 this.sone = profile.sone;
74                 this.name = profile.name;
75                 this.birthDate = profile.birthDate;
76                 this.avatar = profile.avatar;
77                 this.fields.addAll(profile.fields);
78         }
79
80         //
81         // ACCESSORS
82         //
83
84         /**
85          * Returns the Sone this profile belongs to.
86          *
87          * @return The Sone this profile belongs to
88          */
89         public Sone getSone() {
90                 return sone;
91         }
92
93         /**
94          * Returns the first name.
95          *
96          * @return The first name
97          */
98         public String getFirstName() {
99                 return name.getFirst().orNull();
100         }
101
102         /**
103          * Returns the middle name(s).
104          *
105          * @return The middle name
106          */
107         public String getMiddleName() {
108                 return name.getMiddle().orNull();
109         }
110
111         /**
112          * Returns the last name.
113          *
114          * @return The last name
115          */
116         public String getLastName() {
117                 return name.getLast().orNull();
118         }
119
120         /**
121          * Returns the day of the birth date.
122          *
123          * @return The day of the birth date (from 1 to 31)
124          */
125         public Integer getBirthDay() {
126                 return birthDate.getDay().orNull();
127         }
128
129         /**
130          * Returns the month of the birth date.
131          *
132          * @return The month of the birth date (from 1 to 12)
133          */
134         public Integer getBirthMonth() {
135                 return birthDate.getMonth().orNull();
136         }
137
138         /**
139          * Returns the year of the birth date.
140          *
141          * @return The year of the birth date
142          */
143         public Integer getBirthYear() {
144                 return birthDate.getYear().orNull();
145         }
146
147         /**
148          * Returns the ID of the currently selected avatar image.
149          *
150          * @return The ID of the currently selected avatar image, or {@code null} if
151          *         no avatar is selected.
152          */
153         public String getAvatar() {
154                 return avatar;
155         }
156
157         /**
158          * Sets the avatar image.
159          *
160          * @param avatarId
161          *              The ID of the new avatar image
162          * @return This profile
163          */
164         public Profile setAvatar(Optional<String> avatarId) {
165                 this.avatar = avatarId.orNull();
166                 return this;
167         }
168
169         /**
170          * Returns the fields of this profile.
171          *
172          * @return The fields of this profile
173          */
174         public List<Field> getFields() {
175                 return new ArrayList<Field>(fields);
176         }
177
178         /**
179          * Returns whether this profile contains the given field.
180          *
181          * @param field
182          *            The field to check for
183          * @return {@code true} if this profile contains the field, false otherwise
184          */
185         public boolean hasField(Field field) {
186                 return fields.contains(field);
187         }
188
189         public Optional<Field> getFieldById(String fieldId) {
190                 checkNotNull(fieldId, "fieldId must not be null");
191                 for (Field field : fields) {
192                         if (field.getId().equals(fieldId)) {
193                                 return of(field);
194                         }
195                 }
196                 return absent();
197         }
198
199         public Optional<Field> getFieldByName(String fieldName) {
200                 for (Field field : fields) {
201                         if (field.getName().equals(fieldName)) {
202                                 return of(field);
203                         }
204                 }
205                 return absent();
206         }
207
208         /**
209          * Appends a new field to the list of fields.
210          *
211          * @param fieldName
212          *            The name of the new field
213          * @return The new field
214          * @throws IllegalArgumentException
215          *             if the name is not valid
216          */
217         public Field addField(String fieldName) throws IllegalArgumentException {
218                 checkNotNull(fieldName, "fieldName must not be null");
219                 checkArgument(fieldName.length() > 0, "fieldName must not be empty");
220                 checkState(!getFieldByName(fieldName).isPresent(), "fieldName must be unique");
221                 @SuppressWarnings("synthetic-access")
222                 Field field = new Field(fieldName);
223                 fields.add(field);
224                 return field;
225         }
226
227         public void renameField(Field field, String newName) {
228                 int indexOfField = getFieldIndex(field);
229                 if (indexOfField == -1) {
230                         return;
231                 }
232                 fields.set(indexOfField, new Field(field.getId(), newName, field.getValue()));
233         }
234
235         public void setField(Field field, String newValue) {
236                 int indexOfField = getFieldIndex(field);
237                 if (indexOfField == -1) {
238                         return;
239                 }
240                 fields.set(indexOfField, new Field(field.getId(), field.getName(), newValue));
241         }
242
243         /**
244          * Moves the given field up one position in the field list. The index of the
245          * field to move must be greater than {@code 0} (because you obviously can
246          * not move the first field further up).
247          *
248          * @param field
249          *            The field to move up
250          */
251         public void moveFieldUp(Field field) {
252                 checkNotNull(field, "field must not be null");
253                 checkArgument(hasField(field), "field must belong to this profile");
254                 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
255                 int fieldIndex = getFieldIndex(field);
256                 fields.remove(field);
257                 fields.add(fieldIndex - 1, field);
258         }
259
260         /**
261          * Moves the given field down one position in the field list. The index of
262          * the field to move must be less than the index of the last field (because
263          * you obviously can not move the last field further down).
264          *
265          * @param field
266          *            The field to move down
267          */
268         public void moveFieldDown(Field field) {
269                 checkNotNull(field, "field must not be null");
270                 checkArgument(hasField(field), "field must belong to this profile");
271                 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
272                 int fieldIndex = getFieldIndex(field);
273                 fields.remove(field);
274                 fields.add(fieldIndex + 1, field);
275         }
276
277         /**
278          * Removes the given field.
279          *
280          * @param field
281          *            The field to remove
282          */
283         public void removeField(Field field) {
284                 checkNotNull(field, "field must not be null");
285                 checkArgument(hasField(field), "field must belong to this profile");
286                 fields.remove(field);
287         }
288
289         public Modifier modify() {
290                 return new Modifier() {
291                         private Optional<String> firstName = name.getFirst();
292                         private Optional<String> middleName = name.getMiddle();
293                         private Optional<String> lastName = name.getLast();
294                         private Optional<Integer> birthYear = birthDate.getYear();
295                         private Optional<Integer> birthMonth = birthDate.getMonth();
296                         private Optional<Integer> birthDay = birthDate.getDay();
297
298                         @Override
299                         public Modifier setFirstName(String firstName) {
300                                 this.firstName = fromNullable(firstName);
301                                 return this;
302                         }
303
304                         @Override
305                         public Modifier setMiddleName(String middleName) {
306                                 this.middleName = fromNullable(middleName);
307                                 return this;
308                         }
309
310                         @Override
311                         public Modifier setLastName(String lastName) {
312                                 this.lastName = fromNullable(lastName);
313                                 return this;
314                         }
315
316                         @Override
317                         public Modifier setBirthYear(Integer birthYear) {
318                                 this.birthYear = fromNullable(birthYear);
319                                 return this;
320                         }
321
322                         @Override
323                         public Modifier setBirthMonth(Integer birthMonth) {
324                                 this.birthMonth = fromNullable(birthMonth);
325                                 return this;
326                         }
327
328                         @Override
329                         public Modifier setBirthDay(Integer birthDay) {
330                                 this.birthDay = fromNullable(birthDay);
331                                 return this;
332                         }
333
334                         @Override
335                         public Profile update() {
336                                 Profile.this.name = new Name(firstName, middleName, lastName);
337                                 Profile.this.birthDate = new BirthDate(birthYear, birthMonth, birthDay);
338                                 return Profile.this;
339                         }
340                 };
341         }
342
343         public interface Modifier {
344
345                 Modifier setFirstName(String firstName);
346                 Modifier setMiddleName(String middleName);
347                 Modifier setLastName(String lastName);
348                 Modifier setBirthYear(Integer birthYear);
349                 Modifier setBirthMonth(Integer birthMonth);
350                 Modifier setBirthDay(Integer birthDay);
351                 Profile update();
352
353         }
354
355         //
356         // PRIVATE METHODS
357         //
358
359         /**
360          * Returns the index of the field with the given name.
361          *
362          * @param field
363          *            The name of the field
364          * @return The index of the field, or {@code -1} if there is no field with
365          *         the given name
366          */
367         private int getFieldIndex(Field field) {
368                 return fields.indexOf(field);
369         }
370
371         //
372         // INTERFACE Fingerprintable
373         //
374
375         /**
376          * {@inheritDoc}
377          */
378         @Override
379         public String getFingerprint() {
380                 Hasher hash = Hashing.sha256().newHasher();
381                 hash.putString("Profile(");
382                 hash.putString(name.getFingerprint());
383                 hash.putString(birthDate.getFingerprint());
384                 if (avatar != null) {
385                         hash.putString("Avatar(").putString(avatar).putString(")");
386                 }
387                 hash.putString("ContactInformation(");
388                 for (Field field : fields) {
389                         hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
390                 }
391                 hash.putString(")");
392                 hash.putString(")");
393
394                 return hash.hash().toString();
395         }
396
397         /**
398          * Container for a profile field.
399          *
400          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
401          */
402         public static class Field {
403
404                 private final String id;
405                 private final String name;
406                 private final String value;
407
408                 public Field(String name) {
409                         this(name, null);
410                 }
411
412                 public Field(String name, String value) {
413                         this(randomUUID().toString(), name, value);
414                 }
415
416                 public Field(String id, String name, String value) {
417                         this.id = checkNotNull(id, "id must not be null");
418                         this.name = name;
419                         this.value = value;
420                 }
421
422                 public String getId() {
423                         return id;
424                 }
425
426                 public String getName() {
427                         return name;
428                 }
429
430                 public String getValue() {
431                         return value;
432                 }
433
434                 @Override
435                 public boolean equals(Object object) {
436                         if (!(object instanceof Field)) {
437                                 return false;
438                         }
439                         Field field = (Field) object;
440                         return id.equals(field.id);
441                 }
442
443                 @Override
444                 public int hashCode() {
445                         return id.hashCode();
446                 }
447
448         }
449
450         public static class Name implements Fingerprintable {
451
452                 private final Optional<String> first;
453                 private final Optional<String> middle;
454                 private final Optional<String> last;
455
456                 public Name() {
457                         this(Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent());
458                 }
459
460                 public Name(Optional<String> first, Optional<String> middle, Optional<String> last) {
461                         this.first = first;
462                         this.middle = middle;
463                         this.last = last;
464                 }
465
466                 public Optional<String> getFirst() {
467                         return first;
468                 }
469
470                 public Optional<String> getMiddle() {
471                         return middle;
472                 }
473
474                 public Optional<String> getLast() {
475                         return last;
476                 }
477
478                 @Override
479                 public String getFingerprint() {
480                         Hasher hash = Hashing.sha256().newHasher();
481                         hash.putString("Name(");
482                         if (first.isPresent()) {
483                                 hash.putString("First(").putString(first.get()).putString(")");
484                         }
485                         if (middle.isPresent()) {
486                                 hash.putString("Middle(").putString(middle.get()).putString(")");
487                         }
488                         if (last.isPresent()) {
489                                 hash.putString("Last(").putString(last.get()).putString(")");
490                         }
491                         hash.putString(")");
492                         return hash.hash().toString();
493                 }
494
495         }
496
497         public static class BirthDate implements Fingerprintable {
498
499                 private final Optional<Integer> year;
500                 private final Optional<Integer> month;
501                 private final Optional<Integer> day;
502
503                 public BirthDate() {
504                         this(Optional.<Integer>absent(), Optional.<Integer>absent(), Optional.<Integer>absent());
505                 }
506
507                 public BirthDate(Optional<Integer> year, Optional<Integer> month, Optional<Integer> day) {
508                         this.year = year;
509                         this.month = month;
510                         this.day = day;
511                 }
512
513                 public Optional<Integer> getYear() {
514                         return year;
515                 }
516
517                 public Optional<Integer> getMonth() {
518                         return month;
519                 }
520
521                 public Optional<Integer> getDay() {
522                         return day;
523                 }
524
525                 @Override
526                 public String getFingerprint() {
527                         Hasher hash = Hashing.sha256().newHasher();
528                         hash.putString("Birthdate(");
529                         if (year.isPresent()) {
530                                 hash.putString("Year(").putInt(year.get()).putString(")");
531                         }
532                         if (month.isPresent()) {
533                                 hash.putString("Month(").putInt(month.get()).putString(")");
534                         }
535                         if (day.isPresent()) {
536                                 hash.putString("Day(").putInt(day.get()).putString(")");
537                         }
538                         hash.putString(")");
539                         return hash.hash().toString();
540                 }
541
542         }
543
544 }