Implement getDirectedSones() differently, adhere to new post provider interface.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 8172b45..bafe7a8 100644 (file)
@@ -149,7 +149,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        private volatile FcpInterface fcpInterface;
 
        /** The times Sones were followed. */
-       private final Map<Sone, Long> soneFollowingTimes = new HashMap<Sone, Long>();
+       private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
 
        /** Locked local Sones. */
        /* synchronize on itself. */
@@ -357,30 +357,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return The Sone with the given ID, or {@code null} if there is no such
         *         Sone
         */
-       public Sone getSone(String id) {
-               return getSone(id, true);
-       }
-
-       /**
-        * Returns the Sone with the given ID, regardless whether it’s local or
-        * remote.
-        *
-        * @param id
-        *            The ID of the Sone to get
-        * @param create
-        *            {@code true} to create a new Sone if none exists,
-        *            {@code false} to return {@code null} if a Sone with the given
-        *            ID does not exist
-        * @return The Sone with the given ID, or {@code null} if there is no such
-        *         Sone
-        */
        @Override
-       public Sone getSone(String id, boolean create) {
+       public Sone getSone(String id) {
                synchronized (sones) {
-                       if (!sones.containsKey(id) && create) {
-                               Sone sone = new Sone(id, false);
-                               sones.put(id, sone);
-                       }
                        return sones.get(id);
                }
        }
@@ -544,26 +523,21 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Returns all posts that have the given Sone as recipient.
-        *
-        * @see Post#getRecipient()
-        * @param recipient
-        *            The recipient of the posts
-        * @return All posts that have the given Sone as recipient
+        * {@inheritDoc}
         */
-       public Set<Post> getDirectedPosts(Sone recipient) {
-               checkNotNull(recipient, "recipient must not be null");
-               Set<Post> directedPosts = new HashSet<Post>();
+       @Override
+       public Collection<Post> getDirectedPosts(final String recipientId) {
+               checkNotNull(recipientId, "recipient must not be null");
                synchronized (posts) {
-                       for (Post post : posts.values()) {
-                               if (recipient.equals(post.getRecipient())) {
-                                       directedPosts.add(post);
+                       return Collections2.filter(posts.values(), new Predicate<Post>() {
+
+                               @Override
+                               public boolean apply(Post post) {
+                                       return (post.getRecipient() != null) && (post.getRecipient().getId().equals(recipientId));
                                }
-                       }
+                       });
                }
-               return directedPosts;
        }
-
        /**
         * Returns a post reply builder.
         *
@@ -851,7 +825,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption<Boolean>(true));
                sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption<ShowCustomAvatars>(ShowCustomAvatars.NEVER));
 
-               followSone(sone, getSone("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"));
+               followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
                touchConfiguration();
                return sone;
        }
@@ -882,7 +856,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                        eventBus.post(new NewSoneFoundEvent(sone));
                                        for (Sone localSone : getLocalSones()) {
                                                if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
-                                                       followSone(localSone, sone);
+                                                       followSone(localSone, sone.getId());
                                                }
                                        }
                                }
@@ -912,33 +886,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void followSone(Sone sone, String soneId) {
                checkNotNull(sone, "sone must not be null");
                checkNotNull(soneId, "soneId must not be null");
-               Sone followedSone = getSone(soneId, true);
-               if (followedSone == null) {
-                       logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId));
-                       return;
-               }
-               followSone(sone, getSone(soneId));
-       }
-
-       /**
-        * Lets the given local Sone follow the other given Sone. If the given Sone
-        * was not followed by any local Sone before, this will mark all elements of
-        * the followed Sone as read that have been created before the current
-        * moment.
-        *
-        * @param sone
-        *            The local Sone that should follow the other Sone
-        * @param followedSone
-        *            The Sone that should be followed
-        */
-       public void followSone(Sone sone, Sone followedSone) {
-               checkNotNull(sone, "sone must not be null");
-               checkNotNull(followedSone, "followedSone must not be null");
-               sone.addFriend(followedSone.getId());
+               sone.addFriend(soneId);
                synchronized (soneFollowingTimes) {
-                       if (!soneFollowingTimes.containsKey(followedSone)) {
+                       if (!soneFollowingTimes.containsKey(soneId)) {
                                long now = System.currentTimeMillis();
-                               soneFollowingTimes.put(followedSone, now);
+                               soneFollowingTimes.put(soneId, now);
+                               Sone followedSone = getSone(soneId);
+                               if (followedSone == null) {
+                                       return;
+                               }
                                for (Post post : followedSone.getPosts()) {
                                        if (post.getTime() < now) {
                                                markPostKnown(post);
@@ -965,30 +921,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void unfollowSone(Sone sone, String soneId) {
                checkNotNull(sone, "sone must not be null");
                checkNotNull(soneId, "soneId must not be null");
-               unfollowSone(sone, getSone(soneId, false));
-       }
-
-       /**
-        * Lets the given local Sone unfollow the other given Sone. If the given
-        * local Sone is the last local Sone that followed the given Sone, its
-        * following time will be removed.
-        *
-        * @param sone
-        *            The local Sone that should unfollow another Sone
-        * @param unfollowedSone
-        *            The Sone being unfollowed
-        */
-       public void unfollowSone(Sone sone, Sone unfollowedSone) {
-               checkNotNull(sone, "sone must not be null");
-               checkNotNull(unfollowedSone, "unfollowedSone must not be null");
-               sone.removeFriend(unfollowedSone.getId());
+               sone.removeFriend(soneId);
                boolean unfollowedSoneStillFollowed = false;
                for (Sone localSone : getLocalSones()) {
-                       unfollowedSoneStillFollowed |= localSone.hasFriend(unfollowedSone.getId());
+                       unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
                }
                if (!unfollowedSoneStillFollowed) {
                        synchronized (soneFollowingTimes) {
-                               soneFollowingTimes.remove(unfollowedSone);
+                               soneFollowingTimes.remove(soneId);
                        }
                }
                touchConfiguration();
@@ -1104,7 +1044,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                synchronized (knownPosts) {
                                        for (Post post : sone.getPosts()) {
                                                PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
-                                               postBuilder.copyPost(post).from(storedSone);
+                                               postBuilder.copyPost(post).from(storedSone.getId());
                                                Post newPost = postBuilder.build().setKnown(knownPosts.contains(post.getId()));
                                                if (!storedPosts.contains(newPost)) {
                                                        if (newPost.getTime() < getSoneFollowingTime(sone)) {
@@ -1130,7 +1070,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                Set<PostReply> storedReplies = storedSone.getReplies();
                                synchronized (knownReplies) {
                                        for (PostReply reply : sone.getReplies()) {
-                                               reply.setSone(storedSone).setKnown(knownReplies.contains(reply.getId()));
+                                               reply.setKnown(knownReplies.contains(reply.getId()));
                                                if (!storedReplies.contains(reply)) {
                                                        if (reply.getTime() < getSoneFollowingTime(sone)) {
                                                                knownReplies.add(reply.getId());
@@ -1307,9 +1247,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid post found, aborting load!");
                                return;
                        }
-                       PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone).withTime(postTime).withText(postText);
+                       PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
-                               postBuilder.to(getSone(postRecipientId));
+                               postBuilder.to(postRecipientId);
                        }
                        posts.add(postBuilder.build());
                }
@@ -1330,7 +1270,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                return;
                        }
                        PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder();
-                       postReplyBuilder.withId(replyId).from(sone).to(postId).withTime(replyTime).withText(replyText);
+                       postReplyBuilder.withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText);
                        replies.add(postReplyBuilder.build());
                }
 
@@ -1536,9 +1476,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
-               postBuilder.from(sone).randomId().withTime(time).withText(text.trim());
+               postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
                if (recipient != null) {
-                       postBuilder.to(recipient);
+                       postBuilder.to(recipient.getId());
                }
                final Post post = postBuilder.build();
                synchronized (posts) {
@@ -1663,7 +1603,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder();
-               postReplyBuilder.randomId().from(sone).to(post.getId()).currentTime().withText(text.trim());
+               postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
                final PostReply reply = postReplyBuilder.build();
                synchronized (replies) {
                        replies.put(reply.getId(), reply);
@@ -2120,8 +2060,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        /* save Sone following times. */
                        soneCounter = 0;
                        synchronized (soneFollowingTimes) {
-                               for (Entry<Sone, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
-                                       configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey().getId());
+                               for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
+                                       configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
                                        configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
                                        ++soneCounter;
                                }
@@ -2244,7 +2184,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, String.format("Ignoring Sone with invalid ID: %s", soneId));
                        } else {
                                synchronized (soneFollowingTimes) {
-                                       soneFollowingTimes.put(getSone(soneId), time);
+                                       soneFollowingTimes.put(soneId, time);
                                }
                        }
                        ++soneCounter;
@@ -2393,7 +2333,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        /* some local identity still trusts this identity, don’t remove. */
                        return;
                }
-               Sone sone = getSone(identity.getId(), false);
+               Sone sone = getSone(identity.getId());
                if (sone == null) {
                        /* TODO - we don’t have the Sone anymore. should this happen? */
                        return;