🎨 Use Kotlin arrow type instead of Predicate
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jun 2020 19:51:19 +0000 (21:51 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jun 2020 19:51:19 +0000 (21:51 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt
src/main/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilter.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt
src/test/kotlin/net/pterodactylus/sone/notify/DefaultPostVisibilityFilterTest.kt
src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/IndexPageTest.kt

index 69077ed..4650625 100644 (file)
@@ -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());
        }
 
        //
index 64df7b8..9230862 100644 (file)
@@ -11,7 +11,7 @@ interface PostVisibilityFilter {
        fun isPostVisible(sone: Sone?, post: Post): Boolean
 
        @JvmDefault
-       fun isVisible(currentSone: Sone?): Predicate<Post?> =
-                       Predicate { p: Post? -> p != null && isPostVisible(currentSone, p) }
+       fun isVisible(currentSone: Sone?) =
+                       { p: Post? -> p != null && isPostVisible(currentSone, p) }
 
 }
index 34018f6..785386a 100644 (file)
@@ -11,7 +11,7 @@ interface ReplyVisibilityFilter {
        fun isReplyVisible(sone: Sone?, reply: PostReply): Boolean
 
        @JvmDefault
-       fun isVisible(currentSone: Sone?): Predicate<PostReply> =
-                       Predicate { reply: PostReply -> isReplyVisible(currentSone, reply) }
+       fun isVisible(currentSone: Sone?) =
+                       { reply: PostReply -> isReplyVisible(currentSone, reply) }
 
 }
index 5e3f905..89d6b93 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).test(it) }
+                               .filter { postVisibilityFilter.isVisible(currentSone).invoke(it) }
                                .sortedByDescending { it.time }
                                .let { posts ->
                                        posts.paginate(soneRequest.core.preferences.postsPerPage)
index 8a9a699..e49b6d4 100644 (file)
@@ -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))
        }
 
 }
index e91bd42..b9f3595 100644 (file)
@@ -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))
        }
 
 }
index 8c86365..29f4a9c 100644 (file)
@@ -40,7 +40,7 @@ class IndexPageTest : WebPageTest({ webInterface, loaders, templateRenderer -> I
 
        @Before
        fun setupPostVisibilityFilter() {
-               whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { 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<Post> { (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")