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