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.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;
30 * A profile stores personal information about a {@link Sone}. All information
31 * is optional and can be {@code null}.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class Profile implements Fingerprintable {
37 /** The Sone this profile belongs to. */
38 private final Sone sone;
40 /** The first name. */
41 private volatile String firstName;
43 /** The middle name(s). */
44 private volatile String middleName;
47 private volatile String lastName;
49 /** The day of the birth date. */
50 private volatile Integer birthDay;
52 /** The month of the birth date. */
53 private volatile Integer birthMonth;
55 /** The year of the birth date. */
56 private volatile Integer birthYear;
58 /** The ID of the avatar image. */
59 private volatile String avatar;
61 /** Additional fields in the profile. */
62 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
65 * Creates a new empty profile.
68 * The Sone this profile belongs to
70 public Profile(Sone sone) {
75 * Creates a copy of a profile.
80 public Profile(Profile profile) {
81 this.sone = profile.sone;
82 this.firstName = profile.firstName;
83 this.middleName = profile.middleName;
84 this.lastName = profile.lastName;
85 this.birthDay = profile.birthDay;
86 this.birthMonth = profile.birthMonth;
87 this.birthYear = profile.birthYear;
88 this.avatar = profile.avatar;
89 this.fields.addAll(profile.fields);
97 * Returns the Sone this profile belongs to.
99 * @return The Sone this profile belongs to
101 public Sone getSone() {
106 * Returns the first name.
108 * @return The first name
110 public String getFirstName() {
115 * Sets the first name.
118 * The first name to set
119 * @return This profile (for method chaining)
121 public Profile setFirstName(String firstName) {
122 this.firstName = firstName;
127 * Returns the middle name(s).
129 * @return The middle name
131 public String getMiddleName() {
136 * Sets the middle name.
139 * The middle name to set
140 * @return This profile (for method chaining)
142 public Profile setMiddleName(String middleName) {
143 this.middleName = middleName;
148 * Returns the last name.
150 * @return The last name
152 public String getLastName() {
157 * Sets the last name.
160 * The last name to set
161 * @return This profile (for method chaining)
163 public Profile setLastName(String lastName) {
164 this.lastName = lastName;
169 * Returns the day of the birth date.
171 * @return The day of the birth date (from 1 to 31)
173 public Integer getBirthDay() {
178 * Sets the day of the birth date.
181 * The day of the birth date (from 1 to 31)
182 * @return This profile (for method chaining)
184 public Profile setBirthDay(Integer birthDay) {
185 this.birthDay = birthDay;
190 * Returns the month of the birth date.
192 * @return The month of the birth date (from 1 to 12)
194 public Integer getBirthMonth() {
199 * Sets the month of the birth date.
202 * The month of the birth date (from 1 to 12)
203 * @return This profile (for method chaining)
205 public Profile setBirthMonth(Integer birthMonth) {
206 this.birthMonth = birthMonth;
211 * Returns the year of the birth date.
213 * @return The year of the birth date
215 public Integer getBirthYear() {
220 * Returns the ID of the currently selected avatar image.
222 * @return The ID of the currently selected avatar image, or {@code null} if
223 * no avatar is selected.
225 public String getAvatar() {
230 * Sets the avatar image.
233 * The new avatar image, or {@code null} to not select an avatar
237 public Profile setAvatar(Image avatar) {
238 if (avatar == null) {
242 checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
243 this.avatar = avatar.getId();
248 * Sets the year of the birth date.
251 * The year of the birth date
252 * @return This profile (for method chaining)
254 public Profile setBirthYear(Integer birthYear) {
255 this.birthYear = birthYear;
260 * Returns the fields of this profile.
262 * @return The fields of this profile
264 public List<Field> getFields() {
265 return new ArrayList<Field>(fields);
269 * Returns whether this profile contains the given field.
272 * The field to check for
273 * @return {@code true} if this profile contains the field, false otherwise
275 public boolean hasField(Field field) {
276 return fields.contains(field);
280 * Returns the field with the given ID.
283 * The ID of the field to get
284 * @return The field, or {@code null} if this profile does not contain a
285 * field with the given ID
287 public Field getFieldById(String fieldId) {
288 checkNotNull(fieldId, "fieldId must not be null");
289 for (Field field : fields) {
290 if (field.getId().equals(fieldId)) {
298 * Returns the field with the given name.
301 * The name of the field to get
302 * @return The field, or {@code null} if this profile does not contain a
303 * field with the given name
305 public Field getFieldByName(String fieldName) {
306 for (Field field : fields) {
307 if (field.getName().equals(fieldName)) {
315 * Appends a new field to the list of fields.
318 * The name of the new field
319 * @return The new field
320 * @throws IllegalArgumentException
321 * if the name is not valid
323 public Field addField(String fieldName) throws IllegalArgumentException {
324 checkNotNull(fieldName, "fieldName must not be null");
325 checkArgument(fieldName.length() > 0, "fieldName must not be empty");
326 checkState(getFieldByName(fieldName) == null, "fieldName must be unique");
327 @SuppressWarnings("synthetic-access")
328 Field field = new Field().setName(fieldName);
334 * Moves the given field up one position in the field list. The index of the
335 * field to move must be greater than {@code 0} (because you obviously can
336 * not move the first field further up).
339 * The field to move up
341 public void moveFieldUp(Field field) {
342 checkNotNull(field, "field must not be null");
343 checkArgument(hasField(field), "field must belong to this profile");
344 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
345 int fieldIndex = getFieldIndex(field);
346 fields.remove(field);
347 fields.add(fieldIndex - 1, field);
351 * Moves the given field down one position in the field list. The index of
352 * the field to move must be less than the index of the last field (because
353 * you obviously can not move the last field further down).
356 * The field to move down
358 public void moveFieldDown(Field field) {
359 checkNotNull(field, "field must not be null");
360 checkArgument(hasField(field), "field must belong to this profile");
361 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
362 int fieldIndex = getFieldIndex(field);
363 fields.remove(field);
364 fields.add(fieldIndex + 1, field);
368 * Removes the given field.
371 * The field to remove
373 public void removeField(Field field) {
374 checkNotNull(field, "field must not be null");
375 checkArgument(hasField(field), "field must belong to this profile");
376 fields.remove(field);
384 * Returns the index of the field with the given name.
387 * The name of the field
388 * @return The index of the field, or {@code -1} if there is no field with
391 private int getFieldIndex(Field field) {
392 return fields.indexOf(field);
396 // INTERFACE Fingerprintable
403 public String getFingerprint() {
404 StringBuilder fingerprint = new StringBuilder();
405 fingerprint.append("Profile(");
406 if (firstName != null) {
407 fingerprint.append("FirstName(").append(firstName).append(')');
409 if (middleName != null) {
410 fingerprint.append("MiddleName(").append(middleName).append(')');
412 if (lastName != null) {
413 fingerprint.append("LastName(").append(lastName).append(')');
415 if (birthDay != null) {
416 fingerprint.append("BirthDay(").append(birthDay).append(')');
418 if (birthMonth != null) {
419 fingerprint.append("BirthMonth(").append(birthMonth).append(')');
421 if (birthYear != null) {
422 fingerprint.append("BirthYear(").append(birthYear).append(')');
424 if (avatar != null) {
425 fingerprint.append("Avatar(").append(avatar).append(')');
427 fingerprint.append("ContactInformation(");
428 for (Field field : fields) {
429 fingerprint.append(field.getName()).append('(').append(field.getValue()).append(')');
431 fingerprint.append(")");
432 fingerprint.append(")");
434 return fingerprint.toString();
438 * Container for a profile field.
440 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
444 /** The ID of the field. */
445 private final String id;
447 /** The name of the field. */
450 /** The value of the field. */
451 private String value;
454 * Creates a new field with a random ID.
457 this(UUID.randomUUID().toString());
461 * Creates a new field with the given ID.
464 * The ID of the field
466 private Field(String id) {
467 this.id = checkNotNull(id, "id must not be null");
471 * Returns the ID of this field.
473 * @return The ID of this field
475 public String getId() {
480 * Returns the name of this field.
482 * @return The name of this field
484 public String getName() {
489 * Sets the name of this field. The name must not be {@code null} and
490 * must not match any other fields in this profile but my match the name
494 * The new name of this field
497 public Field setName(String name) {
498 checkNotNull(name, "name must not be null");
499 checkArgument(getFieldByName(name) == null, "name must be unique");
505 * Returns the value of this field.
507 * @return The value of this field
509 public String getValue() {
514 * Sets the value of this field. While {@code null} is allowed, no
515 * guarantees are made that {@code null} values are correctly persisted
516 * across restarts of the plugin!
519 * The new value of this field
522 public Field setValue(String value) {
535 public boolean equals(Object object) {
536 if (!(object instanceof Field)) {
539 Field field = (Field) object;
540 return id.equals(field.id);
547 public int hashCode() {
548 return id.hashCode();