From d3341150dde11b1d510193b71b22db85e426d6fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 20 Jun 2020 14:41:17 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Use=20Java=E2=80=99s=20Predicate?= =?utf8?q?=20in=20post=20visibility=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/notify/ListNotificationFilter.java | 3 ++- .../net/pterodactylus/sone/notify/PostVisibilityFilter.java | 11 +++-------- src/main/java/net/pterodactylus/sone/web/WebInterface.java | 2 +- src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt | 2 +- .../net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt | 4 ++-- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java index 739907c..f2da694 100644 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java @@ -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> filterPostNotification(@Nonnull ListNotification postNotification, @Nullable Sone currentSone) { - List newPosts = from(postNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList(); + List newPosts = postNotification.getElements().stream().filter(postVisibilityFilter.isVisible(currentSone)).collect(toList()); if (newPosts.isEmpty()) { return Optional.absent(); } diff --git a/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java b/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java index d29d537..cf84ebf 100644 --- a/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java +++ b/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java @@ -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 isVisible(@Nullable final Sone currentSone) { - return new Predicate() { - @Override - public boolean apply(@Nullable Post post) { - return (post != null) && isPostVisible(currentSone, post); - } - }; + return post -> (post != null) && isPostVisible(currentSone, post); } } diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 40e5708..96677db 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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 diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt index 069ef9f..5e3f905 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt @@ -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) diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt index 549b06e..f89035c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt @@ -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)) } } -- 2.7.4