X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneChangeDetector.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneChangeDetector.java;h=efcdc6effed5606769dd74922b825105f2a31f97;hb=bae04bf1966d021bd57d5cfc827e8f2b1cd247ce;hp=0000000000000000000000000000000000000000;hpb=39ad8597b568c51f095c07a8de4f5424db185d8e;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneChangeDetector.java b/src/main/java/net/pterodactylus/sone/core/SoneChangeDetector.java new file mode 100644 index 0000000..efcdc6e --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/SoneChangeDetector.java @@ -0,0 +1,114 @@ +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); + + } + +}