2 * Sone - Profile.java - Copyright © 2010–2016 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.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.UUID;
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
32 import com.google.common.hash.Hasher;
33 import com.google.common.hash.Hashing;
36 * A profile stores personal information about a {@link Sone}. All information
37 * is optional and can be {@code null}.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class Profile implements Fingerprintable {
43 /** The Sone this profile belongs to. */
44 private final Sone sone;
46 /** The first name. */
47 private volatile String firstName;
49 /** The middle name(s). */
50 private volatile String middleName;
53 private volatile String lastName;
55 /** The day of the birth date. */
56 private volatile Integer birthDay;
58 /** The month of the birth date. */
59 private volatile Integer birthMonth;
61 /** The year of the birth date. */
62 private volatile Integer birthYear;
64 /** The ID of the avatar image. */
65 private volatile String avatar;
67 /** Additional fields in the profile. */
68 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
71 * Creates a new empty profile.
74 * The Sone this profile belongs to
76 public Profile(Sone sone) {
81 * Creates a copy of a profile.
86 public Profile(@Nonnull Profile profile) {
87 this.sone = profile.sone;
88 this.firstName = profile.firstName;
89 this.middleName = profile.middleName;
90 this.lastName = profile.lastName;
91 this.birthDay = profile.birthDay;
92 this.birthMonth = profile.birthMonth;
93 this.birthYear = profile.birthYear;
94 this.avatar = profile.avatar;
95 this.fields.addAll(profile.fields);
103 * Returns the Sone this profile belongs to.
105 * @return The Sone this profile belongs to
108 public Sone getSone() {
113 * Returns the first name.
115 * @return The first name
118 public String getFirstName() {
123 * Sets the first name.
126 * The first name to set
127 * @return This profile (for method chaining)
130 public Profile setFirstName(@Nullable String firstName) {
131 this.firstName = firstName;
136 * Returns the middle name(s).
138 * @return The middle name
141 public String getMiddleName() {
146 * Sets the middle name.
149 * The middle name to set
150 * @return This profile (for method chaining)
153 public Profile setMiddleName(@Nullable String middleName) {
154 this.middleName = middleName;
159 * Returns the last name.
161 * @return The last name
164 public String getLastName() {
169 * Sets the last name.
172 * The last name to set
173 * @return This profile (for method chaining)
176 public Profile setLastName(@Nullable String lastName) {
177 this.lastName = lastName;
182 * Returns the day of the birth date.
184 * @return The day of the birth date (from 1 to 31)
187 public Integer getBirthDay() {
192 * Sets the day of the birth date.
195 * The day of the birth date (from 1 to 31)
196 * @return This profile (for method chaining)
199 public Profile setBirthDay(@Nullable Integer birthDay) {
200 this.birthDay = birthDay;
205 * Returns the month of the birth date.
207 * @return The month of the birth date (from 1 to 12)
210 public Integer getBirthMonth() {
215 * Sets the month of the birth date.
218 * The month of the birth date (from 1 to 12)
219 * @return This profile (for method chaining)
222 public Profile setBirthMonth(@Nullable Integer birthMonth) {
223 this.birthMonth = birthMonth;
228 * Returns the year of the birth date.
230 * @return The year of the birth date
233 public Integer getBirthYear() {
238 * Returns the ID of the currently selected avatar image.
240 * @return The ID of the currently selected avatar image, or {@code null} if
241 * no avatar is selected.
244 public String getAvatar() {
249 * Sets the avatar image.
252 * The new avatar image, or {@code null} to not select an avatar
257 public Profile setAvatar(@Nullable Image avatar) {
258 if (avatar == null) {
262 checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
263 this.avatar = avatar.getId();
268 * Sets the year of the birth date.
271 * The year of the birth date
272 * @return This profile (for method chaining)
275 public Profile setBirthYear(@Nullable Integer birthYear) {
276 this.birthYear = birthYear;
281 * Returns the fields of this profile.
283 * @return The fields of this profile
286 public List<Field> getFields() {
287 return new ArrayList<Field>(fields);
291 * Returns whether this profile contains the given field.
294 * The field to check for
295 * @return {@code true} if this profile contains the field, false otherwise
297 public boolean hasField(@Nonnull Field field) {
298 return fields.contains(field);
302 * Returns the field with the given ID.
305 * The ID of the field to get
306 * @return The field, or {@code null} if this profile does not contain a
307 * field with the given ID
310 public Field getFieldById(@Nonnull String fieldId) {
311 checkNotNull(fieldId, "fieldId must not be null");
312 for (Field field : fields) {
313 if (field.getId().equals(fieldId)) {
321 * Returns the field with the given name.
324 * The name of the field to get
325 * @return The field, or {@code null} if this profile does not contain a
326 * field with the given name
329 public Field getFieldByName(@Nonnull String fieldName) {
330 for (Field field : fields) {
331 if (field.getName().equals(fieldName)) {
339 * Appends a new field to the list of fields.
342 * The name of the new field
343 * @return The new field
344 * @throws IllegalArgumentException
345 * if the name is not valid
348 public Field addField(@Nonnull String fieldName) throws IllegalArgumentException {
349 checkNotNull(fieldName, "fieldName must not be null");
350 if (fieldName.length() == 0) {
351 throw new EmptyFieldName();
353 if (getFieldByName(fieldName) != null) {
354 throw new DuplicateField();
356 @SuppressWarnings("synthetic-access")
357 Field field = new Field().setName(fieldName).setValue("");
363 * Moves the given field up one position in the field list. The index of the
364 * field to move must be greater than {@code 0} (because you obviously can
365 * not move the first field further up).
368 * The field to move up
370 public void moveFieldUp(@Nonnull Field field) {
371 checkNotNull(field, "field must not be null");
372 checkArgument(hasField(field), "field must belong to this profile");
373 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
374 int fieldIndex = getFieldIndex(field);
375 fields.remove(field);
376 fields.add(fieldIndex - 1, field);
380 * Moves the given field down one position in the field list. The index of
381 * the field to move must be less than the index of the last field (because
382 * you obviously can not move the last field further down).
385 * The field to move down
387 public void moveFieldDown(@Nonnull Field field) {
388 checkNotNull(field, "field must not be null");
389 checkArgument(hasField(field), "field must belong to this profile");
390 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
391 int fieldIndex = getFieldIndex(field);
392 fields.remove(field);
393 fields.add(fieldIndex + 1, field);
397 * Removes the given field.
400 * The field to remove
402 public void removeField(@Nonnull Field field) {
403 checkNotNull(field, "field must not be null");
404 checkArgument(hasField(field), "field must belong to this profile");
405 fields.remove(field);
413 * Returns the index of the field with the given name.
416 * The name of the field
417 * @return The index of the field, or {@code -1} if there is no field with
420 private int getFieldIndex(@Nonnull Field field) {
421 return fields.indexOf(field);
425 // INTERFACE Fingerprintable
432 public String getFingerprint() {
433 Hasher hash = Hashing.sha256().newHasher();
434 hash.putString("Profile(");
435 if (firstName != null) {
436 hash.putString("FirstName(").putString(firstName).putString(")");
438 if (middleName != null) {
439 hash.putString("MiddleName(").putString(middleName).putString(")");
441 if (lastName != null) {
442 hash.putString("LastName(").putString(lastName).putString(")");
444 if (birthDay != null) {
445 hash.putString("BirthDay(").putInt(birthDay).putString(")");
447 if (birthMonth != null) {
448 hash.putString("BirthMonth(").putInt(birthMonth).putString(")");
450 if (birthYear != null) {
451 hash.putString("BirthYear(").putInt(birthYear).putString(")");
453 if (avatar != null) {
454 hash.putString("Avatar(").putString(avatar).putString(")");
456 hash.putString("ContactInformation(");
457 for (Field field : fields) {
458 hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
463 return hash.hash().toString();
467 * Container for a profile field.
469 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
473 /** The ID of the field. */
474 private final String id;
476 /** The name of the field. */
479 /** The value of the field. */
480 private String value;
483 * Creates a new field with a random ID.
486 this(UUID.randomUUID().toString());
490 * Creates a new field with the given ID.
493 * The ID of the field
495 private Field(@Nonnull String id) {
496 this.id = checkNotNull(id, "id must not be null");
500 * Returns the ID of this field.
502 * @return The ID of this field
505 public String getId() {
510 * Returns the name of this field.
512 * @return The name of this field
515 public String getName() {
520 * Sets the name of this field. The name must not be {@code null} and
521 * must not match any other fields in this profile but my match the name
525 * The new name of this field
529 public Field setName(@Nonnull String name) {
530 checkNotNull(name, "name must not be null");
531 checkArgument(getFieldByName(name) == null, "name must be unique");
537 * Returns the value of this field.
539 * @return The value of this field
542 public String getValue() {
547 * Sets the value of this field. While {@code null} is allowed, no
548 * guarantees are made that {@code null} values are correctly persisted
549 * across restarts of the plugin!
552 * The new value of this field
556 public Field setValue(@Nullable String value) {
569 public boolean equals(Object object) {
570 if (!(object instanceof Field)) {
573 Field field = (Field) object;
574 return id.equals(field.id);
581 public int hashCode() {
582 return id.hashCode();
588 * Exception that signals the addition of a field with an empty name.
590 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
592 public static class EmptyFieldName extends IllegalArgumentException { }
595 * Exception that signals the addition of a field that already exists.
597 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
599 public static class DuplicateField extends IllegalArgumentException { }