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;
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 checkArgument(fieldName.length() > 0, "fieldName must not be empty");
329 checkState(getFieldByName(fieldName) == null, "fieldName must be unique");
330 @SuppressWarnings("synthetic-access")
331 Field field = new Field().setName(fieldName);
337 * Moves the given field up one position in the field list. The index of the
338 * field to move must be greater than {@code 0} (because you obviously can
339 * not move the first field further up).
342 * The field to move up
344 public void moveFieldUp(Field field) {
345 checkNotNull(field, "field must not be null");
346 checkArgument(hasField(field), "field must belong to this profile");
347 checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
348 int fieldIndex = getFieldIndex(field);
349 fields.remove(field);
350 fields.add(fieldIndex - 1, field);
354 * Moves the given field down one position in the field list. The index of
355 * the field to move must be less than the index of the last field (because
356 * you obviously can not move the last field further down).
359 * The field to move down
361 public void moveFieldDown(Field field) {
362 checkNotNull(field, "field must not be null");
363 checkArgument(hasField(field), "field must belong to this profile");
364 checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
365 int fieldIndex = getFieldIndex(field);
366 fields.remove(field);
367 fields.add(fieldIndex + 1, field);
371 * Removes the given field.
374 * The field to remove
376 public void removeField(Field field) {
377 checkNotNull(field, "field must not be null");
378 checkArgument(hasField(field), "field must belong to this profile");
379 fields.remove(field);
387 * Returns the index of the field with the given name.
390 * The name of the field
391 * @return The index of the field, or {@code -1} if there is no field with
394 private int getFieldIndex(Field field) {
395 return fields.indexOf(field);
399 // INTERFACE Fingerprintable
406 public String getFingerprint() {
407 Hasher hash = Hashing.sha256().newHasher();
408 hash.putString("Profile(");
409 if (firstName != null) {
410 hash.putString("FirstName(").putString(firstName).putString(")");
412 if (middleName != null) {
413 hash.putString("MiddleName(").putString(middleName).putString(")");
415 if (lastName != null) {
416 hash.putString("LastName(").putString(lastName).putString(")");
418 if (birthDay != null) {
419 hash.putString("BirthDay(").putInt(birthDay).putString(")");
421 if (birthMonth != null) {
422 hash.putString("BirthMonth(").putInt(birthMonth).putString(")");
424 if (birthYear != null) {
425 hash.putString("BirthYear(").putInt(birthYear).putString(")");
427 if (avatar != null) {
428 hash.putString("Avatar(").putString(avatar).putString(")");
430 hash.putString("ContactInformation(");
431 for (Field field : fields) {
432 hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
437 return hash.hash().toString();
441 * Container for a profile field.
443 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
447 /** The ID of the field. */
448 private final String id;
450 /** The name of the field. */
453 /** The value of the field. */
454 private String value;
457 * Creates a new field with a random ID.
460 this(UUID.randomUUID().toString());
464 * Creates a new field with the given ID.
467 * The ID of the field
469 private Field(String id) {
470 this.id = checkNotNull(id, "id must not be null");
474 * Returns the ID of this field.
476 * @return The ID of this field
478 public String getId() {
483 * Returns the name of this field.
485 * @return The name of this field
487 public String getName() {
492 * Sets the name of this field. The name must not be {@code null} and
493 * must not match any other fields in this profile but my match the name
497 * The new name of this field
500 public Field setName(String name) {
501 checkNotNull(name, "name must not be null");
502 checkArgument(getFieldByName(name) == null, "name must be unique");
508 * Returns the value of this field.
510 * @return The value of this field
512 public String getValue() {
517 * Sets the value of this field. While {@code null} is allowed, no
518 * guarantees are made that {@code null} values are correctly persisted
519 * across restarts of the plugin!
522 * The new value of this field
525 public Field setValue(String value) {
538 public boolean equals(Object object) {
539 if (!(object instanceof Field)) {
542 Field field = (Field) object;
543 return id.equals(field.id);
550 public int hashCode() {
551 return id.hashCode();