🎨 Use Java’s Predicate in post visibility filter
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 20 Jun 2020 12:41:17 +0000 (14:41 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 20 Jun 2020 12:49:59 +0000 (14:49 +0200)
src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java
src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt
src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt

index 739907c..f2da694 100644 (file)
@@ -18,6 +18,7 @@
 package net.pterodactylus.sone.notify;
 
 import static com.google.common.collect.FluentIterable.from;
+import static java.util.stream.Collectors.toList;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -119,7 +120,7 @@ public class ListNotificationFilter {
        @Nonnull
        private Optional<ListNotification<Post>> filterPostNotification(@Nonnull ListNotification<Post> postNotification,
                        @Nullable Sone currentSone) {
-               List<Post> newPosts = from(postNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList();
+               List<Post> newPosts = postNotification.getElements().stream().filter(postVisibilityFilter.isVisible(currentSone)).collect(toList());
                if (newPosts.isEmpty()) {
                        return Optional.absent();
                }
index d29d537..cf84ebf 100644 (file)
@@ -1,5 +1,7 @@
 package net.pterodactylus.sone.notify;
 
+import java.util.function.Predicate;
+
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import javax.annotation.Nonnull;
@@ -12,8 +14,6 @@ import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.util.notify.Notification;
 
-import com.google.common.base.Predicate;
-
 /**
  * Filters {@link Notification}s involving {@link Post}s.
  */
@@ -84,12 +84,7 @@ public class PostVisibilityFilter {
 
        @Nonnull
        public Predicate<Post> isVisible(@Nullable final Sone currentSone) {
-               return new Predicate<Post>() {
-                       @Override
-                       public boolean apply(@Nullable Post post) {
-                               return (post != null) && isPostVisible(currentSone, post);
-                       }
-               };
+               return post -> (post != null) && isPostVisible(currentSone, post);
        }
 
 }
index 40e5708..96677db 100644 (file)
@@ -264,7 +264,7 @@ public class WebInterface implements SessionProvider {
                                .addAll(newPostNotification.getElements())
                                .addAll(localPostNotification.getElements())
                                .build();
-               return from(allNewPosts).filter(postVisibilityFilter.isVisible(currentSone)).toSet();
+               return allNewPosts.stream().filter(postVisibilityFilter.isVisible(currentSone)).collect(toSet());
        }
 
        @Nonnull
index 069ef9f..5e3f905 100644 (file)
@@ -26,7 +26,7 @@ class IndexPage @Inject constructor(webInterface: WebInterface, loaders: Loaders
                                                .flatMap { it.posts } +
                                soneRequest.core.getDirectedPosts(currentSone.id)
                                ).distinct()
-                               .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
+                               .filter { postVisibilityFilter.isVisible(currentSone).test(it) }
                                .sortedByDescending { it.time }
                                .let { posts ->
                                        posts.paginate(soneRequest.core.preferences.postsPerPage)
index 549b06e..f89035c 100644 (file)
@@ -111,13 +111,13 @@ class PostVisibilityFilterTest {
        @Test
        fun `predicate will correctly recognize visible post`() {
                val post = createPost(sone = localSone)
-               assertThat(postVisibilityFilter.isVisible(null).apply(post), equalTo(true))
+               assertThat(postVisibilityFilter.isVisible(null).test(post), equalTo(true))
        }
 
        @Test
        fun `predicate will correctly recognize not visible post`() {
                val post = createPost(loaded = false)
-               assertThat(postVisibilityFilter.isVisible(null).apply(post), equalTo(false))
+               assertThat(postVisibilityFilter.isVisible(null).test(post), equalTo(false))
        }
 
 }