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.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;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.UUID;
30 import com.google.common.base.Optional;
31 import com.google.common.hash.Hasher;
32 import com.google.common.hash.Hashing;
35 * A profile stores personal information about a {@link Sone}. All information
36 * is optional and can be {@code null}.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class Profile implements Fingerprintable {
42 /** The Sone this profile belongs to. */
43 private final Sone sone;
45 private volatile Name name = new Name();
46 private volatile BirthDate birthDate = new BirthDate();
48 /** The ID of the avatar image. */
49 private volatile String avatar;
51 /** Additional fields in the profile. */
52 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
55 * Creates a new empty profile.
58 * The Sone this profile belongs to
60 public Profile(Sone sone) {
65 * Creates a copy of a profile.
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);
83 * Returns the Sone this profile belongs to.
85 * @return The Sone this profile belongs to
87 public Sone getSone() {
92 * Returns the first name.
94 * @return The first name
96 public String getFirstName() {
97 return name.getFirst().orNull();
101 * Returns the middle name(s).
103 * @return The middle name
105 public String getMiddleName() {
106 return name.getMiddle().orNull();
110 * Returns the last name.
112 * @return The last name
114 public String getLastName() {
115 return name.getLast().orNull();
119 * Returns the day of the birth date.
121 * @return The day of the birth date (from 1 to 31)
123 public Integer getBirthDay() {
124 return birthDate.getDay().orNull();
128 * Returns the month of the birth date.
130 * @return The month of the birth date (from 1 to 12)
132 public Integer getBirthMonth() {
133 return birthDate.getMonth().orNull();
137 * Returns the year of the birth date.
139 * @return The year of the birth date
141 public Integer getBirthYear() {
142 return birthDate.getYear().orNull();
146 * Returns the ID of the currently selected avatar image.
148 * @return The ID of the currently selected avatar image, or {@code null} if
149 * no avatar is selected.
151 public String getAvatar() {
156 * Sets the avatar image.
159 * The ID of the new avatar image
160 * @return This profile
162 public Profile setAvatar(Optional<String> avatarId) {
163 this.avatar = avatarId.orNull();
168 * Returns the fields of this profile.
170 * @return The fields of this profile
172 public List<Field> getFields() {
173 return new ArrayList<Field>(fields);
177 * Returns whether this profile contains the given field.
180 * The field to check for
181 * @return {@code true} if this profile contains the field, false otherwise
183 public boolean hasField(Field field) {
184 return fields.contains(field);
188 * Returns the field with the given ID.
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
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)) {
206 * Returns the field with the given name.
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
213 public Field getFieldByName(String fieldName) {
214 for (Field field : fields) {
215 if (field.getName().equals(fieldName)) {
223 * Appends a new field to the list of fields.
226 * The name of the new field
227 * @return The new field
228 * @throws IllegalArgumentException
229 * if the name is not valid
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);
242 * Moves the given field up one position in the field list. The index of the
243 * field to move must be greater than {@code 0} (because you obviously can
244 * not move the first field further up).
247 * The field to move up
249 public void moveFieldUp(Field field) {
250 checkNotNull(field, "field must not be null");
251 checkArgument(hasField(field), "field must belong to this profile");
252 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
253 int fieldIndex = getFieldIndex(field);
254 fields.remove(field);
255 fields.add(fieldIndex - 1, field);
259 * Moves the given field down one position in the field list. The index of
260 * the field to move must be less than the index of the last field (because
261 * you obviously can not move the last field further down).
264 * The field to move down
266 public void moveFieldDown(Field field) {
267 checkNotNull(field, "field must not be null");
268 checkArgument(hasField(field), "field must belong to this profile");
269 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
270 int fieldIndex = getFieldIndex(field);
271 fields.remove(field);
272 fields.add(fieldIndex + 1, field);
276 * Removes the given field.
279 * The field to remove
281 public void removeField(Field field) {
282 checkNotNull(field, "field must not be null");
283 checkArgument(hasField(field), "field must belong to this profile");
284 fields.remove(field);
287 public Modifier modify() {
288 return new Modifier() {
289 private Optional<String> firstName = name.getFirst();
290 private Optional<String> middleName = name.getMiddle();
291 private Optional<String> lastName = name.getLast();
292 private Optional<Integer> birthYear = birthDate.getYear();
293 private Optional<Integer> birthMonth = birthDate.getMonth();
294 private Optional<Integer> birthDay = birthDate.getDay();
297 public Modifier setFirstName(String firstName) {
298 this.firstName = fromNullable(firstName);
303 public Modifier setMiddleName(String middleName) {
304 this.middleName = fromNullable(middleName);
309 public Modifier setLastName(String lastName) {
310 this.lastName = fromNullable(lastName);
315 public Modifier setBirthYear(Integer birthYear) {
316 this.birthYear = fromNullable(birthYear);
321 public Modifier setBirthMonth(Integer birthMonth) {
322 this.birthMonth = fromNullable(birthMonth);
327 public Modifier setBirthDay(Integer birthDay) {
328 this.birthDay = fromNullable(birthDay);
333 public Profile update() {
334 Profile.this.name = new Name(firstName, middleName, lastName);
335 Profile.this.birthDate = new BirthDate(birthYear, birthMonth, birthDay);
341 public interface Modifier {
343 Modifier setFirstName(String firstName);
344 Modifier setMiddleName(String middleName);
345 Modifier setLastName(String lastName);
346 Modifier setBirthYear(Integer birthYear);
347 Modifier setBirthMonth(Integer birthMonth);
348 Modifier setBirthDay(Integer birthDay);
358 * Returns the index of the field with the given name.
361 * The name of the field
362 * @return The index of the field, or {@code -1} if there is no field with
365 private int getFieldIndex(Field field) {
366 return fields.indexOf(field);
370 // INTERFACE Fingerprintable
377 public String getFingerprint() {
378 Hasher hash = Hashing.sha256().newHasher();
379 hash.putString("Profile(");
380 hash.putString(name.getFingerprint());
381 hash.putString(birthDate.getFingerprint());
382 if (avatar != null) {
383 hash.putString("Avatar(").putString(avatar).putString(")");
385 hash.putString("ContactInformation(");
386 for (Field field : fields) {
387 hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
392 return hash.hash().toString();
396 * Container for a profile field.
398 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
402 /** The ID of the field. */
403 private final String id;
405 /** The name of the field. */
408 /** The value of the field. */
409 private String value;
412 * Creates a new field with a random ID.
415 this(UUID.randomUUID().toString());
419 * Creates a new field with the given ID.
422 * The ID of the field
424 private Field(String id) {
425 this.id = checkNotNull(id, "id must not be null");
428 public Field(String id, String name, String value) {
429 this.id = checkNotNull(id, "id must not be null");
435 * Returns the ID of this field.
437 * @return The ID of this field
439 public String getId() {
444 * Returns the name of this field.
446 * @return The name of this field
448 public String getName() {
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
458 * The new name of this field
461 public Field setName(String name) {
462 checkNotNull(name, "name must not be null");
463 checkArgument(getFieldByName(name) == null, "name must be unique");
469 * Returns the value of this field.
471 * @return The value of this field
473 public String getValue() {
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!
483 * The new value of this field
486 public Field setValue(String value) {
499 public boolean equals(Object object) {
500 if (!(object instanceof Field)) {
503 Field field = (Field) object;
504 return id.equals(field.id);
511 public int hashCode() {
512 return id.hashCode();
517 public static class Name implements Fingerprintable {
519 private final Optional<String> first;
520 private final Optional<String> middle;
521 private final Optional<String> last;
524 this(Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent());
527 public Name(Optional<String> first, Optional<String> middle, Optional<String> last) {
529 this.middle = middle;
533 public Optional<String> getFirst() {
537 public Optional<String> getMiddle() {
541 public Optional<String> getLast() {
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(")");
552 if (middle.isPresent()) {
553 hash.putString("Middle(").putString(middle.get()).putString(")");
555 if (last.isPresent()) {
556 hash.putString("Last(").putString(last.get()).putString(")");
559 return hash.hash().toString();
564 public static class BirthDate implements Fingerprintable {
566 private final Optional<Integer> year;
567 private final Optional<Integer> month;
568 private final Optional<Integer> day;
571 this(Optional.<Integer>absent(), Optional.<Integer>absent(), Optional.<Integer>absent());
574 public BirthDate(Optional<Integer> year, Optional<Integer> month, Optional<Integer> day) {
580 public Optional<Integer> getYear() {
584 public Optional<Integer> getMonth() {
588 public Optional<Integer> getDay() {
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(")");
599 if (month.isPresent()) {
600 hash.putString("Month(").putInt(month.get()).putString(")");
602 if (day.isPresent()) {
603 hash.putString("Day(").putInt(day.get()).putString(")");
606 return hash.hash().toString();