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.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Optional.of;
23 import static com.google.common.base.Preconditions.checkArgument;
24 import static com.google.common.base.Preconditions.checkNotNull;
25 import static java.lang.Math.max;
26 import static java.lang.Math.min;
27 import static java.util.UUID.randomUUID;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
33 import com.google.common.base.Optional;
34 import com.google.common.hash.Hasher;
35 import com.google.common.hash.Hashing;
38 * A profile stores personal information about a {@link Sone}. All information
39 * is optional and can be {@code null}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class Profile implements Fingerprintable {
45 /** The Sone this profile belongs to. */
46 private final Sone sone;
48 private volatile Name name = new Name();
49 private volatile BirthDate birthDate = new BirthDate();
51 /** The ID of the avatar image. */
52 private volatile String avatar;
54 /** Additional fields in the profile. */
55 private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
58 * Creates a new empty profile.
61 * The Sone this profile belongs to
63 public Profile(Sone sone) {
68 * Creates a copy of a profile.
73 public Profile(Profile profile) {
74 this.sone = profile.sone;
75 this.name = profile.name;
76 this.birthDate = profile.birthDate;
77 this.avatar = profile.avatar;
78 this.fields.addAll(profile.fields);
86 * Returns the Sone this profile belongs to.
88 * @return The Sone this profile belongs to
90 public Sone getSone() {
94 public String getFirstName() {
95 return name.getFirst().orNull();
98 public String getMiddleName() {
99 return name.getMiddle().orNull();
102 public String getLastName() {
103 return name.getLast().orNull();
106 public Integer getBirthDay() {
107 return birthDate.getDay().orNull();
110 public Integer getBirthMonth() {
111 return birthDate.getMonth().orNull();
114 public Integer getBirthYear() {
115 return birthDate.getYear().orNull();
118 public String getAvatar() {
122 public Profile setAvatar(Optional<String> avatarId) {
123 this.avatar = avatarId.orNull();
127 public List<Field> getFields() {
128 return new ArrayList<Field>(fields);
131 public boolean hasField(Field field) {
132 return fields.contains(field);
135 public Optional<Field> getFieldById(String fieldId) {
136 checkNotNull(fieldId, "fieldId must not be null");
137 for (Field field : fields) {
138 if (field.getId().equals(fieldId)) {
145 public Optional<Field> getFieldByName(String fieldName) {
146 for (Field field : fields) {
147 if (field.getName().equals(fieldName)) {
154 public Field addField(String fieldName) throws IllegalArgumentException {
155 checkNotNull(fieldName, "fieldName must not be null");
156 checkArgument(fieldName.length() > 0, "fieldName must not be empty");
157 checkArgument(!getFieldByName(fieldName).isPresent(), "fieldName must be unique");
158 @SuppressWarnings("synthetic-access")
159 Field field = new Field(fieldName);
164 public void renameField(Field field, String newName) {
165 int indexOfField = getFieldIndex(field);
166 if (indexOfField == -1) {
169 fields.set(indexOfField, new Field(field.getId(), newName, field.getValue()));
172 public void setField(Field field, String newValue) {
173 int indexOfField = getFieldIndex(field);
174 if (indexOfField == -1) {
177 fields.set(indexOfField, new Field(field.getId(), field.getName(), newValue));
180 public void moveFieldUp(Field field) {
181 checkNotNull(field, "field must not be null");
182 checkArgument(hasField(field), "field must belong to this profile");
183 int fieldIndex = getFieldIndex(field);
184 fields.remove(field);
185 fields.add(max(fieldIndex - 1, 0), field);
188 public void moveFieldDown(Field field) {
189 checkNotNull(field, "field must not be null");
190 checkArgument(hasField(field), "field must belong to this profile");
191 int fieldIndex = getFieldIndex(field);
192 fields.remove(field);
193 fields.add(min(fieldIndex + 1, fields.size()), field);
196 public void removeField(Field field) {
197 checkNotNull(field, "field must not be null");
198 fields.remove(field);
201 public Modifier modify() {
202 return new Modifier() {
203 private Optional<String> firstName = name.getFirst();
204 private Optional<String> middleName = name.getMiddle();
205 private Optional<String> lastName = name.getLast();
206 private Optional<Integer> birthYear = birthDate.getYear();
207 private Optional<Integer> birthMonth = birthDate.getMonth();
208 private Optional<Integer> birthDay = birthDate.getDay();
211 public Modifier setFirstName(String firstName) {
212 this.firstName = fromNullable(firstName);
217 public Modifier setMiddleName(String middleName) {
218 this.middleName = fromNullable(middleName);
223 public Modifier setLastName(String lastName) {
224 this.lastName = fromNullable(lastName);
229 public Modifier setBirthYear(Integer birthYear) {
230 this.birthYear = fromNullable(birthYear);
235 public Modifier setBirthMonth(Integer birthMonth) {
236 this.birthMonth = fromNullable(birthMonth);
241 public Modifier setBirthDay(Integer birthDay) {
242 this.birthDay = fromNullable(birthDay);
247 public Profile update() {
248 Profile.this.name = new Name(firstName, middleName, lastName);
249 Profile.this.birthDate = new BirthDate(birthYear, birthMonth, birthDay);
255 public interface Modifier {
257 Modifier setFirstName(String firstName);
258 Modifier setMiddleName(String middleName);
259 Modifier setLastName(String lastName);
260 Modifier setBirthYear(Integer birthYear);
261 Modifier setBirthMonth(Integer birthMonth);
262 Modifier setBirthDay(Integer birthDay);
272 * Returns the index of the field with the given name.
275 * The name of the field
276 * @return The index of the field, or {@code -1} if there is no field with
279 private int getFieldIndex(Field field) {
280 return fields.indexOf(field);
284 // INTERFACE Fingerprintable
288 public String getFingerprint() {
289 Hasher hash = Hashing.sha256().newHasher();
290 hash.putString("Profile(");
291 hash.putString(name.getFingerprint());
292 hash.putString(birthDate.getFingerprint());
293 if (avatar != null) {
294 hash.putString("Avatar(").putString(avatar).putString(")");
296 hash.putString("ContactInformation(");
297 for (Field field : fields) {
298 if (field.getValue() != null) {
299 hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
305 return hash.hash().toString();
309 * Container for a profile field.
311 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
313 public static class Field {
315 private final String id;
316 private final String name;
317 private final String value;
319 public Field(String name) {
323 public Field(String name, String value) {
324 this(randomUUID().toString(), name, value);
327 public Field(String id, String name, String value) {
328 this.id = checkNotNull(id, "id must not be null");
333 public String getId() {
337 public String getName() {
341 public String getValue() {
346 public boolean equals(Object object) {
347 if (!(object instanceof Field)) {
350 Field field = (Field) object;
351 return id.equals(field.id);
355 public int hashCode() {
356 return id.hashCode();
361 public static class Name implements Fingerprintable {
363 private final Optional<String> first;
364 private final Optional<String> middle;
365 private final Optional<String> last;
368 this(Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent());
371 public Name(Optional<String> first, Optional<String> middle, Optional<String> last) {
373 this.middle = middle;
377 public Optional<String> getFirst() {
381 public Optional<String> getMiddle() {
385 public Optional<String> getLast() {
390 public String getFingerprint() {
391 Hasher hash = Hashing.sha256().newHasher();
392 hash.putString("Name(");
393 if (first.isPresent()) {
394 hash.putString("First(").putString(first.get()).putString(")");
396 if (middle.isPresent()) {
397 hash.putString("Middle(").putString(middle.get()).putString(")");
399 if (last.isPresent()) {
400 hash.putString("Last(").putString(last.get()).putString(")");
403 return hash.hash().toString();
408 public static class BirthDate implements Fingerprintable {
410 private final Optional<Integer> year;
411 private final Optional<Integer> month;
412 private final Optional<Integer> day;
415 this(Optional.<Integer>absent(), Optional.<Integer>absent(), Optional.<Integer>absent());
418 public BirthDate(Optional<Integer> year, Optional<Integer> month, Optional<Integer> day) {
424 public Optional<Integer> getYear() {
428 public Optional<Integer> getMonth() {
432 public Optional<Integer> getDay() {
437 public String getFingerprint() {
438 Hasher hash = Hashing.sha256().newHasher();
439 hash.putString("Birthdate(");
440 if (year.isPresent()) {
441 hash.putString("Year(").putInt(year.get()).putString(")");
443 if (month.isPresent()) {
444 hash.putString("Month(").putInt(month.get()).putString(")");
446 if (day.isPresent()) {
447 hash.putString("Day(").putInt(day.get()).putString(")");
450 return hash.hash().toString();