Add sone change detector that finds new and removed elements.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 19 Sep 2014 05:46:42 +0000 (07:46 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 19 Sep 2014 05:46:42 +0000 (07:46 +0200)
src/main/java/net/pterodactylus/sone/core/SoneChangeDetector.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/core/SoneChangeDetectorTest.java [new file with mode: 0644]

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 (file)
index 0000000..efcdc6e
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SoneChangeDetector {
+
+       private final Sone oldSone;
+       private Optional<PostProcessor> newPostProcessor = absent();
+       private Optional<PostProcessor> removedPostProcessor = absent();
+       private Optional<PostReplyProcessor> newPostReplyProcessor = absent();
+       private Optional<PostReplyProcessor> 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<PostReply> postReplies,
+                       Optional<PostReplyProcessor> postReplyProcessor) {
+               for (PostReply postReply : postReplies) {
+                       notifyPostReplyProcessor(postReplyProcessor, postReply);
+               }
+       }
+
+       private void notifyPostReplyProcessor(
+                       Optional<PostReplyProcessor> postReplyProcessor,
+                       PostReply postReply) {
+               if (postReplyProcessor.isPresent()) {
+                       postReplyProcessor.get()
+                                       .processPostReply(postReply);
+               }
+       }
+
+       private void processPosts(FluentIterable<Post> posts,
+                       Optional<PostProcessor> newPostProcessor) {
+               for (Post post : posts) {
+                       notifyPostProcessor(newPostProcessor, post);
+               }
+       }
+
+       private void notifyPostProcessor(Optional<PostProcessor> postProcessor,
+                       Post newPost) {
+               if (postProcessor.isPresent()) {
+                       postProcessor.get().processPost(newPost);
+               }
+       }
+
+       private <T> Predicate<T> notContainedIn(final Collection<T> posts) {
+               return new Predicate<T>() {
+                       @Override
+                       public boolean apply(T element) {
+                               return !posts.contains(element);
+                       }
+               };
+       }
+
+       public interface PostProcessor {
+
+               void processPost(Post post);
+
+       }
+
+       public interface PostReplyProcessor {
+
+               void processPostReply(PostReply postReply);
+
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/core/SoneChangeDetectorTest.java b/src/test/java/net/pterodactylus/sone/core/SoneChangeDetectorTest.java
new file mode 100644 (file)
index 0000000..f070eff
--- /dev/null
@@ -0,0 +1,108 @@
+package net.pterodactylus.sone.core;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.HashSet;
+
+import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
+import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
+import net.pterodactylus.sone.data.Sone;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link SoneChangeDetector}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SoneChangeDetectorTest {
+
+       private final Sone oldSone = mock(Sone.class);
+       private final Sone newSone = mock(Sone.class);
+       private final SoneChangeDetector soneChangeDetector =
+                       new SoneChangeDetector(oldSone);
+       private final Post oldPost = mock(Post.class);
+       private final Post removedPost = mock(Post.class);
+       private final Post newPost = mock(Post.class);
+       private final PostProcessor newPostProcessor = mock(PostProcessor.class);
+       private final PostProcessor removedPostProcessor =
+                       mock(PostProcessor.class);
+       private final PostReply oldPostReply = mock(PostReply.class);
+       private final PostReply removedPostReply = mock(PostReply.class);
+       private final PostReply newPostReply = mock(PostReply.class);
+       private final PostReplyProcessor newPostReplyProcessor =
+                       mock(PostReplyProcessor.class);
+       private final PostReplyProcessor removedPostReplyProcessor =
+                       mock(PostReplyProcessor.class);
+
+       @Before
+       public void setupPosts() {
+               when(oldSone.getPosts()).thenReturn(asList(oldPost, removedPost));
+               when(newSone.getPosts()).thenReturn(asList(oldPost, newPost));
+       }
+
+       @Before
+       public void setupPostProcessors() {
+               soneChangeDetector.onNewPosts(newPostProcessor);
+               soneChangeDetector.onRemovedPosts(removedPostProcessor);
+       }
+
+       @Before
+       public void setupPostReplies() {
+               when(oldSone.getReplies()).thenReturn(
+                               new HashSet<PostReply>(
+                                               asList(oldPostReply, removedPostReply)));
+               when(newSone.getReplies()).thenReturn(
+                               new HashSet<PostReply>(asList(oldPostReply, newPostReply)));
+       }
+
+       @Before
+       public void setupPostReplyProcessors() {
+               soneChangeDetector.onNewPostReplies(newPostReplyProcessor);
+               soneChangeDetector.onRemovedPostReplies(removedPostReplyProcessor);
+       }
+
+       @Test
+       public void changeDetectorDetectsChanges() {
+               soneChangeDetector.detectChanges(newSone);
+
+               verify(newPostProcessor).processPost(newPost);
+               verify(newPostProcessor, never()).processPost(oldPost);
+               verify(newPostProcessor, never()).processPost(removedPost);
+               verify(removedPostProcessor).processPost(removedPost);
+               verify(removedPostProcessor, never()).processPost(oldPost);
+               verify(removedPostProcessor, never()).processPost(newPost);
+
+               verify(newPostReplyProcessor).processPostReply(newPostReply);
+               verify(newPostReplyProcessor, never()).processPostReply(oldPostReply);
+               verify(newPostReplyProcessor, never()).processPostReply(
+                               removedPostReply);
+               verify(removedPostReplyProcessor).processPostReply(removedPostReply);
+               verify(removedPostReplyProcessor, never()).processPostReply(
+                               oldPostReply);
+               verify(removedPostReplyProcessor, never()).processPostReply(
+                               newPostReply);
+       }
+
+       @Test
+       public void changeDetectorDoesNotNotifyAnyProcessorIfProcessorsUnset() {
+           soneChangeDetector.onNewPosts(null);
+           soneChangeDetector.onRemovedPosts(null);
+           soneChangeDetector.onNewPostReplies(null);
+           soneChangeDetector.onRemovedPostReplies(null);
+               soneChangeDetector.detectChanges(newSone);
+               verify(newPostProcessor, never()).processPost(any(Post.class));
+               verify(removedPostProcessor, never()).processPost(any(Post.class));
+               verify(newPostReplyProcessor, never()).processPostReply(any(PostReply.class));
+               verify(removedPostReplyProcessor, never()).processPostReply(any(PostReply.class));
+       }
+
+}