Move Sone status into Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Jan 2012 20:58:51 +0000 (21:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 17 Jan 2012 16:09:23 +0000 (17:09 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/template/SoneAccessor.java
src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java

index a51a189..c58a508 100644 (file)
@@ -42,9 +42,10 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.data.Profile.Field;
+import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
+import net.pterodactylus.sone.data.Sone.SoneStatus;
 import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
 import net.pterodactylus.sone.freenet.wot.Identity;
@@ -74,26 +75,6 @@ import freenet.keys.FreenetURI;
  */
 public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener, ImageInsertListener {
 
  */
 public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener, ImageInsertListener {
 
-       /**
-        * Enumeration for the possible states of a {@link Sone}.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
-        */
-       public enum SoneStatus {
-
-               /** The Sone is unknown, i.e. not yet downloaded. */
-               unknown,
-
-               /** The Sone is idle, i.e. not being downloaded or inserted. */
-               idle,
-
-               /** The Sone is currently being inserted. */
-               inserting,
-
-               /** The Sone is currently being downloaded. */
-               downloading,
-       }
-
        /** The logger. */
        private static final Logger logger = Logging.getLogger(Core.class);
 
        /** The logger. */
        private static final Logger logger = Logging.getLogger(Core.class);
 
@@ -133,10 +114,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        /** The FCP interface. */
        private volatile FcpInterface fcpInterface;
 
        /** The FCP interface. */
        private volatile FcpInterface fcpInterface;
 
-       /** The Sones’ statuses. */
-       /* synchronize access on itself. */
-       private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
-
        /** The times Sones were followed. */
        private final Map<Sone, Long> soneFollowingTimes = new HashMap<Sone, Long>();
 
        /** The times Sones were followed. */
        private final Map<Sone, Long> soneFollowingTimes = new HashMap<Sone, Long>();
 
@@ -306,33 +283,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
        }
 
        /**
-        * Returns the status of the given Sone.
-        *
-        * @param sone
-        *            The Sone to get the status for
-        * @return The status of the Sone
-        */
-       public SoneStatus getSoneStatus(Sone sone) {
-               synchronized (soneStatuses) {
-                       return soneStatuses.get(sone);
-               }
-       }
-
-       /**
-        * Sets the status of the given Sone.
-        *
-        * @param sone
-        *            The Sone to set the status of
-        * @param soneStatus
-        *            The status to set
-        */
-       public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
-               synchronized (soneStatuses) {
-                       soneStatuses.put(sone, soneStatus);
-               }
-       }
-
-       /**
         * Returns the Sone rescuer for the given local Sone.
         *
         * @param sone
         * Returns the Sone rescuer for the given local Sone.
         *
         * @param sone
@@ -488,7 +438,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        if ((sone == null) && create) {
                                sone = new Sone(id);
                                localSones.put(id, sone);
                        if ((sone == null) && create) {
                                sone = new Sone(id);
                                localSones.put(id, sone);
-                               setSoneStatus(sone, SoneStatus.unknown);
                        }
                        return sone;
                }
                        }
                        return sone;
                }
@@ -521,7 +470,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        if ((sone == null) && create && (id != null) && (id.length() == 43)) {
                                sone = new Sone(id);
                                remoteSones.put(id, sone);
                        if ((sone == null) && create && (id != null) && (id.length() == 43)) {
                                sone = new Sone(id);
                                remoteSones.put(id, sone);
-                               setSoneStatus(sone, SoneStatus.unknown);
                        }
                        return sone;
                }
                        }
                        return sone;
                }
@@ -972,7 +920,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
                        soneInserter.addSoneInsertListener(this);
                        soneInserters.put(sone, soneInserter);
                        final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
                        soneInserter.addSoneInsertListener(this);
                        soneInserters.put(sone, soneInserter);
-                       setSoneStatus(sone, SoneStatus.idle);
+                       sone.setStatus(SoneStatus.idle);
                        loadSone(sone);
                        soneInserter.start();
                        return sone;
                        loadSone(sone);
                        soneInserter.start();
                        return sone;
@@ -1040,7 +988,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                }
                        }
                        soneDownloader.addSone(sone);
                                }
                        }
                        soneDownloader.addSone(sone);
-                       setSoneStatus(sone, SoneStatus.unknown);
                        soneDownloaders.execute(new Runnable() {
 
                                @Override
                        soneDownloaders.execute(new Runnable() {
 
                                @Override
index 5024ab9..90f926a 100644 (file)
@@ -26,7 +26,6 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import net.pterodactylus.sone.core.Core.SoneStatus;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
@@ -34,6 +33,7 @@ import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.Sone.SoneStatus;
 import net.pterodactylus.util.collection.Pair;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.collection.Pair;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
@@ -153,7 +153,7 @@ public class SoneDownloader extends AbstractService {
        public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) {
                logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, soneUri });
                FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" });
        public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) {
                logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, soneUri });
                FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" });
-               core.setSoneStatus(sone, SoneStatus.downloading);
+               sone.setStatus(SoneStatus.downloading);
                try {
                        Pair<FreenetURI, FetchResult> fetchResults = freenetInterface.fetchUri(requestUri);
                        if (fetchResults == null) {
                try {
                        Pair<FreenetURI, FetchResult> fetchResults = freenetInterface.fetchUri(requestUri);
                        if (fetchResults == null) {
@@ -170,7 +170,7 @@ public class SoneDownloader extends AbstractService {
                        }
                        return parsedSone;
                } finally {
                        }
                        return parsedSone;
                } finally {
-                       core.setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
+                       sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
                }
        }
 
                }
        }
 
index 4d9878f..ccf8a21 100644 (file)
@@ -27,11 +27,11 @@ import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import net.pterodactylus.sone.core.Core.SoneStatus;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.Sone.SoneStatus;
 import net.pterodactylus.sone.freenet.StringBucket;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.collection.ListBuilder;
 import net.pterodactylus.sone.freenet.StringBucket;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.collection.ListBuilder;
@@ -233,7 +233,7 @@ public class SoneInserter extends AbstractService {
 
                                boolean success = false;
                                try {
 
                                boolean success = false;
                                try {
-                                       core.setSoneStatus(sone, SoneStatus.inserting);
+                                       sone.setStatus(SoneStatus.inserting);
                                        long insertTime = System.currentTimeMillis();
                                        insertInformation.setTime(insertTime);
                                        soneInsertListenerManager.fireInsertStarted();
                                        long insertTime = System.currentTimeMillis();
                                        insertInformation.setTime(insertTime);
                                        soneInsertListenerManager.fireInsertStarted();
@@ -253,7 +253,7 @@ public class SoneInserter extends AbstractService {
                                        soneInsertListenerManager.fireInsertAborted(se1);
                                        logger.log(Level.WARNING, "Could not insert Sone “" + sone.getName() + "”!", se1);
                                } finally {
                                        soneInsertListenerManager.fireInsertAborted(se1);
                                        logger.log(Level.WARNING, "Could not insert Sone “" + sone.getName() + "”!", se1);
                                } finally {
-                                       core.setSoneStatus(sone, SoneStatus.idle);
+                                       sone.setStatus(SoneStatus.idle);
                                }
 
                                /*
                                }
 
                                /*
index 74ab276..63cdbcb 100644 (file)
@@ -49,6 +49,26 @@ import freenet.keys.FreenetURI;
 public class Sone implements Fingerprintable, Comparable<Sone> {
 
        /**
 public class Sone implements Fingerprintable, Comparable<Sone> {
 
        /**
+        * Enumeration for the possible states of a {@link Sone}.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public enum SoneStatus {
+
+               /** The Sone is unknown, i.e. not yet downloaded. */
+               unknown,
+
+               /** The Sone is idle, i.e. not being downloaded or inserted. */
+               idle,
+
+               /** The Sone is currently being inserted. */
+               inserting,
+
+               /** The Sone is currently being downloaded. */
+               downloading,
+       }
+
+       /**
         * The possible values for the “show custom avatars” option.
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         * The possible values for the “show custom avatars” option.
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
@@ -171,6 +191,9 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
        /** The time of the last inserted update. */
        private volatile long time;
 
        /** The time of the last inserted update. */
        private volatile long time;
 
+       /** The status of this Sone. */
+       private volatile SoneStatus status = SoneStatus.unknown;
+
        /** The profile of this Sone. */
        private volatile Profile profile = new Profile(this);
 
        /** The profile of this Sone. */
        private volatile Profile profile = new Profile(this);
 
@@ -360,6 +383,30 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
        }
 
        /**
        }
 
        /**
+        * Returns the status of this Sone.
+        *
+        * @return The status of this Sone
+        */
+       public SoneStatus getStatus() {
+               return status;
+       }
+
+       /**
+        * Sets the new status of this Sone.
+        *
+        * @param status
+        *            The new status of this Sone
+        * @return This Sone
+        * @throws IllegalArgumentException
+        *             if {@code status} is {@code null}
+        */
+       public Sone setStatus(SoneStatus status) {
+               Validation.begin().isNotNull("Sone Status", status).check();
+               this.status = status;
+               return this;
+       }
+
+       /**
         * Returns a copy of the profile. If you want to update values in the
         * profile of this Sone, update the values in the returned {@link Profile}
         * and use {@link #setProfile(Profile)} to change the profile in this Sone.
         * Returns a copy of the profile. If you want to update values in the
         * profile of this Sone, update the values in the returned {@link Profile}
         * and use {@link #setProfile(Profile)} to change the profile in this Sone.
index ead6066..305e9a8 100644 (file)
@@ -21,9 +21,9 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.sone.core.Core;
 import java.util.logging.Logger;
 
 import net.pterodactylus.sone.core.Core;
-import net.pterodactylus.sone.core.Core.SoneStatus;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.Sone.SoneStatus;
 import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
 import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
@@ -86,15 +86,15 @@ public class SoneAccessor extends ReflectionAccessor {
                } else if (member.equals("modified")) {
                        return core.isModifiedSone(sone);
                } else if (member.equals("status")) {
                } else if (member.equals("modified")) {
                        return core.isModifiedSone(sone);
                } else if (member.equals("status")) {
-                       return core.getSoneStatus(sone);
+                       return sone.getStatus();
                } else if (member.equals("unknown")) {
                } else if (member.equals("unknown")) {
-                       return core.getSoneStatus(sone) == SoneStatus.unknown;
+                       return sone.getStatus() == SoneStatus.unknown;
                } else if (member.equals("idle")) {
                } else if (member.equals("idle")) {
-                       return core.getSoneStatus(sone) == SoneStatus.idle;
+                       return sone.getStatus() == SoneStatus.idle;
                } else if (member.equals("inserting")) {
                } else if (member.equals("inserting")) {
-                       return core.getSoneStatus(sone) == SoneStatus.inserting;
+                       return sone.getStatus() == SoneStatus.inserting;
                } else if (member.equals("downloading")) {
                } else if (member.equals("downloading")) {
-                       return core.getSoneStatus(sone) == SoneStatus.downloading;
+                       return sone.getStatus() == SoneStatus.downloading;
                } else if (member.equals("new")) {
                        return core.isNewSone(sone.getId());
                } else if (member.equals("locked")) {
                } else if (member.equals("new")) {
                        return core.isNewSone(sone.getId());
                } else if (member.equals("locked")) {
index 1f23e4a..16bb133 100644 (file)
@@ -177,7 +177,7 @@ public class GetStatusAjaxPage extends JsonPage {
                jsonSone.put("id", sone.getId());
                jsonSone.put("name", SoneAccessor.getNiceName(sone));
                jsonSone.put("local", sone.getInsertUri() != null);
                jsonSone.put("id", sone.getId());
                jsonSone.put("name", SoneAccessor.getNiceName(sone));
                jsonSone.put("local", sone.getInsertUri() != null);
-               jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
+               jsonSone.put("status", sone.getStatus().name());
                jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
                jsonSone.put("locked", webInterface.getCore().isLocked(sone));
                jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
                jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
                jsonSone.put("locked", webInterface.getCore().isLocked(sone));
                jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);