From 161c216be80f6f0438bacd2642b1aa0091ba1b1f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 6 Nov 2014 06:51:05 +0100 Subject: [PATCH] Collect events in a separate method. --- .../java/net/pterodactylus/sone/core/Core.java | 78 ++++++++++++---------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 97182c2..dc5e207 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -865,41 +865,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone)); return; } - final List events = new ArrayList(); - 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()) { - events.add(new NewPostFoundEvent(post)); - } - } - }); - soneChangeDetector.onRemovedPosts(new PostProcessor() { - @Override - public void processPost(Post post) { - events.add(new PostRemovedEvent(post)); - } - }); - soneChangeDetector.onNewPostReplies(new PostReplyProcessor() { - @Override - public void processPostReply(PostReply postReply) { - if (postReply.getTime() < getSoneFollowingTime(sone)) { - postReply.setKnown(true); - } else if (!postReply.isKnown()) { - events.add(new NewPostReplyFoundEvent(postReply)); - } - } - }); - soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() { - @Override - public void processPostReply(PostReply postReply) { - events.add(new PostReplyRemovedEvent(postReply)); - } - }); - soneChangeDetector.detectChanges(sone); + List events = + collectEventsForChangesInSone(storedSone.get(), sone); database.storeSone(sone); for (Object event : events) { eventBus.post(event); @@ -913,6 +880,47 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } } + private List collectEventsForChangesInSone(Sone oldSone, + final Sone newSone) { + final List events = new ArrayList(); + SoneChangeDetector soneChangeDetector = new SoneChangeDetector( + oldSone); + soneChangeDetector.onNewPosts(new PostProcessor() { + @Override + public void processPost(Post post) { + if (post.getTime() < getSoneFollowingTime(newSone)) { + post.setKnown(true); + } else if (!post.isKnown()) { + events.add(new NewPostFoundEvent(post)); + } + } + }); + soneChangeDetector.onRemovedPosts(new PostProcessor() { + @Override + public void processPost(Post post) { + events.add(new PostRemovedEvent(post)); + } + }); + soneChangeDetector.onNewPostReplies(new PostReplyProcessor() { + @Override + public void processPostReply(PostReply postReply) { + if (postReply.getTime() < getSoneFollowingTime(newSone)) { + postReply.setKnown(true); + } else if (!postReply.isKnown()) { + events.add(new NewPostReplyFoundEvent(postReply)); + } + } + }); + soneChangeDetector.onRemovedPostReplies(new PostReplyProcessor() { + @Override + public void processPostReply(PostReply postReply) { + events.add(new PostReplyRemovedEvent(postReply)); + } + }); + soneChangeDetector.detectChanges(newSone); + return events; + } + /** * Deletes the given Sone. This will remove the Sone from the * {@link #getLocalSones() local Sones}, stop its {@link SoneInserter} and -- 2.7.4