Add method to rename a field.
[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 avatarId
159          *              The ID of the new avatar image
160          * @return This profile
161          */
162         public Profile setAvatar(Optional<String> avatarId) {
163                 this.avatar = avatarId.orNull();
164                 return this;
165         }
166
167         /**
168          * Returns the fields of this profile.
169          *
170          * @return The fields of this profile
171          */
172         public List<Field> getFields() {
173                 return new ArrayList<Field>(fields);
174         }
175
176         /**
177          * Returns whether this profile contains the given field.
178          *
179          * @param field
180          *            The field to check for
181          * @return {@code true} if this profile contains the field, false otherwise
182          */
183         public boolean hasField(Field field) {
184                 return fields.contains(field);
185         }
186
187         /**
188          * Returns the field with the given ID.
189          *
190          * @param fieldId
191          *            The ID of the field to get
192          * @return The field, or {@code null} if this profile does not contain a
193          *         field with the given ID
194          */
195         public Field getFieldById(String fieldId) {
196                 checkNotNull(fieldId, "fieldId must not be null");
197                 for (Field field : fields) {
198                         if (field.getId().equals(fieldId)) {
199                                 return field;
200                         }
201                 }
202                 return null;
203         }
204
205         /**
206          * Returns the field with the given name.
207          *
208          * @param fieldName
209          *            The name of the field to get
210          * @return The field, or {@code null} if this profile does not contain a
211          *         field with the given name
212          */
213         public Field getFieldByName(String fieldName) {
214                 for (Field field : fields) {
215                         if (field.getName().equals(fieldName)) {
216                                 return field;
217                         }
218                 }
219                 return null;
220         }
221
222         /**
223          * Appends a new field to the list of fields.
224          *
225          * @param fieldName
226          *            The name of the new field
227          * @return The new field
228          * @throws IllegalArgumentException
229          *             if the name is not valid
230          */
231         public Field addField(String fieldName) throws IllegalArgumentException {
232                 checkNotNull(fieldName, "fieldName must not be null");
233                 checkArgument(fieldName.length() > 0, "fieldName must not be empty");
234                 checkState(getFieldByName(fieldName) == null, "fieldName must be unique");
235                 @SuppressWarnings("synthetic-access")
236                 Field field = new Field().setName(fieldName);
237                 fields.add(field);
238                 return field;
239         }
240
241         public void renameField(Field field, String newName) {
242                 int indexOfField = fields.indexOf(field);
243                 if (indexOfField == -1) {
244                         return;
245                 }
246                 fields.set(indexOfField, new Field(field.getId(), newName, field.getValue()));
247         }
248
249         /**
250          * Moves the given field up one position in the field list. The index of the
251          * field to move must be greater than {@code 0} (because you obviously can
252          * not move the first field further up).
253          *
254          * @param field
255          *            The field to move up
256          */
257         public void moveFieldUp(Field field) {
258                 checkNotNull(field, "field must not be null");
259                 checkArgument(hasField(field), "field must belong to this profile");
260                 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
261                 int fieldIndex = getFieldIndex(field);
262                 fields.remove(field);
263                 fields.add(fieldIndex - 1, field);
264         }
265
266         /**
267          * Moves the given field down one position in the field list. The index of
268          * the field to move must be less than the index of the last field (because
269          * you obviously can not move the last field further down).
270          *
271          * @param field
272          *            The field to move down
273          */
274         public void moveFieldDown(Field field) {
275                 checkNotNull(field, "field must not be null");
276                 checkArgument(hasField(field), "field must belong to this profile");
277                 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
278                 int fieldIndex = getFieldIndex(field);
279                 fields.remove(field);
280                 fields.add(fieldIndex + 1, field);
281         }
282
283         /**
284          * Removes the given field.
285          *
286          * @param field
287          *            The field to remove
288          */
289         public void removeField(Field field) {
290                 checkNotNull(field, "field must not be null");
291                 checkArgument(hasField(field), "field must belong to this profile");
292                 fields.remove(field);
293         }
294
295         public Modifier modify() {
296                 return new Modifier() {
297                         private Optional<String> firstName = name.getFirst();
298                         private Optional<String> middleName = name.getMiddle();
299                         private Optional<String> lastName = name.getLast();
300                         private Optional<Integer> birthYear = birthDate.getYear();
301                         private Optional<Integer> birthMonth = birthDate.getMonth();
302                         private Optional<Integer> birthDay = birthDate.getDay();
303
304                         @Override
305                         public Modifier setFirstName(String firstName) {
306                                 this.firstName = fromNullable(firstName);
307                                 return this;
308                         }
309
310                         @Override
311                         public Modifier setMiddleName(String middleName) {
312                                 this.middleName = fromNullable(middleName);
313                                 return this;
314                         }
315
316                         @Override
317                         public Modifier setLastName(String lastName) {
318                                 this.lastName = fromNullable(lastName);
319                                 return this;
320                         }
321
322                         @Override
323                         public Modifier setBirthYear(Integer birthYear) {
324                                 this.birthYear = fromNullable(birthYear);
325                                 return this;
326                         }
327
328                         @Override
329                         public Modifier setBirthMonth(Integer birthMonth) {
330                                 this.birthMonth = fromNullable(birthMonth);
331                                 return this;
332                         }
333
334                         @Override
335                         public Modifier setBirthDay(Integer birthDay) {
336                                 this.birthDay = fromNullable(birthDay);
337                                 return this;
338                         }
339
340                         @Override
341                         public Profile update() {
342                                 Profile.this.name = new Name(firstName, middleName, lastName);
343                                 Profile.this.birthDate = new BirthDate(birthYear, birthMonth, birthDay);
344                                 return Profile.this;
345                         }
346                 };
347         }
348
349         public interface Modifier {
350
351                 Modifier setFirstName(String firstName);
352                 Modifier setMiddleName(String middleName);
353                 Modifier setLastName(String lastName);
354                 Modifier setBirthYear(Integer birthYear);
355                 Modifier setBirthMonth(Integer birthMonth);
356                 Modifier setBirthDay(Integer birthDay);
357                 Profile update();
358
359         }
360
361         //
362         // PRIVATE METHODS
363         //
364
365         /**
366          * Returns the index of the field with the given name.
367          *
368          * @param field
369          *            The name of the field
370          * @return The index of the field, or {@code -1} if there is no field with
371          *         the given name
372          */
373         private int getFieldIndex(Field field) {
374                 return fields.indexOf(field);
375         }
376
377         //
378         // INTERFACE Fingerprintable
379         //
380
381         /**
382          * {@inheritDoc}
383          */
384         @Override
385         public String getFingerprint() {
386                 Hasher hash = Hashing.sha256().newHasher();
387                 hash.putString("Profile(");
388                 hash.putString(name.getFingerprint());
389                 hash.putString(birthDate.getFingerprint());
390                 if (avatar != null) {
391                         hash.putString("Avatar(").putString(avatar).putString(")");
392                 }
393                 hash.putString("ContactInformation(");
394                 for (Field field : fields) {
395                         hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
396                 }
397                 hash.putString(")");
398                 hash.putString(")");
399
400                 return hash.hash().toString();
401         }
402
403         /**
404          * Container for a profile field.
405          *
406          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
407          */
408         public class Field {
409
410                 /** The ID of the field. */
411                 private final String id;
412
413                 /** The name of the field. */
414                 private String name;
415
416                 /** The value of the field. */
417                 private String value;
418
419                 /**
420                  * Creates a new field with a random ID.
421                  */
422                 private Field() {
423                         this(UUID.randomUUID().toString());
424                 }
425
426                 /**
427                  * Creates a new field with the given ID.
428                  *
429                  * @param id
430                  *            The ID of the field
431                  */
432                 private Field(String id) {
433                         this.id = checkNotNull(id, "id must not be null");
434                 }
435
436                 public Field(String id, String name, String value) {
437                         this.id = checkNotNull(id, "id must not be null");
438                         this.name = name;
439                         this.value = value;
440                 }
441
442                 /**
443                  * Returns the ID of this field.
444                  *
445                  * @return The ID of this field
446                  */
447                 public String getId() {
448                         return id;
449                 }
450
451                 /**
452                  * Returns the name of this field.
453                  *
454                  * @return The name of this field
455                  */
456                 public String getName() {
457                         return name;
458                 }
459
460                 /**
461                  * Sets the name of this field. The name must not be {@code null} and
462                  * must not match any other fields in this profile but my match the name
463                  * of this field.
464                  *
465                  * @param name
466                  *            The new name of this field
467                  * @return This field
468                  */
469                 public Field setName(String name) {
470                         checkNotNull(name, "name must not be null");
471                         checkArgument(getFieldByName(name) == null, "name must be unique");
472                         this.name = name;
473                         return this;
474                 }
475
476                 /**
477                  * Returns the value of this field.
478                  *
479                  * @return The value of this field
480                  */
481                 public String getValue() {
482                         return value;
483                 }
484
485                 /**
486                  * Sets the value of this field. While {@code null} is allowed, no
487                  * guarantees are made that {@code null} values are correctly persisted
488                  * across restarts of the plugin!
489                  *
490                  * @param value
491                  *            The new value of this field
492                  * @return This field
493                  */
494                 public Field setValue(String value) {
495                         this.value = value;
496                         return this;
497                 }
498
499                 //
500                 // OBJECT METHODS
501                 //
502
503                 /**
504                  * {@inheritDoc}
505                  */
506                 @Override
507                 public boolean equals(Object object) {
508                         if (!(object instanceof Field)) {
509                                 return false;
510                         }
511                         Field field = (Field) object;
512                         return id.equals(field.id);
513                 }
514
515                 /**
516                  * {@inheritDoc}
517                  */
518                 @Override
519                 public int hashCode() {
520                         return id.hashCode();
521                 }
522
523         }
524
525         public static class Name implements Fingerprintable {
526
527                 private final Optional<String> first;
528                 private final Optional<String> middle;
529                 private final Optional<String> last;
530
531                 public Name() {
532                         this(Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent());
533                 }
534
535                 public Name(Optional<String> first, Optional<String> middle, Optional<String> last) {
536                         this.first = first;
537                         this.middle = middle;
538                         this.last = last;
539                 }
540
541                 public Optional<String> getFirst() {
542                         return first;
543                 }
544
545                 public Optional<String> getMiddle() {
546                         return middle;
547                 }
548
549                 public Optional<String> getLast() {
550                         return last;
551                 }
552
553                 @Override
554                 public String getFingerprint() {
555                         Hasher hash = Hashing.sha256().newHasher();
556                         hash.putString("Name(");
557                         if (first.isPresent()) {
558                                 hash.putString("First(").putString(first.get()).putString(")");
559                         }
560                         if (middle.isPresent()) {
561                                 hash.putString("Middle(").putString(middle.get()).putString(")");
562                         }
563                         if (last.isPresent()) {
564                                 hash.putString("Last(").putString(last.get()).putString(")");
565                         }
566                         hash.putString(")");
567                         return hash.hash().toString();
568                 }
569
570         }
571
572         public static class BirthDate implements Fingerprintable {
573
574                 private final Optional<Integer> year;
575                 private final Optional<Integer> month;
576                 private final Optional<Integer> day;
577
578                 public BirthDate() {
579                         this(Optional.<Integer>absent(), Optional.<Integer>absent(), Optional.<Integer>absent());
580                 }
581
582                 public BirthDate(Optional<Integer> year, Optional<Integer> month, Optional<Integer> day) {
583                         this.year = year;
584                         this.month = month;
585                         this.day = day;
586                 }
587
588                 public Optional<Integer> getYear() {
589                         return year;
590                 }
591
592                 public Optional<Integer> getMonth() {
593                         return month;
594                 }
595
596                 public Optional<Integer> getDay() {
597                         return day;
598                 }
599
600                 @Override
601                 public String getFingerprint() {
602                         Hasher hash = Hashing.sha256().newHasher();
603                         hash.putString("Birthdate(");
604                         if (year.isPresent()) {
605                                 hash.putString("Year(").putInt(year.get()).putString(")");
606                         }
607                         if (month.isPresent()) {
608                                 hash.putString("Month(").putInt(month.get()).putString(")");
609                         }
610                         if (day.isPresent()) {
611                                 hash.putString("Day(").putInt(day.get()).putString(")");
612                         }
613                         hash.putString(")");
614                         return hash.hash().toString();
615                 }
616
617         }
618
619 }