2 * Sone - Profile.java - Copyright © 2010–2020 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 java.nio.charset.StandardCharsets.UTF_8;
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 public class Profile implements Fingerprintable {
41 /** The Sone this profile belongs to. */
42 private final Sone sone;
44 /** The first name. */
45 private volatile String firstName;
47 /** The middle name(s). */
48 private volatile String middleName;
51 private volatile String lastName;
53 /** The day of the birth date. */
54 private volatile Integer birthDay;
56 /** The month of the birth date. */
57 private volatile Integer birthMonth;
59 /** The year of the birth date. */
60 private volatile Integer birthYear;
62 /** The ID of the avatar image. */
63 private volatile String avatar;
65 /** Additional fields in the profile. */
66 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
69 * Creates a new empty profile.
72 * The Sone this profile belongs to
74 public Profile(Sone sone) {
79 * Creates a copy of a profile.
84 public Profile(@Nonnull Profile profile) {
85 this.sone = profile.sone;
86 this.firstName = profile.firstName;
87 this.middleName = profile.middleName;
88 this.lastName = profile.lastName;
89 this.birthDay = profile.birthDay;
90 this.birthMonth = profile.birthMonth;
91 this.birthYear = profile.birthYear;
92 this.avatar = profile.avatar;
93 this.fields.addAll(profile.fields);
101 * Returns the Sone this profile belongs to.
103 * @return The Sone this profile belongs to
106 public Sone getSone() {
111 * Returns the first name.
113 * @return The first name
116 public String getFirstName() {
121 * Sets the first name.
124 * The first name to set
125 * @return This profile (for method chaining)
128 public Profile setFirstName(@Nullable String firstName) {
129 this.firstName = "".equals(firstName) ? null : firstName;
134 * Returns the middle name(s).
136 * @return The middle name
139 public String getMiddleName() {
144 * Sets the middle name.
147 * The middle name to set
148 * @return This profile (for method chaining)
151 public Profile setMiddleName(@Nullable String middleName) {
152 this.middleName = "".equals(middleName) ? null : middleName;
157 * Returns the last name.
159 * @return The last name
162 public String getLastName() {
167 * Sets the last name.
170 * The last name to set
171 * @return This profile (for method chaining)
174 public Profile setLastName(@Nullable String lastName) {
175 this.lastName = "".equals(lastName) ? null : lastName;
180 * Returns the day of the birth date.
182 * @return The day of the birth date (from 1 to 31)
185 public Integer getBirthDay() {
190 * Sets the day of the birth date.
193 * The day of the birth date (from 1 to 31)
194 * @return This profile (for method chaining)
197 public Profile setBirthDay(@Nullable Integer birthDay) {
198 this.birthDay = birthDay;
203 * Returns the month of the birth date.
205 * @return The month of the birth date (from 1 to 12)
208 public Integer getBirthMonth() {
213 * Sets the month of the birth date.
216 * The month of the birth date (from 1 to 12)
217 * @return This profile (for method chaining)
220 public Profile setBirthMonth(@Nullable Integer birthMonth) {
221 this.birthMonth = birthMonth;
226 * Returns the year of the birth date.
228 * @return The year of the birth date
231 public Integer getBirthYear() {
236 * Returns the ID of the currently selected avatar image.
238 * @return The ID of the currently selected avatar image, or {@code null} if
239 * no avatar is selected.
242 public String getAvatar() {
247 * Sets the avatar image.
250 * The new avatar image, or {@code null} to not select an avatar
255 public Profile setAvatar(@Nullable Image avatar) {
256 if (avatar == null) {
260 checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
261 this.avatar = avatar.getId();
266 * Sets the year of the birth date.
269 * The year of the birth date
270 * @return This profile (for method chaining)
273 public Profile setBirthYear(@Nullable Integer birthYear) {
274 this.birthYear = birthYear;
279 * Returns the fields of this profile.
281 * @return The fields of this profile
284 public List<Field> getFields() {
285 return new ArrayList<>(fields);
289 * Returns whether this profile contains the given field.
292 * The field to check for
293 * @return {@code true} if this profile contains the field, false otherwise
295 public boolean hasField(@Nonnull Field field) {
296 return fields.contains(field);
300 * Returns the field with the given ID.
303 * The ID of the field to get
304 * @return The field, or {@code null} if this profile does not contain a
305 * field with the given ID
308 public Field getFieldById(@Nonnull String fieldId) {
309 checkNotNull(fieldId, "fieldId must not be null");
310 for (Field field : fields) {
311 if (field.getId().equals(fieldId)) {
319 * Returns the field with the given name.
322 * The name of the field to get
323 * @return The field, or {@code null} if this profile does not contain a
324 * field with the given name
327 public Field getFieldByName(@Nonnull String fieldName) {
328 for (Field field : fields) {
329 if (field.getName().equals(fieldName)) {
337 * Appends a new field to the list of fields.
340 * The name of the new field
341 * @return The new field
342 * @throws IllegalArgumentException
343 * if the name is not valid
346 public Field addField(@Nonnull String fieldName) throws IllegalArgumentException {
347 checkNotNull(fieldName, "fieldName must not be null");
348 if (fieldName.length() == 0) {
349 throw new EmptyFieldName();
351 if (getFieldByName(fieldName) != null) {
352 throw new DuplicateField();
354 @SuppressWarnings("synthetic-access")
355 Field field = new Field().setName(fieldName).setValue("");
361 * Moves the given field up one position in the field list. The index of the
362 * field to move must be greater than {@code 0} (because you obviously can
363 * not move the first field further up).
366 * The field to move up
368 public void moveFieldUp(@Nonnull Field field) {
369 checkNotNull(field, "field must not be null");
370 checkArgument(hasField(field), "field must belong to this profile");
371 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
372 int fieldIndex = getFieldIndex(field);
373 fields.remove(field);
374 fields.add(fieldIndex - 1, field);
378 * Moves the given field down one position in the field list. The index of
379 * the field to move must be less than the index of the last field (because
380 * you obviously can not move the last field further down).
383 * The field to move down
385 public void moveFieldDown(@Nonnull Field field) {
386 checkNotNull(field, "field must not be null");
387 checkArgument(hasField(field), "field must belong to this profile");
388 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
389 int fieldIndex = getFieldIndex(field);
390 fields.remove(field);
391 fields.add(fieldIndex + 1, field);
395 * Removes the given field.
398 * The field to remove
400 public void removeField(@Nonnull Field field) {
401 checkNotNull(field, "field must not be null");
402 checkArgument(hasField(field), "field must belong to this profile");
403 fields.remove(field);
411 * Returns the index of the field with the given name.
414 * The name of the field
415 * @return The index of the field, or {@code -1} if there is no field with
418 private int getFieldIndex(@Nonnull Field field) {
419 return fields.indexOf(field);
423 // INTERFACE Fingerprintable
430 public String getFingerprint() {
431 Hasher hash = Hashing.sha256().newHasher();
432 hash.putString("Profile(", UTF_8);
433 if (firstName != null) {
434 hash.putString("FirstName(", UTF_8).putString(firstName, UTF_8).putString(")", UTF_8);
436 if (middleName != null) {
437 hash.putString("MiddleName(", UTF_8).putString(middleName, UTF_8).putString(")", UTF_8);
439 if (lastName != null) {
440 hash.putString("LastName(", UTF_8).putString(lastName, UTF_8).putString(")", UTF_8);
442 if (birthDay != null) {
443 hash.putString("BirthDay(", UTF_8).putInt(birthDay).putString(")", UTF_8);
445 if (birthMonth != null) {
446 hash.putString("BirthMonth(", UTF_8).putInt(birthMonth).putString(")", UTF_8);
448 if (birthYear != null) {
449 hash.putString("BirthYear(", UTF_8).putInt(birthYear).putString(")", UTF_8);
451 if (avatar != null) {
452 hash.putString("Avatar(", UTF_8).putString(avatar, UTF_8).putString(")", UTF_8);
454 hash.putString("ContactInformation(", UTF_8);
455 for (Field field : fields) {
456 hash.putString(field.getName(), UTF_8).putString("(", UTF_8).putString(field.getValue(), UTF_8).putString(")", UTF_8);
458 hash.putString(")", UTF_8);
459 hash.putString(")", UTF_8);
461 return hash.hash().toString();
465 * Container for a profile field.
469 /** The ID of the field. */
470 private final String id;
472 /** The name of the field. */
475 /** The value of the field. */
476 private String value;
479 * Creates a new field with a random ID.
482 this(UUID.randomUUID().toString());
486 * Creates a new field with the given ID.
489 * The ID of the field
491 private Field(@Nonnull String id) {
492 this.id = checkNotNull(id, "id must not be null");
496 * Returns the ID of this field.
498 * @return The ID of this field
501 public String getId() {
506 * Returns the name of this field.
508 * @return The name of this field
511 public String getName() {
516 * Sets the name of this field. The name must not be {@code null} and
517 * must not match any other fields in this profile but my match the name
521 * The new name of this field
525 public Field setName(@Nonnull String name) {
526 checkNotNull(name, "name must not be null");
527 checkArgument(getFieldByName(name) == null, "name must be unique");
533 * Returns the value of this field.
535 * @return The value of this field
538 public String getValue() {
543 * Sets the value of this field. While {@code null} is allowed, no
544 * guarantees are made that {@code null} values are correctly persisted
545 * across restarts of the plugin!
548 * The new value of this field
552 public Field setValue(@Nullable String value) {
565 public boolean equals(Object object) {
566 if (!(object instanceof Field)) {
569 Field field = (Field) object;
570 return id.equals(field.id);
577 public int hashCode() {
578 return id.hashCode();
584 * Exception that signals the addition of a field with an empty name.
586 public static class EmptyFieldName extends IllegalArgumentException { }
589 * Exception that signals the addition of a field that already exists.
591 public static class DuplicateField extends IllegalArgumentException { }