From d50ceb969ffc7187d48127c14f8cd22ba135b137 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 19 Sep 2014 07:47:41 +0200 Subject: [PATCH] Use Sone change detector in core to send notifications. --- .../java/net/pterodactylus/sone/core/Core.java | 83 +++++++++------------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 7d38ed9..5fa330b 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -49,6 +49,8 @@ import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFou import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound; import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound; import net.pterodactylus.sone.core.Options.DefaultOption; +import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor; +import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor; import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay; import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; import net.pterodactylus.sone.core.event.MarkPostKnownEvent; @@ -946,7 +948,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * {@code true} if the stored Sone should be updated regardless * of the age of the given Sone */ - public void updateSone(Sone sone, boolean soneRescueMode) { + public void updateSone(final Sone sone, boolean soneRescueMode) { Optional storedSone = getSone(sone.getId()); if (storedSone.isPresent()) { if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) { @@ -954,47 +956,42 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return; } /* find removed posts. */ - Collection removedPosts = new ArrayList(); - Collection newPosts = new ArrayList(); - Collection existingPosts = database.getPosts(sone.getId()); - for (Post oldPost : existingPosts) { - if (!sone.getPosts().contains(oldPost)) { - removedPosts.add(oldPost); - } - } - /* find new posts. */ - for (Post newPost : sone.getPosts()) { - if (existingPosts.contains(newPost)) { - continue; + SoneChangeDetector soneChangeDetector = new SoneChangeDetector(storedSone.get()); + soneChangeDetector.onNewPosts(new PostProcessor() { + @Override + public void processPost(Post post) { + if (post.getTime() < getSoneFollowingTime(sone)) { + post.setKnown(true); + } else if (!post.isKnown()) { + eventBus.post(new NewPostFoundEvent(post)); + } } - if (newPost.getTime() < getSoneFollowingTime(sone)) { - newPost.setKnown(true); - } else if (!newPost.isKnown()) { - newPosts.add(newPost); + }); + soneChangeDetector.onRemovedPosts(new PostProcessor() { + @Override + public void processPost(Post post) { + eventBus.post(new PostRemovedEvent(post)); } - } - /* store posts. */ - database.storePosts(sone, sone.getPosts()); - Collection newPostReplies = new ArrayList(); - Collection removedPostReplies = new ArrayList(); - if (!soneRescueMode) { - for (PostReply reply : storedSone.get().getReplies()) { - if (!sone.getReplies().contains(reply)) { - removedPostReplies.add(reply); + }); + soneChangeDetector.onNewPostReplies(new PostReplyProcessor() { + @Override + public void processPostReply(PostReply postReply) { + if (postReply.getTime() < getSoneFollowingTime(sone)) { + postReply.setKnown(true); + } else if (!postReply.isKnown()) { + eventBus.post(new NewPostReplyFoundEvent(postReply)); } } - } - Set storedReplies = storedSone.get().getReplies(); - for (PostReply reply : sone.getReplies()) { - if (storedReplies.contains(reply)) { - continue; - } - if (reply.getTime() < getSoneFollowingTime(sone)) { - reply.setKnown(true); - } else if (!reply.isKnown()) { - newPostReplies.add(reply); + }); + soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() { + @Override + public void processPostReply(PostReply postReply) { + eventBus.post(new PostReplyRemovedEvent(postReply)); } - } + }); + soneChangeDetector.detectChanges(sone); + /* store posts. */ + database.storePosts(sone, sone.getPosts()); database.storePostReplies(sone, sone.getReplies()); for (Album album : storedSone.get().getRootAlbum().getAlbums()) { database.removeAlbum(album); @@ -1002,18 +999,6 @@ 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 : toAllAlbums.apply(sone)) { database.storeAlbum(album); for (Image image : album.getImages()) { -- 2.7.4