X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneChangeDetector.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneChangeDetector.java;h=0000000000000000000000000000000000000000;hp=efcdc6effed5606769dd74922b825105f2a31f97;hb=03cec6a6772c2d836d94864adddaf544cbe9d72f;hpb=6f1f26e3998cfef155b0cf59152827accea70d30 diff --git a/src/main/java/net/pterodactylus/sone/core/SoneChangeDetector.java b/src/main/java/net/pterodactylus/sone/core/SoneChangeDetector.java deleted file mode 100644 index efcdc6e..0000000 --- a/src/main/java/net/pterodactylus/sone/core/SoneChangeDetector.java +++ /dev/null @@ -1,114 +0,0 @@ -package net.pterodactylus.sone.core; - -import static com.google.common.base.Optional.absent; -import static com.google.common.base.Optional.fromNullable; -import static com.google.common.collect.FluentIterable.from; - -import java.util.Collection; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; - -import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.collect.FluentIterable; - -/** - * Compares the contents of two {@link Sone}s and fires events for new and - * removed elements. - * - * @author David ‘Bombe’ Roden - */ -public class SoneChangeDetector { - - private final Sone oldSone; - private Optional newPostProcessor = absent(); - private Optional removedPostProcessor = absent(); - private Optional newPostReplyProcessor = absent(); - private Optional removedPostReplyProcessor = absent(); - - public SoneChangeDetector(Sone oldSone) { - this.oldSone = oldSone; - } - - public void onNewPosts(PostProcessor newPostProcessor) { - this.newPostProcessor = fromNullable(newPostProcessor); - } - - public void onRemovedPosts(PostProcessor removedPostProcessor) { - this.removedPostProcessor = fromNullable(removedPostProcessor); - } - - public void onNewPostReplies(PostReplyProcessor newPostReplyProcessor) { - this.newPostReplyProcessor = fromNullable(newPostReplyProcessor); - } - - public void onRemovedPostReplies( - PostReplyProcessor removedPostReplyProcessor) { - this.removedPostReplyProcessor = fromNullable(removedPostReplyProcessor); - } - - public void detectChanges(Sone newSone) { - processPosts(from(newSone.getPosts()).filter( - notContainedIn(oldSone.getPosts())), newPostProcessor); - processPosts(from(oldSone.getPosts()).filter( - notContainedIn(newSone.getPosts())), removedPostProcessor); - processPostReplies(from(newSone.getReplies()).filter( - notContainedIn(oldSone.getReplies())), newPostReplyProcessor); - processPostReplies(from(oldSone.getReplies()).filter( - notContainedIn(newSone.getReplies())), removedPostReplyProcessor); - } - - private void processPostReplies(FluentIterable postReplies, - Optional postReplyProcessor) { - for (PostReply postReply : postReplies) { - notifyPostReplyProcessor(postReplyProcessor, postReply); - } - } - - private void notifyPostReplyProcessor( - Optional postReplyProcessor, - PostReply postReply) { - if (postReplyProcessor.isPresent()) { - postReplyProcessor.get() - .processPostReply(postReply); - } - } - - private void processPosts(FluentIterable posts, - Optional newPostProcessor) { - for (Post post : posts) { - notifyPostProcessor(newPostProcessor, post); - } - } - - private void notifyPostProcessor(Optional postProcessor, - Post newPost) { - if (postProcessor.isPresent()) { - postProcessor.get().processPost(newPost); - } - } - - private Predicate notContainedIn(final Collection posts) { - return new Predicate() { - @Override - public boolean apply(T element) { - return !posts.contains(element); - } - }; - } - - public interface PostProcessor { - - void processPost(Post post); - - } - - public interface PostReplyProcessor { - - void processPostReply(PostReply postReply); - - } - -}