2 * Sone - Profile.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
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;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
32 import com.google.common.base.Optional;
33 import com.google.common.hash.Hasher;
34 import com.google.common.hash.Hashing;
37 * A profile stores personal information about a {@link Sone}. All information
38 * is optional and can be {@code null}.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class Profile implements Fingerprintable {
44 /** The Sone this profile belongs to. */
45 private final Sone sone;
47 private volatile Name name = new Name();
48 private volatile BirthDate birthDate = new BirthDate();
50 /** The ID of the avatar image. */
51 private volatile String avatar;
53 /** Additional fields in the profile. */
54 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
57 * Creates a new empty profile.
60 * The Sone this profile belongs to
62 public Profile(Sone sone) {
67 * Creates a copy of a profile.
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);
85 * Returns the Sone this profile belongs to.
87 * @return The Sone this profile belongs to
89 public Sone getSone() {
94 * Returns the first name.
96 * @return The first name
98 public String getFirstName() {
99 return name.getFirst().orNull();
103 * Returns the middle name(s).
105 * @return The middle name
107 public String getMiddleName() {
108 return name.getMiddle().orNull();
112 * Returns the last name.
114 * @return The last name
116 public String getLastName() {
117 return name.getLast().orNull();
121 * Returns the day of the birth date.
123 * @return The day of the birth date (from 1 to 31)
125 public Integer getBirthDay() {
126 return birthDate.getDay().orNull();
130 * Returns the month of the birth date.
132 * @return The month of the birth date (from 1 to 12)
134 public Integer getBirthMonth() {
135 return birthDate.getMonth().orNull();
139 * Returns the year of the birth date.
141 * @return The year of the birth date
143 public Integer getBirthYear() {
144 return birthDate.getYear().orNull();
148 * Returns the ID of the currently selected avatar image.
150 * @return The ID of the currently selected avatar image, or {@code null} if
151 * no avatar is selected.
153 public String getAvatar() {
158 * Sets the avatar image.
161 * The ID of the new avatar image
162 * @return This profile
164 public Profile setAvatar(Optional<String> avatarId) {
165 this.avatar = avatarId.orNull();
170 * Returns the fields of this profile.
172 * @return The fields of this profile
174 public List<Field> getFields() {
175 return new ArrayList<Field>(fields);
179 * Returns whether this profile contains the given field.
182 * The field to check for
183 * @return {@code true} if this profile contains the field, false otherwise
185 public boolean hasField(Field field) {
186 return fields.contains(field);
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)) {
199 public Optional<Field> getFieldByName(String fieldName) {
200 for (Field field : fields) {
201 if (field.getName().equals(fieldName)) {
209 * Appends a new field to the list of fields.
212 * The name of the new field
213 * @return The new field
214 * @throws IllegalArgumentException
215 * if the name is not valid
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);
227 public void renameField(Field field, String newName) {
228 int indexOfField = getFieldIndex(field);
229 if (indexOfField == -1) {
232 fields.set(indexOfField, new Field(field.getId(), newName, field.getValue()));
235 public void setField(Field field, String newValue) {
236 int indexOfField = getFieldIndex(field);
237 if (indexOfField == -1) {
240 fields.set(indexOfField, new Field(field.getId(), field.getName(), newValue));
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).
249 * The field to move up
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);
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).
266 * The field to move down
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);
278 * Removes the given field.
281 * The field to remove
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);
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();
299 public Modifier setFirstName(String firstName) {
300 this.firstName = fromNullable(firstName);
305 public Modifier setMiddleName(String middleName) {
306 this.middleName = fromNullable(middleName);
311 public Modifier setLastName(String lastName) {
312 this.lastName = fromNullable(lastName);
317 public Modifier setBirthYear(Integer birthYear) {
318 this.birthYear = fromNullable(birthYear);
323 public Modifier setBirthMonth(Integer birthMonth) {
324 this.birthMonth = fromNullable(birthMonth);
329 public Modifier setBirthDay(Integer birthDay) {
330 this.birthDay = fromNullable(birthDay);
335 public Profile update() {
336 Profile.this.name = new Name(firstName, middleName, lastName);
337 Profile.this.birthDate = new BirthDate(birthYear, birthMonth, birthDay);
343 public interface Modifier {
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);
360 * Returns the index of the field with the given name.
363 * The name of the field
364 * @return The index of the field, or {@code -1} if there is no field with
367 private int getFieldIndex(Field field) {
368 return fields.indexOf(field);
372 // INTERFACE Fingerprintable
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(")");
387 hash.putString("ContactInformation(");
388 for (Field field : fields) {
389 hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
394 return hash.hash().toString();
398 * Container for a profile field.
400 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
402 public static class Field {
404 private final String id;
405 private final String name;
406 private final String value;
408 public Field(String name) {
412 public Field(String name, String value) {
413 this(randomUUID().toString(), name, value);
416 public Field(String id, String name, String value) {
417 this.id = checkNotNull(id, "id must not be null");
422 public String getId() {
426 public String getName() {
430 public String getValue() {
435 public boolean equals(Object object) {
436 if (!(object instanceof Field)) {
439 Field field = (Field) object;
440 return id.equals(field.id);
444 public int hashCode() {
445 return id.hashCode();
450 public static class Name implements Fingerprintable {
452 private final Optional<String> first;
453 private final Optional<String> middle;
454 private final Optional<String> last;
457 this(Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent());
460 public Name(Optional<String> first, Optional<String> middle, Optional<String> last) {
462 this.middle = middle;
466 public Optional<String> getFirst() {
470 public Optional<String> getMiddle() {
474 public Optional<String> getLast() {
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(")");
485 if (middle.isPresent()) {
486 hash.putString("Middle(").putString(middle.get()).putString(")");
488 if (last.isPresent()) {
489 hash.putString("Last(").putString(last.get()).putString(")");
492 return hash.hash().toString();
497 public static class BirthDate implements Fingerprintable {
499 private final Optional<Integer> year;
500 private final Optional<Integer> month;
501 private final Optional<Integer> day;
504 this(Optional.<Integer>absent(), Optional.<Integer>absent(), Optional.<Integer>absent());
507 public BirthDate(Optional<Integer> year, Optional<Integer> month, Optional<Integer> day) {
513 public Optional<Integer> getYear() {
517 public Optional<Integer> getMonth() {
521 public Optional<Integer> getDay() {
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(")");
532 if (month.isPresent()) {
533 hash.putString("Month(").putInt(month.get()).putString(")");
535 if (day.isPresent()) {
536 hash.putString("Day(").putInt(day.get()).putString(")");
539 return hash.hash().toString();