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.LinkedHashMap;
24 import java.util.List;
27 import net.pterodactylus.util.validation.Validation;
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 first name. */
38 private volatile String firstName;
40 /** The middle name(s). */
41 private volatile String middleName;
44 private volatile String lastName;
46 /** The day of the birth date. */
47 private volatile Integer birthDay;
49 /** The month of the birth date. */
50 private volatile Integer birthMonth;
52 /** The year of the birth date. */
53 private volatile Integer birthYear;
55 /** Additional fields in the profile. */
56 private final List<String> fields = Collections.synchronizedList(new ArrayList<String>());
58 /** The field values. */
59 private final Map<String, String> fieldValues = Collections.synchronizedMap(new HashMap<String, String>());
62 * Creates a new empty profile.
69 * Creates a copy of a profile.
74 public Profile(Profile profile) {
75 if (profile == null) {
78 this.firstName = profile.firstName;
79 this.middleName = profile.middleName;
80 this.lastName = profile.lastName;
81 this.birthDay = profile.birthDay;
82 this.birthMonth = profile.birthMonth;
83 this.birthYear = profile.birthYear;
84 this.fields.addAll(profile.fields);
85 this.fieldValues.putAll(profile.fieldValues);
93 * Returns the first name.
95 * @return The first name
97 public String getFirstName() {
102 * Sets the first name.
105 * The first name to set
106 * @return This profile (for method chaining)
108 public Profile setFirstName(String firstName) {
109 this.firstName = firstName;
114 * Returns the middle name(s).
116 * @return The middle name
118 public String getMiddleName() {
123 * Sets the middle name.
126 * The middle name to set
127 * @return This profile (for method chaining)
129 public Profile setMiddleName(String middleName) {
130 this.middleName = middleName;
135 * Returns the last name.
137 * @return The last name
139 public String getLastName() {
144 * Sets the last name.
147 * The last name to set
148 * @return This profile (for method chaining)
150 public Profile setLastName(String lastName) {
151 this.lastName = lastName;
156 * Returns the day of the birth date.
158 * @return The day of the birth date (from 1 to 31)
160 public Integer getBirthDay() {
165 * Sets the day of the birth date.
168 * The day of the birth date (from 1 to 31)
169 * @return This profile (for method chaining)
171 public Profile setBirthDay(Integer birthDay) {
172 this.birthDay = birthDay;
177 * Returns the month of the birth date.
179 * @return The month of the birth date (from 1 to 12)
181 public Integer getBirthMonth() {
186 * Sets the month of the birth date.
189 * The month of the birth date (from 1 to 12)
190 * @return This profile (for method chaining)
192 public Profile setBirthMonth(Integer birthMonth) {
193 this.birthMonth = birthMonth;
198 * Returns the year of the birth date.
200 * @return The year of the birth date
202 public Integer getBirthYear() {
207 * Sets the year of the birth date.
210 * The year of the birth date
211 * @return This profile (for method chaining)
213 public Profile setBirthYear(Integer birthYear) {
214 this.birthYear = birthYear;
219 * Appends a new field to the list of fields.
223 * @throws IllegalArgumentException
224 * if the name is not valid
226 public void addField(String field) throws IllegalArgumentException {
227 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Unique", !fields.contains(field), true).check();
232 * Moves the field with the given index up one position in the field list.
233 * The index of the field to move must be greater than {@code 0} (because
234 * you obviously can not move the first field further up).
237 * The index of the field to move
239 public void moveFieldUp(int fieldIndex) {
240 Validation.begin().isGreater("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
241 String field = fields.remove(fieldIndex);
242 fields.add(fieldIndex - 1, field);
246 * Moves the field with the given name up one position in the field list.
247 * The field must not be the first field (because you obviously can not move
248 * the first field further up).
251 * The name of the field to move
253 public void moveFieldUp(String field) {
254 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
255 moveFieldUp(getFieldIndex(field));
259 * Moves the field with the given index down one position in the field list.
260 * The index of the field to move must be less than the index of the last
261 * field (because you obviously can not move the last field further down).
264 * The index of the field to move
266 public void moveFieldDown(int fieldIndex) {
267 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size() - 1).check();
268 String field = fields.remove(fieldIndex);
269 fields.add(fieldIndex + 1, field);
273 * Moves the field with the given name down one position in the field list.
274 * The field must not be the last field (because you obviously can not move
275 * the last field further down).
278 * The name of the field to move
280 public void moveFieldDown(String field) {
281 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
282 moveFieldDown(getFieldIndex(field));
286 * Removes the field at the given index.
289 * The index of the field to remove
291 public void removeField(int fieldIndex) {
292 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
293 String field = fields.remove(fieldIndex);
294 fieldValues.remove(field);
298 * Removes the field with the given name.
301 * The name of the field
303 public void removeField(String field) {
304 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
305 removeField(getFieldIndex(field));
309 * Returns the value of the field with the given name.
312 * The name of the field
313 * @return The value of the field, or {@code null} if there is no such field
315 public String getField(String field) {
316 return fieldValues.get(field);
320 * Sets the value of the field with the given name.
323 * The index of the field
325 * The value of the field
327 public void setField(int fieldIndex, String value) {
328 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
329 setField(fields.get(fieldIndex), value);
333 * Sets the value of the field with the given name.
336 * The name of the field
338 * The value of the field
340 public void setField(String field, String value) {
341 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
342 fieldValues.put(field, value);
346 * Returns a list of all fields stored in this profile.
348 * @return The fields of this profile
350 public List<String> getFieldNames() {
351 return Collections.unmodifiableList(fields);
358 * The index of the field to rename
360 * The new name of the field
362 public void setFieldName(int fieldIndex, String fieldName) {
363 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).isNotNull("Field Name", fieldName).isEqual("New Field Name Unique", !fields.contains(fieldName) || (getFieldIndex(fieldName) == fieldIndex), true).check();
364 String value = fieldValues.remove(fields.get(fieldIndex));
365 fields.set(fieldIndex, fieldName);
366 fieldValues.put(fieldName, value);
370 * Returns all field names and their values, ordered the same way
371 * {@link #getFieldNames()} returns the names of the fields.
373 * @return All field names and values
375 public Map<String, String> getFields() {
376 Map<String, String> fields = new LinkedHashMap<String, String>();
377 for (String field : getFieldNames()) {
378 fields.put(field, getField(field));
388 * Returns the index of the field with the given name.
391 * The name of the field
392 * @return The index of the field, or {@code -1} if there is no field with
395 private int getFieldIndex(String field) {
396 return fields.indexOf(field);
400 // INTERFACE Fingerprintable
407 public String getFingerprint() {
408 StringBuilder fingerprint = new StringBuilder();
409 fingerprint.append("Profile(");
410 if (firstName != null) {
411 fingerprint.append("FirstName(").append(firstName).append(')');
413 if (middleName != null) {
414 fingerprint.append("MiddleName(").append(middleName).append(')');
416 if (lastName != null) {
417 fingerprint.append("LastName(").append(lastName).append(')');
419 if (birthDay != null) {
420 fingerprint.append("BirthDay(").append(birthDay).append(')');
422 if (birthMonth != null) {
423 fingerprint.append("BirthMonth(").append(birthMonth).append(')');
425 if (birthYear != null) {
426 fingerprint.append("BirthYear(").append(birthYear).append(')');
428 fingerprint.append("ContactInformation(");
429 for (String field : fields) {
430 fingerprint.append(field).append('(').append(fieldValues.get(field)).append(')');
432 fingerprint.append(")");
433 fingerprint.append(")");
435 return fingerprint.toString();