From 7d89048a7d5e9890d130f37994e8b4a410e692f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 28 Jun 2020 21:51:19 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Use=20Kotlin=20arrow=20type=20ins?= =?utf8?q?tead=20of=20Predicate?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/main/java/net/pterodactylus/sone/web/WebInterface.java | 4 ++-- src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt | 4 ++-- .../kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilter.kt | 4 ++-- src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt | 2 +- .../net/pterodactylus/sone/notify/DefaultPostVisibilityFilterTest.kt | 4 ++-- .../kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt | 4 ++-- src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 69077ed..4650625 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 allNewPosts.stream().filter(postVisibilityFilter.isVisible(currentSone)).collect(toSet()); + return allNewPosts.stream().filter(p -> postVisibilityFilter.isPostVisible(currentSone, p)).collect(toSet()); } @Nonnull @@ -273,7 +273,7 @@ public class WebInterface implements SessionProvider { .addAll(newReplyNotification.getElements()) .addAll(localReplyNotification.getElements()) .build(); - return allNewReplies.stream().filter(replyVisibilityFilter.isVisible(currentSone)).collect(toSet()); + return allNewReplies.stream().filter(r -> replyVisibilityFilter.isReplyVisible(currentSone, r)).collect(toSet()); } // diff --git a/src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt b/src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt index 64df7b8..9230862 100644 --- a/src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt @@ -11,7 +11,7 @@ interface PostVisibilityFilter { fun isPostVisible(sone: Sone?, post: Post): Boolean @JvmDefault - fun isVisible(currentSone: Sone?): Predicate = - Predicate { p: Post? -> p != null && isPostVisible(currentSone, p) } + fun isVisible(currentSone: Sone?) = + { p: Post? -> p != null && isPostVisible(currentSone, p) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilter.kt b/src/main/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilter.kt index 34018f6..785386a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilter.kt @@ -11,7 +11,7 @@ interface ReplyVisibilityFilter { fun isReplyVisible(sone: Sone?, reply: PostReply): Boolean @JvmDefault - fun isVisible(currentSone: Sone?): Predicate = - Predicate { reply: PostReply -> isReplyVisible(currentSone, reply) } + fun isVisible(currentSone: Sone?) = + { reply: PostReply -> isReplyVisible(currentSone, reply) } } 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 5e3f905..89d6b93 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).test(it) } + .filter { postVisibilityFilter.isVisible(currentSone).invoke(it) } .sortedByDescending { it.time } .let { posts -> posts.paginate(soneRequest.core.preferences.postsPerPage) diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/DefaultPostVisibilityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/DefaultPostVisibilityFilterTest.kt index 8a9a699..e49b6d4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/DefaultPostVisibilityFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/DefaultPostVisibilityFilterTest.kt @@ -111,13 +111,13 @@ class DefaultPostVisibilityFilterTest { @Test fun `predicate will correctly recognize visible post`() { val post = createPost(sone = localSone) - assertThat(postVisibilityFilter.isVisible(null).test(post), equalTo(true)) + assertThat(postVisibilityFilter.isVisible(null).invoke(post), equalTo(true)) } @Test fun `predicate will correctly recognize not visible post`() { val post = createPost(loaded = false) - assertThat(postVisibilityFilter.isVisible(null).test(post), equalTo(false)) + assertThat(postVisibilityFilter.isVisible(null).invoke(post), equalTo(false)) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt index e91bd42..b9f3595 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt @@ -52,13 +52,13 @@ class ReplyVisibilityFilterTest { @Test fun `predicate correctly recognizes visible reply`() { val postReply = createPostReply(post = post) - assertThat(replyVisibilityFilter.isVisible(localSone).test(postReply), equalTo(true)) + assertThat(replyVisibilityFilter.isVisible(localSone).invoke(postReply), equalTo(true)) } @Test fun `predicate correctly recognizes not visible reply`() { val postReply = createPostReply(post = post, time = System.currentTimeMillis() + 100000) - assertThat(replyVisibilityFilter.isVisible(localSone).test(postReply), equalTo(false)) + assertThat(replyVisibilityFilter.isVisible(localSone).invoke(postReply), equalTo(false)) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt index 8c86365..29f4a9c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt @@ -40,7 +40,7 @@ class IndexPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> I @Before fun setupPostVisibilityFilter() { - whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate { true }) + whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn { true } } @Before @@ -107,7 +107,7 @@ class IndexPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> I val followedPosts = listOf(createPost(2500, true), createPost(1500)) whenever(followedSone.posts).thenReturn(followedPosts) whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2")) - whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate { (it?.time ?: 10000) < 2500 }) + whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn { (it?.time ?: 10000) < 2500 } addSone("followed1", followedSone) page.processTemplate(freenetRequest, templateContext) @Suppress("UNCHECKED_CAST") -- 2.7.4