2 * FreenetSone - Profile.java - Copyright © 2010 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 java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
26 import net.pterodactylus.util.validation.Validation;
29 * A profile stores personal information about a {@link Sone}. All information
30 * is optional and can be {@code null}.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class Profile implements Fingerprintable {
36 /** Whether the profile was modified. */
37 private volatile boolean modified;
39 /** The first name. */
40 private volatile String firstName;
42 /** The middle name(s). */
43 private volatile String middleName;
46 private volatile String lastName;
48 /** The day of the birth date. */
49 private volatile Integer birthDay;
51 /** The month of the birth date. */
52 private volatile Integer birthMonth;
54 /** The year of the birth date. */
55 private volatile Integer birthYear;
57 /** Additional fields in the profile. */
58 private final List<String> fields = Collections.synchronizedList(new ArrayList<String>());
60 /** The field values. */
61 private final Map<String, String> fieldValues = Collections.synchronizedMap(new HashMap<String, String>());
64 * Creates a new empty profile.
71 * Creates a copy of a profile.
76 public Profile(Profile profile) {
77 if (profile == null) {
80 this.firstName = profile.firstName;
81 this.middleName = profile.middleName;
82 this.lastName = profile.lastName;
83 this.birthDay = profile.birthDay;
84 this.birthMonth = profile.birthMonth;
85 this.birthYear = profile.birthYear;
86 this.fieldValues.putAll(profile.fieldValues);
94 * Returns whether this profile was modified after creation. To clear the
95 * “is modified” flag you need to create a new profile from this one using
96 * the {@link #Profile(Profile)} constructor.
98 * @return {@code true} if this profile was modified after creation,
99 * {@code false} otherwise
101 public boolean isModified() {
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 modified |= ((firstName != null) && (!firstName.equals(this.firstName))) || (this.firstName != null);
123 this.firstName = firstName;
128 * Returns the middle name(s).
130 * @return The middle name
132 public String getMiddleName() {
137 * Sets the middle name.
140 * The middle name to set
141 * @return This profile (for method chaining)
143 public Profile setMiddleName(String middleName) {
144 modified |= ((middleName != null) && (!middleName.equals(this.middleName))) || (this.middleName != null);
145 this.middleName = middleName;
150 * Returns the last name.
152 * @return The last name
154 public String getLastName() {
159 * Sets the last name.
162 * The last name to set
163 * @return This profile (for method chaining)
165 public Profile setLastName(String lastName) {
166 modified |= ((lastName != null) && (!lastName.equals(this.lastName))) || (this.lastName != null);
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 modified |= ((birthDay != null) && (!birthDay.equals(this.birthDay))) || (this.birthDay != null);
189 this.birthDay = birthDay;
194 * Returns the month of the birth date.
196 * @return The month of the birth date (from 1 to 12)
198 public Integer getBirthMonth() {
203 * Sets the month of the birth date.
206 * The month of the birth date (from 1 to 12)
207 * @return This profile (for method chaining)
209 public Profile setBirthMonth(Integer birthMonth) {
210 modified |= ((birthMonth != null) && (!birthMonth.equals(this.birthMonth))) || (this.birthMonth != null);
211 this.birthMonth = birthMonth;
216 * Returns the year of the birth date.
218 * @return The year of the birth date
220 public Integer getBirthYear() {
225 * Sets the year of the birth date.
228 * The year of the birth date
229 * @return This profile (for method chaining)
231 public Profile setBirthYear(Integer birthYear) {
232 modified |= ((birthYear != null) && (!birthYear.equals(this.birthYear))) || (this.birthYear != null);
233 this.birthYear = birthYear;
238 * Appends a new field to the list of fields.
242 * @throws IllegalArgumentException
243 * if the name is not valid
245 public void addField(String field) throws IllegalArgumentException {
246 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Unique", !fields.contains(field), true).check();
251 * Moves the field with the given index up one position in the field list.
252 * The index of the field to move must be greater than {@code 0} (because
253 * you obviously can not move the first field further up).
256 * The index of the field to move
258 public void moveFieldUp(int fieldIndex) {
259 Validation.begin().isGreater("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
260 String field = fields.remove(fieldIndex);
261 fields.add(fieldIndex - 1, field);
265 * Moves the field with the given name up one position in the field list.
266 * The field must not be the first field (because you obviously can not move
267 * the first field further up).
270 * The name of the field to move
272 public void moveFieldUp(String field) {
273 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
274 moveFieldUp(getFieldIndex(field));
278 * Moves the field with the given index down one position in the field list.
279 * The index of the field to move must be less than the index of the last
280 * field (because you obviously can not move the last field further down).
283 * The index of the field to move
285 public void moveFieldDown(int fieldIndex) {
286 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size() - 1).check();
287 String field = fields.remove(fieldIndex);
288 fields.add(fieldIndex + 1, field);
292 * Moves the field with the given name down one position in the field list.
293 * The field must not be the last field (because you obviously can not move
294 * the last field further down).
297 * The name of the field to move
299 public void moveFieldDown(String field) {
300 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
301 moveFieldDown(getFieldIndex(field));
305 * Removes the field at the given index.
308 * The index of the field to remove
310 public void removeField(int fieldIndex) {
311 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
312 String field = fields.remove(fieldIndex);
313 fieldValues.remove(field);
317 * Removes the field with the given name.
320 * The name of the field
322 public void removeField(String field) {
323 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
324 removeField(getFieldIndex(field));
328 * Returns the value of the field with the given name.
331 * The name of the field
332 * @return The value of the field, or {@code null} if there is no such field
334 public String getField(String field) {
335 return fieldValues.get(field);
339 * Sets the value of the field with the given name.
342 * The name of the field
344 * The value of the field
346 public void setField(String field, String value) {
347 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
348 fieldValues.put(field, value);
352 * Returns a list of all fields stored in this profile.
354 * @return The fields of this profile
356 public List<String> getFields() {
357 return Collections.unmodifiableList(fields);
365 * Returns the index of the field with the given name.
368 * The name of the field
369 * @return The index of the field, or {@code -1} if there is no field with
372 private int getFieldIndex(String field) {
373 return fields.indexOf(field);
377 // INTERFACE Fingerprintable
384 public String getFingerprint() {
385 StringBuilder fingerprint = new StringBuilder();
386 fingerprint.append("Profile(");
387 if (firstName != null) {
388 fingerprint.append("FirstName(").append(firstName).append(')');
390 if (middleName != null) {
391 fingerprint.append("MiddleName(").append(middleName).append(')');
393 if (lastName != null) {
394 fingerprint.append("LastName(").append(lastName).append(')');
396 if (birthDay != null) {
397 fingerprint.append("BirthDay(").append(birthDay).append(')');
399 if (birthMonth != null) {
400 fingerprint.append("BirthMonth(").append(birthMonth).append(')');
402 if (birthYear != null) {
403 fingerprint.append("BirthYear(").append(birthYear).append(')');
405 fingerprint.append("ContactInformation(");
406 for (String field : fields) {
407 fingerprint.append(field).append('(').append(fieldValues.get(field)).append(')');
409 fingerprint.append(")");
410 fingerprint.append(")");
412 return fingerprint.toString();