Use the appropriate Sone predicates.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index ecb5857..643c835 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.sone.core;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Predicates.not;
+import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
 
 import java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -91,7 +93,6 @@ import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
 import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultimap;
@@ -127,7 +128,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        private final EventBus eventBus;
 
        /** The configuration. */
-       private Configuration configuration;
+       private final Configuration configuration;
 
        /** Whether we’re currently saving the configuration. */
        private boolean storingConfiguration = false;
@@ -241,18 +242,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Sets the configuration to use. This will automatically save the current
-        * configuration to the given configuration.
-        *
-        * @param configuration
-        *            The new configuration to use
-        */
-       public void setConfiguration(Configuration configuration) {
-               this.configuration = configuration;
-               touchConfiguration();
-       }
-
-       /**
         * Returns the options used by the core.
         *
         * @return The options of the core
@@ -355,13 +344,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Override
        public Collection<Sone> getLocalSones() {
                synchronized (sones) {
-                       return FluentIterable.from(sones.values()).filter(new Predicate<Sone>() {
-
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return sone.isLocal();
-                               }
-                       }).toSet();
+                       return FluentIterable.from(sones.values()).filter(LOCAL_SONE_FILTER).toSet();
                }
        }
 
@@ -396,13 +379,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Override
        public Collection<Sone> getRemoteSones() {
                synchronized (sones) {
-                       return FluentIterable.from(sones.values()).filter(new Predicate<Sone>() {
-
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return !sone.isLocal();
-                               }
-                       }).toSet();
+                       return FluentIterable.from(sones.values()).filter(not(LOCAL_SONE_FILTER)).toSet();
                }
        }
 
@@ -436,7 +413,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         {@code false} otherwise
         */
        public boolean isModifiedSone(Sone sone) {
-               return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
+               return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
        }
 
        /**
@@ -994,10 +971,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                return;
                        }
                        /* find removed posts. */
+                       Collection<Post> removedPosts = new ArrayList<Post>();
+                       Collection<Post> newPosts = new ArrayList<Post>();
                        Collection<Post> existingPosts = database.getPosts(sone.getId());
                        for (Post oldPost : existingPosts) {
                                if (!sone.getPosts().contains(oldPost)) {
-                                       eventBus.post(new PostRemovedEvent(oldPost));
+                                       removedPosts.add(oldPost);
                                }
                        }
                        /* find new posts. */
@@ -1008,15 +987,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                if (newPost.getTime() < getSoneFollowingTime(sone)) {
                                        newPost.setKnown(true);
                                } else if (!newPost.isKnown()) {
-                                       eventBus.post(new NewPostFoundEvent(newPost));
+                                       newPosts.add(newPost);
                                }
                        }
                        /* store posts. */
                        database.storePosts(sone, sone.getPosts());
+                       Collection<PostReply> newPostReplies = new ArrayList<PostReply>();
+                       Collection<PostReply> removedPostReplies = new ArrayList<PostReply>();
                        if (!soneRescueMode) {
                                for (PostReply reply : storedSone.get().getReplies()) {
                                        if (!sone.getReplies().contains(reply)) {
-                                               eventBus.post(new PostReplyRemovedEvent(reply));
+                                               removedPostReplies.add(reply);
                                        }
                                }
                        }
@@ -1028,7 +1009,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                if (reply.getTime() < getSoneFollowingTime(sone)) {
                                        reply.setKnown(true);
                                } else if (!reply.isKnown()) {
-                                       eventBus.post(new NewPostReplyFoundEvent(reply));
+                                       newPostReplies.add(reply);
                                }
                        }
                        database.storePostReplies(sone, sone.getReplies());
@@ -1038,6 +1019,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                        database.removeImage(image);
                                }
                        }
+                       for (Post removedPost : removedPosts) {
+                               eventBus.post(new PostRemovedEvent(removedPost));
+                       }
+                       for (Post newPost : newPosts) {
+                               eventBus.post(new NewPostFoundEvent(newPost));
+                       }
+                       for (PostReply removedPostReply : removedPostReplies) {
+                               eventBus.post(new PostReplyRemovedEvent(removedPostReply));
+                       }
+                       for (PostReply newPostReply : newPostReplies) {
+                               eventBus.post(new NewPostReplyFoundEvent(newPostReply));
+                       }
                        for (Album album : sone.getRootAlbum().getAlbums()) {
                                database.storeAlbum(album);
                                for (Image image : album.getImages()) {