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 com.google.common.hash.Hasher;
30 import com.google.common.hash.Hashing;
33 * A profile stores personal information about a {@link Sone}. All information
34 * is optional and can be {@code null}.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class Profile implements Fingerprintable {
40 /** The Sone this profile belongs to. */
41 private final Sone sone;
43 /** The first name. */
44 private volatile String firstName;
46 /** The middle name(s). */
47 private volatile String middleName;
50 private volatile String lastName;
52 /** The day of the birth date. */
53 private volatile Integer birthDay;
55 /** The month of the birth date. */
56 private volatile Integer birthMonth;
58 /** The year of the birth date. */
59 private volatile Integer birthYear;
61 /** The ID of the avatar image. */
62 private volatile String avatar;
64 /** Additional fields in the profile. */
65 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
68 * Creates a new empty profile.
71 * The Sone this profile belongs to
73 public Profile(Sone sone) {
78 * Creates a copy of a profile.
83 public Profile(Profile profile) {
84 this.sone = profile.sone;
85 this.firstName = profile.firstName;
86 this.middleName = profile.middleName;
87 this.lastName = profile.lastName;
88 this.birthDay = profile.birthDay;
89 this.birthMonth = profile.birthMonth;
90 this.birthYear = profile.birthYear;
91 this.avatar = profile.avatar;
92 this.fields.addAll(profile.fields);
100 * Returns the Sone this profile belongs to.
102 * @return The Sone this profile belongs to
104 public Sone getSone() {
109 * Returns the first name.
111 * @return The first name
113 public String getFirstName() {
118 * Sets the first name.
121 * The first name to set
122 * @return This profile (for method chaining)
124 public Profile setFirstName(String firstName) {
125 this.firstName = firstName;
130 * Returns the middle name(s).
132 * @return The middle name
134 public String getMiddleName() {
139 * Sets the middle name.
142 * The middle name to set
143 * @return This profile (for method chaining)
145 public Profile setMiddleName(String middleName) {
146 this.middleName = middleName;
151 * Returns the last name.
153 * @return The last name
155 public String getLastName() {
160 * Sets the last name.
163 * The last name to set
164 * @return This profile (for method chaining)
166 public Profile setLastName(String lastName) {
167 this.lastName = lastName;
172 * Returns the day of the birth date.
174 * @return The day of the birth date (from 1 to 31)
176 public Integer getBirthDay() {
181 * Sets the day of the birth date.
184 * The day of the birth date (from 1 to 31)
185 * @return This profile (for method chaining)
187 public Profile setBirthDay(Integer birthDay) {
188 this.birthDay = birthDay;
193 * Returns the month of the birth date.
195 * @return The month of the birth date (from 1 to 12)
197 public Integer getBirthMonth() {
202 * Sets the month of the birth date.
205 * The month of the birth date (from 1 to 12)
206 * @return This profile (for method chaining)
208 public Profile setBirthMonth(Integer birthMonth) {
209 this.birthMonth = birthMonth;
214 * Returns the year of the birth date.
216 * @return The year of the birth date
218 public Integer getBirthYear() {
223 * Returns the ID of the currently selected avatar image.
225 * @return The ID of the currently selected avatar image, or {@code null} if
226 * no avatar is selected.
228 public String getAvatar() {
233 * Sets the avatar image.
236 * The new avatar image, or {@code null} to not select an avatar
240 public Profile setAvatar(Image avatar) {
241 if (avatar == null) {
245 checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
246 this.avatar = avatar.getId();
251 * Sets the year of the birth date.
254 * The year of the birth date
255 * @return This profile (for method chaining)
257 public Profile setBirthYear(Integer birthYear) {
258 this.birthYear = birthYear;
263 * Returns the fields of this profile.
265 * @return The fields of this profile
267 public List<Field> getFields() {
268 return new ArrayList<Field>(fields);
272 * Returns whether this profile contains the given field.
275 * The field to check for
276 * @return {@code true} if this profile contains the field, false otherwise
278 public boolean hasField(Field field) {
279 return fields.contains(field);
283 * Returns the field with the given ID.
286 * The ID of the field to get
287 * @return The field, or {@code null} if this profile does not contain a
288 * field with the given ID
290 public Field getFieldById(String fieldId) {
291 checkNotNull(fieldId, "fieldId must not be null");
292 for (Field field : fields) {
293 if (field.getId().equals(fieldId)) {
301 * Returns the field with the given name.
304 * The name of the field to get
305 * @return The field, or {@code null} if this profile does not contain a
306 * field with the given name
308 public Field getFieldByName(String fieldName) {
309 for (Field field : fields) {
310 if (field.getName().equals(fieldName)) {
318 * Appends a new field to the list of fields.
321 * The name of the new field
322 * @return The new field
323 * @throws IllegalArgumentException
324 * if the name is not valid
326 public Field addField(String fieldName) throws IllegalArgumentException {
327 checkNotNull(fieldName, "fieldName must not be null");
328 if (fieldName.length() == 0) {
329 throw new EmptyFieldName();
331 if (getFieldByName(fieldName) != null) {
332 throw new DuplicateField();
334 @SuppressWarnings("synthetic-access")
335 Field field = new Field().setName(fieldName).setValue("");
341 * Moves the given field up one position in the field list. The index of the
342 * field to move must be greater than {@code 0} (because you obviously can
343 * not move the first field further up).
346 * The field to move up
348 public void moveFieldUp(Field field) {
349 checkNotNull(field, "field must not be null");
350 checkArgument(hasField(field), "field must belong to this profile");
351 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
352 int fieldIndex = getFieldIndex(field);
353 fields.remove(field);
354 fields.add(fieldIndex - 1, field);
358 * Moves the given field down one position in the field list. The index of
359 * the field to move must be less than the index of the last field (because
360 * you obviously can not move the last field further down).
363 * The field to move down
365 public void moveFieldDown(Field field) {
366 checkNotNull(field, "field must not be null");
367 checkArgument(hasField(field), "field must belong to this profile");
368 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
369 int fieldIndex = getFieldIndex(field);
370 fields.remove(field);
371 fields.add(fieldIndex + 1, field);
375 * Removes the given field.
378 * The field to remove
380 public void removeField(Field field) {
381 checkNotNull(field, "field must not be null");
382 checkArgument(hasField(field), "field must belong to this profile");
383 fields.remove(field);
391 * Returns the index of the field with the given name.
394 * The name of the field
395 * @return The index of the field, or {@code -1} if there is no field with
398 private int getFieldIndex(Field field) {
399 return fields.indexOf(field);
403 // INTERFACE Fingerprintable
410 public String getFingerprint() {
411 Hasher hash = Hashing.sha256().newHasher();
412 hash.putString("Profile(");
413 if (firstName != null) {
414 hash.putString("FirstName(").putString(firstName).putString(")");
416 if (middleName != null) {
417 hash.putString("MiddleName(").putString(middleName).putString(")");
419 if (lastName != null) {
420 hash.putString("LastName(").putString(lastName).putString(")");
422 if (birthDay != null) {
423 hash.putString("BirthDay(").putInt(birthDay).putString(")");
425 if (birthMonth != null) {
426 hash.putString("BirthMonth(").putInt(birthMonth).putString(")");
428 if (birthYear != null) {
429 hash.putString("BirthYear(").putInt(birthYear).putString(")");
431 if (avatar != null) {
432 hash.putString("Avatar(").putString(avatar).putString(")");
434 hash.putString("ContactInformation(");
435 for (Field field : fields) {
436 hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
441 return hash.hash().toString();
445 * Container for a profile field.
447 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
451 /** The ID of the field. */
452 private final String id;
454 /** The name of the field. */
457 /** The value of the field. */
458 private String value;
461 * Creates a new field with a random ID.
464 this(UUID.randomUUID().toString());
468 * Creates a new field with the given ID.
471 * The ID of the field
473 private Field(String id) {
474 this.id = checkNotNull(id, "id must not be null");
478 * Returns the ID of this field.
480 * @return The ID of this field
482 public String getId() {
487 * Returns the name of this field.
489 * @return The name of this field
491 public String getName() {
496 * Sets the name of this field. The name must not be {@code null} and
497 * must not match any other fields in this profile but my match the name
501 * The new name of this field
504 public Field setName(String name) {
505 checkNotNull(name, "name must not be null");
506 checkArgument(getFieldByName(name) == null, "name must be unique");
512 * Returns the value of this field.
514 * @return The value of this field
516 public String getValue() {
521 * Sets the value of this field. While {@code null} is allowed, no
522 * guarantees are made that {@code null} values are correctly persisted
523 * across restarts of the plugin!
526 * The new value of this field
529 public Field setValue(String value) {
542 public boolean equals(Object object) {
543 if (!(object instanceof Field)) {
546 Field field = (Field) object;
547 return id.equals(field.id);
554 public int hashCode() {
555 return id.hashCode();
561 * Exception that signals the addition of a field with an empty name.
563 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
565 public static class EmptyFieldName extends IllegalArgumentException { }
568 * Exception that signals the addition of a field that already exists.
570 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
572 public static class DuplicateField extends IllegalArgumentException { }