Add method to set all albums of a Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
index 7fd5fa6..4bd60cc 100644 (file)
@@ -30,6 +30,7 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.validation.Validation;
 import freenet.keys.FreenetURI;
 
 /**
@@ -40,7 +41,7 @@ import freenet.keys.FreenetURI;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class Sone {
+public class Sone implements Fingerprintable, Comparable<Sone> {
 
        /** comparator that sorts Sones by their nice name. */
        public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
@@ -79,7 +80,10 @@ public class Sone {
        private volatile long time;
 
        /** The profile of this Sone. */
-       private volatile Profile profile;
+       private volatile Profile profile = new Profile();
+
+       /** The client used by the Sone. */
+       private volatile Client client;
 
        /** All friend Sones. */
        private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
@@ -96,6 +100,9 @@ public class Sone {
        /** The IDs of all liked replies. */
        private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
 
+       /** The albums of this Sone. */
+       private final List<Album> albums = Collections.synchronizedList(new ArrayList<Album>());
+
        /**
         * Creates a new Sone.
         *
@@ -173,7 +180,7 @@ public class Sone {
         */
        public Sone setRequestUri(FreenetURI requestUri) {
                if (this.requestUri == null) {
-                       this.requestUri = requestUri.setDocName("Sone").setMetaString(new String[0]);
+                       this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
                        return this;
                }
                if (!this.requestUri.equalsKeypair(requestUri)) {
@@ -201,7 +208,7 @@ public class Sone {
         */
        public Sone setInsertUri(FreenetURI insertUri) {
                if (this.insertUri == null) {
-                       this.insertUri = insertUri.setDocName("Sone").setMetaString(new String[0]);
+                       this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
                        return this;
                }
                if (!this.insertUri.equalsKeypair(insertUri)) {
@@ -281,6 +288,27 @@ public class Sone {
        }
 
        /**
+        * Returns the client used by this Sone.
+        *
+        * @return The client used by this Sone, or {@code null}
+        */
+       public Client getClient() {
+               return client;
+       }
+
+       /**
+        * Sets the client used by this Sone.
+        *
+        * @param client
+        *            The client used by this Sone, or {@code null}
+        * @return This Sone (for method chaining)
+        */
+       public Sone setClient(Client client) {
+               this.client = client;
+               return this;
+       }
+
+       /**
         * Returns all friend Sones of this Sone.
         *
         * @return The friend Sones of this Sone
@@ -557,35 +585,61 @@ public class Sone {
        }
 
        /**
-        * Returns a fingerprint of this Sone. The fingerprint only depends on data
-        * that is actually stored when a Sone is inserted. The fingerprint can be
-        * used to detect changes in Sone data and can also be used to detect if
-        * previous changes are reverted.
+        * Returns the albums of this Sone.
+        *
+        * @return The albums of this Sone
+        */
+       public List<Album> getAlbums() {
+               return Collections.unmodifiableList(albums);
+       }
+
+       /**
+        * Adds an album to this Sone.
+        *
+        * @param album
+        *            The album to add
+        */
+       public synchronized void addAlbum(Album album) {
+               Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
+               albums.add(album);
+       }
+
+       /**
+        * Sets the albums of this Sone.
+        *
+        * @param albums
+        *            The albums of this Sone
+        */
+       public synchronized void setAlbums(Collection<? extends Album> albums) {
+               Validation.begin().isNotNull("Albums", albums).check();
+               albums.clear();
+               for (Album album : albums) {
+                       addAlbum(album);
+               }
+       }
+
+       /**
+        * Removes an album from this Sone.
         *
-        * @return The fingerprint of this Sone
+        * @param album
+        *            The album to remove
+        */
+       public synchronized void removeAlbum(Album album) {
+               Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check();
+               albums.remove(album);
+       }
+
+       //
+       // FINGERPRINTABLE METHODS
+       //
+
+       /**
+        * {@inheritDoc}
         */
+       @Override
        public synchronized String getFingerprint() {
                StringBuilder fingerprint = new StringBuilder();
-               fingerprint.append("Profile(");
-               if (profile.getFirstName() != null) {
-                       fingerprint.append("FirstName(").append(profile.getFirstName()).append(')');
-               }
-               if (profile.getMiddleName() != null) {
-                       fingerprint.append("MiddleName(").append(profile.getMiddleName()).append(')');
-               }
-               if (profile.getLastName() != null) {
-                       fingerprint.append("LastName(").append(profile.getLastName()).append(')');
-               }
-               if (profile.getBirthDay() != null) {
-                       fingerprint.append("BirthDay(").append(profile.getBirthDay()).append(')');
-               }
-               if (profile.getBirthMonth() != null) {
-                       fingerprint.append("BirthMonth(").append(profile.getBirthMonth()).append(')');
-               }
-               if (profile.getBirthYear() != null) {
-                       fingerprint.append("BirthYear(").append(profile.getBirthYear()).append(')');
-               }
-               fingerprint.append(")");
+               fingerprint.append(profile.getFingerprint());
 
                fingerprint.append("Posts(");
                for (Post post : getPosts()) {
@@ -617,10 +671,28 @@ public class Sone {
                }
                fingerprint.append(')');
 
+//             fingerprint.append("Albums(");
+//             for (Album album : albums) {
+//                     fingerprint.append(album.getFingerprint());
+//             }
+//             fingerprint.append(')');
+
                return fingerprint.toString();
        }
 
        //
+       // INTERFACE Comparable<Sone>
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public int compareTo(Sone sone) {
+               return NICE_NAME_COMPARATOR.compare(this, sone);
+       }
+
+       //
        // OBJECT METHODS
        //