2 * Sone - Profile.java - Copyright © 2010–2019 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;
23 import static java.nio.charset.StandardCharsets.UTF_8;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.UUID;
30 import javax.annotation.Nonnull;
31 import javax.annotation.Nullable;
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 public class Profile implements Fingerprintable {
42 /** The Sone this profile belongs to. */
43 private final Sone sone;
45 /** The first name. */
46 private volatile String firstName;
48 /** The middle name(s). */
49 private volatile String middleName;
52 private volatile String lastName;
54 /** The day of the birth date. */
55 private volatile Integer birthDay;
57 /** The month of the birth date. */
58 private volatile Integer birthMonth;
60 /** The year of the birth date. */
61 private volatile Integer birthYear;
63 /** The ID of the avatar image. */
64 private volatile String avatar;
66 /** Additional fields in the profile. */
67 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
70 * Creates a new empty profile.
73 * The Sone this profile belongs to
75 public Profile(Sone sone) {
80 * Creates a copy of a profile.
85 public Profile(@Nonnull Profile profile) {
86 this.sone = profile.sone;
87 this.firstName = profile.firstName;
88 this.middleName = profile.middleName;
89 this.lastName = profile.lastName;
90 this.birthDay = profile.birthDay;
91 this.birthMonth = profile.birthMonth;
92 this.birthYear = profile.birthYear;
93 this.avatar = profile.avatar;
94 this.fields.addAll(profile.fields);
102 * Returns the Sone this profile belongs to.
104 * @return The Sone this profile belongs to
107 public Sone getSone() {
112 * Returns the first name.
114 * @return The first name
117 public String getFirstName() {
122 * Sets the first name.
125 * The first name to set
126 * @return This profile (for method chaining)
129 public Profile setFirstName(@Nullable String firstName) {
130 this.firstName = "".equals(firstName) ? null : firstName;
135 * Returns the middle name(s).
137 * @return The middle name
140 public String getMiddleName() {
145 * Sets the middle name.
148 * The middle name to set
149 * @return This profile (for method chaining)
152 public Profile setMiddleName(@Nullable String middleName) {
153 this.middleName = "".equals(middleName) ? null : middleName;
158 * Returns the last name.
160 * @return The last name
163 public String getLastName() {
168 * Sets the last name.
171 * The last name to set
172 * @return This profile (for method chaining)
175 public Profile setLastName(@Nullable String lastName) {
176 this.lastName = "".equals(lastName) ? null : lastName;
181 * Returns the day of the birth date.
183 * @return The day of the birth date (from 1 to 31)
186 public Integer getBirthDay() {
191 * Sets the day of the birth date.
194 * The day of the birth date (from 1 to 31)
195 * @return This profile (for method chaining)
198 public Profile setBirthDay(@Nullable Integer birthDay) {
199 this.birthDay = birthDay;
204 * Returns the month of the birth date.
206 * @return The month of the birth date (from 1 to 12)
209 public Integer getBirthMonth() {
214 * Sets the month of the birth date.
217 * The month of the birth date (from 1 to 12)
218 * @return This profile (for method chaining)
221 public Profile setBirthMonth(@Nullable Integer birthMonth) {
222 this.birthMonth = birthMonth;
227 * Returns the year of the birth date.
229 * @return The year of the birth date
232 public Integer getBirthYear() {
237 * Returns the ID of the currently selected avatar image.
239 * @return The ID of the currently selected avatar image, or {@code null} if
240 * no avatar is selected.
243 public String getAvatar() {
248 * Sets the avatar image.
251 * The new avatar image, or {@code null} to not select an avatar
256 public Profile setAvatar(@Nullable Image avatar) {
257 if (avatar == null) {
261 checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
262 this.avatar = avatar.getId();
267 * Sets the year of the birth date.
270 * The year of the birth date
271 * @return This profile (for method chaining)
274 public Profile setBirthYear(@Nullable Integer birthYear) {
275 this.birthYear = birthYear;
280 * Returns the fields of this profile.
282 * @return The fields of this profile
285 public List<Field> getFields() {
286 return new ArrayList<>(fields);
290 * Returns whether this profile contains the given field.
293 * The field to check for
294 * @return {@code true} if this profile contains the field, false otherwise
296 public boolean hasField(@Nonnull Field field) {
297 return fields.contains(field);
301 * Returns the field with the given ID.
304 * The ID of the field to get
305 * @return The field, or {@code null} if this profile does not contain a
306 * field with the given ID
309 public Field getFieldById(@Nonnull String fieldId) {
310 checkNotNull(fieldId, "fieldId must not be null");
311 for (Field field : fields) {
312 if (field.getId().equals(fieldId)) {
320 * Returns the field with the given name.
323 * The name of the field to get
324 * @return The field, or {@code null} if this profile does not contain a
325 * field with the given name
328 public Field getFieldByName(@Nonnull String fieldName) {
329 for (Field field : fields) {
330 if (field.getName().equals(fieldName)) {
338 * Appends a new field to the list of fields.
341 * The name of the new field
342 * @return The new field
343 * @throws IllegalArgumentException
344 * if the name is not valid
347 public Field addField(@Nonnull String fieldName) throws IllegalArgumentException {
348 checkNotNull(fieldName, "fieldName must not be null");
349 if (fieldName.length() == 0) {
350 throw new EmptyFieldName();
352 if (getFieldByName(fieldName) != null) {
353 throw new DuplicateField();
355 @SuppressWarnings("synthetic-access")
356 Field field = new Field().setName(fieldName).setValue("");
362 * Moves the given field up one position in the field list. The index of the
363 * field to move must be greater than {@code 0} (because you obviously can
364 * not move the first field further up).
367 * The field to move up
369 public void moveFieldUp(@Nonnull Field field) {
370 checkNotNull(field, "field must not be null");
371 checkArgument(hasField(field), "field must belong to this profile");
372 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
373 int fieldIndex = getFieldIndex(field);
374 fields.remove(field);
375 fields.add(fieldIndex - 1, field);
379 * Moves the given field down one position in the field list. The index of
380 * the field to move must be less than the index of the last field (because
381 * you obviously can not move the last field further down).
384 * The field to move down
386 public void moveFieldDown(@Nonnull Field field) {
387 checkNotNull(field, "field must not be null");
388 checkArgument(hasField(field), "field must belong to this profile");
389 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
390 int fieldIndex = getFieldIndex(field);
391 fields.remove(field);
392 fields.add(fieldIndex + 1, field);
396 * Removes the given field.
399 * The field to remove
401 public void removeField(@Nonnull Field field) {
402 checkNotNull(field, "field must not be null");
403 checkArgument(hasField(field), "field must belong to this profile");
404 fields.remove(field);
412 * Returns the index of the field with the given name.
415 * The name of the field
416 * @return The index of the field, or {@code -1} if there is no field with
419 private int getFieldIndex(@Nonnull Field field) {
420 return fields.indexOf(field);
424 // INTERFACE Fingerprintable
431 public String getFingerprint() {
432 Hasher hash = Hashing.sha256().newHasher();
433 hash.putString("Profile(", UTF_8);
434 if (firstName != null) {
435 hash.putString("FirstName(", UTF_8).putString(firstName, UTF_8).putString(")", UTF_8);
437 if (middleName != null) {
438 hash.putString("MiddleName(", UTF_8).putString(middleName, UTF_8).putString(")", UTF_8);
440 if (lastName != null) {
441 hash.putString("LastName(", UTF_8).putString(lastName, UTF_8).putString(")", UTF_8);
443 if (birthDay != null) {
444 hash.putString("BirthDay(", UTF_8).putInt(birthDay).putString(")", UTF_8);
446 if (birthMonth != null) {
447 hash.putString("BirthMonth(", UTF_8).putInt(birthMonth).putString(")", UTF_8);
449 if (birthYear != null) {
450 hash.putString("BirthYear(", UTF_8).putInt(birthYear).putString(")", UTF_8);
452 if (avatar != null) {
453 hash.putString("Avatar(", UTF_8).putString(avatar, UTF_8).putString(")", UTF_8);
455 hash.putString("ContactInformation(", UTF_8);
456 for (Field field : fields) {
457 hash.putString(field.getName(), UTF_8).putString("(", UTF_8).putString(field.getValue(), UTF_8).putString(")", UTF_8);
459 hash.putString(")", UTF_8);
460 hash.putString(")", UTF_8);
462 return hash.hash().toString();
466 * Container for a profile field.
470 /** The ID of the field. */
471 private final String id;
473 /** The name of the field. */
476 /** The value of the field. */
477 private String value;
480 * Creates a new field with a random ID.
483 this(UUID.randomUUID().toString());
487 * Creates a new field with the given ID.
490 * The ID of the field
492 private Field(@Nonnull String id) {
493 this.id = checkNotNull(id, "id must not be null");
497 * Returns the ID of this field.
499 * @return The ID of this field
502 public String getId() {
507 * Returns the name of this field.
509 * @return The name of this field
512 public String getName() {
517 * Sets the name of this field. The name must not be {@code null} and
518 * must not match any other fields in this profile but my match the name
522 * The new name of this field
526 public Field setName(@Nonnull String name) {
527 checkNotNull(name, "name must not be null");
528 checkArgument(getFieldByName(name) == null, "name must be unique");
534 * Returns the value of this field.
536 * @return The value of this field
539 public String getValue() {
544 * Sets the value of this field. While {@code null} is allowed, no
545 * guarantees are made that {@code null} values are correctly persisted
546 * across restarts of the plugin!
549 * The new value of this field
553 public Field setValue(@Nullable String value) {
566 public boolean equals(Object object) {
567 if (!(object instanceof Field)) {
570 Field field = (Field) object;
571 return id.equals(field.id);
578 public int hashCode() {
579 return id.hashCode();
585 * Exception that signals the addition of a field with an empty name.
587 public static class EmptyFieldName extends IllegalArgumentException { }
590 * Exception that signals the addition of a field that already exists.
592 public static class DuplicateField extends IllegalArgumentException { }