X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FProfile.java;h=64aeae4ccd212e6c05d54915632590662e352cd5;hb=3abc078471d7e7516599eda01de011a23c328d63;hp=2d2f6d40062546419e16bbb2b4e95123faa2cfd8;hpb=22a4aa8c03feedf20036c61635fb9071ac15707c;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/Profile.java b/src/main/java/net/pterodactylus/sone/data/Profile.java index 2d2f6d4..64aeae4 100644 --- a/src/main/java/net/pterodactylus/sone/data/Profile.java +++ b/src/main/java/net/pterodactylus/sone/data/Profile.java @@ -17,6 +17,15 @@ package net.pterodactylus.sone.data; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import net.pterodactylus.util.validation.Validation; + /** * A profile stores personal information about a {@link Sone}. All information * is optional and can be {@code null}. @@ -25,9 +34,6 @@ package net.pterodactylus.sone.data; */ public class Profile implements Fingerprintable { - /** Whether the profile was modified. */ - private volatile boolean modified; - /** The first name. */ private volatile String firstName; @@ -46,6 +52,12 @@ public class Profile implements Fingerprintable { /** The year of the birth date. */ private volatile Integer birthYear; + /** Additional fields in the profile. */ + private final List fields = Collections.synchronizedList(new ArrayList()); + + /** The field values. */ + private final Map fieldValues = Collections.synchronizedMap(new HashMap()); + /** * Creates a new empty profile. */ @@ -69,6 +81,7 @@ public class Profile implements Fingerprintable { this.birthDay = profile.birthDay; this.birthMonth = profile.birthMonth; this.birthYear = profile.birthYear; + this.fieldValues.putAll(profile.fieldValues); } // @@ -76,18 +89,6 @@ public class Profile implements Fingerprintable { // /** - * Returns whether this profile was modified after creation. To clear the - * “is modified” flag you need to create a new profile from this one using - * the {@link #Profile(Profile)} constructor. - * - * @return {@code true} if this profile was modified after creation, - * {@code false} otherwise - */ - public boolean isModified() { - return modified; - } - - /** * Returns the first name. * * @return The first name @@ -104,7 +105,6 @@ public class Profile implements Fingerprintable { * @return This profile (for method chaining) */ public Profile setFirstName(String firstName) { - modified |= ((firstName != null) && (!firstName.equals(this.firstName))) || (this.firstName != null); this.firstName = firstName; return this; } @@ -126,7 +126,6 @@ public class Profile implements Fingerprintable { * @return This profile (for method chaining) */ public Profile setMiddleName(String middleName) { - modified |= ((middleName != null) && (!middleName.equals(this.middleName))) || (this.middleName != null); this.middleName = middleName; return this; } @@ -148,7 +147,6 @@ public class Profile implements Fingerprintable { * @return This profile (for method chaining) */ public Profile setLastName(String lastName) { - modified |= ((lastName != null) && (!lastName.equals(this.lastName))) || (this.lastName != null); this.lastName = lastName; return this; } @@ -170,7 +168,6 @@ public class Profile implements Fingerprintable { * @return This profile (for method chaining) */ public Profile setBirthDay(Integer birthDay) { - modified |= ((birthDay != null) && (!birthDay.equals(this.birthDay))) || (this.birthDay != null); this.birthDay = birthDay; return this; } @@ -192,7 +189,6 @@ public class Profile implements Fingerprintable { * @return This profile (for method chaining) */ public Profile setBirthMonth(Integer birthMonth) { - modified |= ((birthMonth != null) && (!birthMonth.equals(this.birthMonth))) || (this.birthMonth != null); this.birthMonth = birthMonth; return this; } @@ -214,11 +210,163 @@ public class Profile implements Fingerprintable { * @return This profile (for method chaining) */ public Profile setBirthYear(Integer birthYear) { - modified |= ((birthYear != null) && (!birthYear.equals(this.birthYear))) || (this.birthYear != null); this.birthYear = birthYear; return this; } + /** + * Appends a new field to the list of fields. + * + * @param field + * The field to add + * @throws IllegalArgumentException + * if the name is not valid + */ + public void addField(String field) throws IllegalArgumentException { + Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Unique", !fields.contains(field), true).check(); + fields.add(field); + } + + /** + * Moves the field with the given index up one position in the field list. + * The index of the field to move must be greater than {@code 0} (because + * you obviously can not move the first field further up). + * + * @param fieldIndex + * The index of the field to move + */ + public void moveFieldUp(int fieldIndex) { + Validation.begin().isGreater("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check(); + String field = fields.remove(fieldIndex); + fields.add(fieldIndex - 1, field); + } + + /** + * Moves the field with the given name up one position in the field list. + * The field must not be the first field (because you obviously can not move + * the first field further up). + * + * @param field + * The name of the field to move + */ + public void moveFieldUp(String field) { + Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check(); + moveFieldUp(getFieldIndex(field)); + } + + /** + * Moves the field with the given index down one position in the field list. + * The index of the field to move must be less than the index of the last + * field (because you obviously can not move the last field further down). + * + * @param fieldIndex + * The index of the field to move + */ + public void moveFieldDown(int fieldIndex) { + Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size() - 1).check(); + String field = fields.remove(fieldIndex); + fields.add(fieldIndex + 1, field); + } + + /** + * Moves the field with the given name down one position in the field list. + * The field must not be the last field (because you obviously can not move + * the last field further down). + * + * @param field + * The name of the field to move + */ + public void moveFieldDown(String field) { + Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check(); + moveFieldDown(getFieldIndex(field)); + } + + /** + * Removes the field at the given index. + * + * @param fieldIndex + * The index of the field to remove + */ + public void removeField(int fieldIndex) { + Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check(); + String field = fields.remove(fieldIndex); + fieldValues.remove(field); + } + + /** + * Removes the field with the given name. + * + * @param field + * The name of the field + */ + public void removeField(String field) { + Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check(); + removeField(getFieldIndex(field)); + } + + /** + * Returns the value of the field with the given name. + * + * @param field + * The name of the field + * @return The value of the field, or {@code null} if there is no such field + */ + public String getField(String field) { + return fieldValues.get(field); + } + + /** + * Sets the value of the field with the given name. + * + * @param field + * The name of the field + * @param value + * The value of the field + */ + public void setField(String field, String value) { + Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check(); + fieldValues.put(field, value); + } + + /** + * Returns a list of all fields stored in this profile. + * + * @return The fields of this profile + */ + public List getFieldNames() { + return Collections.unmodifiableList(fields); + } + + /** + * Returns all field names and their values, ordered the same way + * {@link #getFieldNames()} returns the names of the fields. + * + * @return All field names and values + */ + public Map getFields() { + Map fields = new LinkedHashMap(); + for (String field : getFieldNames()) { + fields.put(field, getField(field)); + } + return fields; + } + + // + // PRIVATE METHODS + // + + /** + * Returns the index of the field with the given name. + * + * @param field + * The name of the field + * @return The index of the field, or {@code -1} if there is no field with + * the given name + */ + private int getFieldIndex(String field) { + return fields.indexOf(field); + } + // // INTERFACE Fingerprintable // @@ -248,6 +396,11 @@ public class Profile implements Fingerprintable { if (birthYear != null) { fingerprint.append("BirthYear(").append(birthYear).append(')'); } + fingerprint.append("ContactInformation("); + for (String field : fields) { + fingerprint.append(field).append('(').append(fieldValues.get(field)).append(')'); + } + fingerprint.append(")"); fingerprint.append(")"); return fingerprint.toString();