Use copy-on-write sets instead of synchronized collections.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
index e36a306..b79dad3 100644 (file)
@@ -21,9 +21,10 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -47,6 +48,30 @@ import freenet.keys.FreenetURI;
  */
 public class Sone implements Fingerprintable, Comparable<Sone> {
 
+       /**
+        * The possible values for the “show custom avatars” option.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public static enum ShowCustomAvatars {
+
+               /** Never show custom avatars. */
+               NEVER,
+
+               /** Only show custom avatars of followed Sones. */
+               FOLLOWED,
+
+               /** Only show custom avatars of Sones you manually trust. */
+               MANUALLY_TRUSTED,
+
+               /** Only show custom avatars of automatically trusted Sones. */
+               TRUSTED,
+
+               /** Always show custom avatars. */
+               ALWAYS,
+
+       }
+
        /** comparator that sorts Sones by their nice name. */
        public static final Comparator<Sone> NICE_NAME_COMPARATOR = new Comparator<Sone>() {
 
@@ -147,35 +172,32 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
        private volatile long time;
 
        /** The profile of this Sone. */
-       private volatile Profile profile = new Profile();
+       private volatile Profile profile = new Profile(this);
 
        /** The client used by the Sone. */
        private volatile Client client;
 
        /** All friend Sones. */
-       private final Set<String> friendSones = Collections.synchronizedSet(new HashSet<String>());
+       private final Set<String> friendSones = new CopyOnWriteArraySet<String>();
 
        /** All posts. */
-       private final Set<Post> posts = Collections.synchronizedSet(new HashSet<Post>());
+       private final Set<Post> posts = new CopyOnWriteArraySet<Post>();
 
        /** All replies. */
-       private final Set<PostReply> replies = Collections.synchronizedSet(new HashSet<PostReply>());
+       private final Set<PostReply> replies = new CopyOnWriteArraySet<PostReply>();
 
        /** The IDs of all liked posts. */
-       private final Set<String> likedPostIds = Collections.synchronizedSet(new HashSet<String>());
+       private final Set<String> likedPostIds = new CopyOnWriteArraySet<String>();
 
        /** The IDs of all liked replies. */
-       private final Set<String> likedReplyIds = Collections.synchronizedSet(new HashSet<String>());
+       private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
 
        /** The albums of this Sone. */
-       private final List<Album> albums = Collections.synchronizedList(new ArrayList<Album>());
+       private final List<Album> albums = new CopyOnWriteArrayList<Album>();
 
        /** Sone-specific options. */
        private final Options options = new Options();
 
-       /** The avatar of this Sone. */
-       private volatile String avatar;
-
        /**
         * Creates a new Sone.
         *
@@ -767,34 +789,6 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
        }
 
        /**
-        * Returns the ID of the currently selected avatar image.
-        *
-        * @return The ID of the currently selected avatar image, or {@code null} if
-        *         no avatar is selected.
-        */
-       public String getAvatar() {
-               return avatar;
-       }
-
-       /**
-        * Sets the avatar image.
-        *
-        * @param avatar
-        *            The new avatar image, or {@code null} to not select an avatar
-        *            image.
-        * @return This Sone
-        */
-       public Sone setAvatar(Image avatar) {
-               if (avatar == null) {
-                       this.avatar = null;
-                       return this;
-               }
-               Validation.begin().isEqual("Image Owner", avatar.getSone(), this).check();
-               this.avatar = avatar.getId();
-               return this;
-       }
-
-       /**
         * Returns Sone-specific options.
         *
         * @return The options of this Sone
@@ -851,8 +845,6 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
                }
                fingerprint.append(')');
 
-               fingerprint.append("Avatar(").append(avatar).append(')');
-
                return fingerprint.toString();
        }