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.fieldValues.putAll(profile.fieldValues);
92 * Returns the first name.
94 * @return The first name
96 public String getFirstName() {
101 * Sets the first name.
104 * The first name to set
105 * @return This profile (for method chaining)
107 public Profile setFirstName(String firstName) {
108 this.firstName = firstName;
113 * Returns the middle name(s).
115 * @return The middle name
117 public String getMiddleName() {
122 * Sets the middle name.
125 * The middle name to set
126 * @return This profile (for method chaining)
128 public Profile setMiddleName(String middleName) {
129 this.middleName = middleName;
134 * Returns the last name.
136 * @return The last name
138 public String getLastName() {
143 * Sets the last name.
146 * The last name to set
147 * @return This profile (for method chaining)
149 public Profile setLastName(String lastName) {
150 this.lastName = lastName;
155 * Returns the day of the birth date.
157 * @return The day of the birth date (from 1 to 31)
159 public Integer getBirthDay() {
164 * Sets the day of the birth date.
167 * The day of the birth date (from 1 to 31)
168 * @return This profile (for method chaining)
170 public Profile setBirthDay(Integer birthDay) {
171 this.birthDay = birthDay;
176 * Returns the month of the birth date.
178 * @return The month of the birth date (from 1 to 12)
180 public Integer getBirthMonth() {
185 * Sets the month of the birth date.
188 * The month of the birth date (from 1 to 12)
189 * @return This profile (for method chaining)
191 public Profile setBirthMonth(Integer birthMonth) {
192 this.birthMonth = birthMonth;
197 * Returns the year of the birth date.
199 * @return The year of the birth date
201 public Integer getBirthYear() {
206 * Sets the year of the birth date.
209 * The year of the birth date
210 * @return This profile (for method chaining)
212 public Profile setBirthYear(Integer birthYear) {
213 this.birthYear = birthYear;
218 * Appends a new field to the list of fields.
222 * @throws IllegalArgumentException
223 * if the name is not valid
225 public void addField(String field) throws IllegalArgumentException {
226 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Unique", !fields.contains(field), true).check();
231 * Moves the field with the given index up one position in the field list.
232 * The index of the field to move must be greater than {@code 0} (because
233 * you obviously can not move the first field further up).
236 * The index of the field to move
238 public void moveFieldUp(int fieldIndex) {
239 Validation.begin().isGreater("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
240 String field = fields.remove(fieldIndex);
241 fields.add(fieldIndex - 1, field);
245 * Moves the field with the given name up one position in the field list.
246 * The field must not be the first field (because you obviously can not move
247 * the first field further up).
250 * The name of the field to move
252 public void moveFieldUp(String field) {
253 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
254 moveFieldUp(getFieldIndex(field));
258 * Moves the field with the given index down one position in the field list.
259 * The index of the field to move must be less than the index of the last
260 * field (because you obviously can not move the last field further down).
263 * The index of the field to move
265 public void moveFieldDown(int fieldIndex) {
266 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size() - 1).check();
267 String field = fields.remove(fieldIndex);
268 fields.add(fieldIndex + 1, field);
272 * Moves the field with the given name down one position in the field list.
273 * The field must not be the last field (because you obviously can not move
274 * the last field further down).
277 * The name of the field to move
279 public void moveFieldDown(String field) {
280 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
281 moveFieldDown(getFieldIndex(field));
285 * Removes the field at the given index.
288 * The index of the field to remove
290 public void removeField(int fieldIndex) {
291 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
292 String field = fields.remove(fieldIndex);
293 fieldValues.remove(field);
297 * Removes the field with the given name.
300 * The name of the field
302 public void removeField(String field) {
303 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
304 removeField(getFieldIndex(field));
308 * Returns the value of the field with the given name.
311 * The name of the field
312 * @return The value of the field, or {@code null} if there is no such field
314 public String getField(String field) {
315 return fieldValues.get(field);
319 * Sets the value of the field with the given name.
322 * The index of the field
324 * The value of the field
326 public void setField(int fieldIndex, String value) {
327 Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
328 setField(fields.get(fieldIndex), value);
332 * Sets the value of the field with the given name.
335 * The name of the field
337 * The value of the field
339 public void setField(String field, String value) {
340 Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
341 fieldValues.put(field, value);
345 * Returns a list of all fields stored in this profile.
347 * @return The fields of this profile
349 public List<String> getFieldNames() {
350 return Collections.unmodifiableList(fields);
354 * Returns all field names and their values, ordered the same way
355 * {@link #getFieldNames()} returns the names of the fields.
357 * @return All field names and values
359 public Map<String, String> getFields() {
360 Map<String, String> fields = new LinkedHashMap<String, String>();
361 for (String field : getFieldNames()) {
362 fields.put(field, getField(field));
372 * Returns the index of the field with the given name.
375 * The name of the field
376 * @return The index of the field, or {@code -1} if there is no field with
379 private int getFieldIndex(String field) {
380 return fields.indexOf(field);
384 // INTERFACE Fingerprintable
391 public String getFingerprint() {
392 StringBuilder fingerprint = new StringBuilder();
393 fingerprint.append("Profile(");
394 if (firstName != null) {
395 fingerprint.append("FirstName(").append(firstName).append(')');
397 if (middleName != null) {
398 fingerprint.append("MiddleName(").append(middleName).append(')');
400 if (lastName != null) {
401 fingerprint.append("LastName(").append(lastName).append(')');
403 if (birthDay != null) {
404 fingerprint.append("BirthDay(").append(birthDay).append(')');
406 if (birthMonth != null) {
407 fingerprint.append("BirthMonth(").append(birthMonth).append(')');
409 if (birthYear != null) {
410 fingerprint.append("BirthYear(").append(birthYear).append(')');
412 fingerprint.append("ContactInformation(");
413 for (String field : fields) {
414 fingerprint.append(field).append('(').append(fieldValues.get(field)).append(')');
416 fingerprint.append(")");
417 fingerprint.append(")");
419 return fingerprint.toString();