🎨 Replace future reply filter with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 22 Feb 2020 22:44:47 +0000 (23:44 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 22 Feb 2020 23:15:09 +0000 (00:15 +0100)
src/main/java/net/pterodactylus/sone/data/Reply.java
src/main/kotlin/net/pterodactylus/sone/data/Reply.kt
src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt
src/test/kotlin/net/pterodactylus/sone/data/ReplyTest.kt

index 705b1e4..918fd01 100644 (file)
 
 package net.pterodactylus.sone.data;
 
-import java.util.Comparator;
-
-import com.google.common.base.Predicate;
-
 /**
  * Defines methods common for all replies.
  *
@@ -29,19 +25,6 @@ import com.google.common.base.Predicate;
  */
 public interface Reply<T extends Reply<T>> extends Identified {
 
-       /** Filter for replies with timestamps from the future. */
-       public static final Predicate<Reply<?>> FUTURE_REPLY_FILTER = new Predicate<Reply<?>>() {
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public boolean apply(Reply<?> reply) {
-                       return (reply != null) && (reply.getTime() <= System.currentTimeMillis());
-               }
-
-       };
-
        /**
         * Returns the ID of the reply.
         *
index 9a3ec64..cfc940a 100644 (file)
@@ -25,3 +25,10 @@ import java.util.Comparator.comparing
 @get:JvmName("newestReplyFirst")
 val newestReplyFirst: Comparator<Reply<*>> =
                comparing(Reply<*>::getTime).reversed()
+
+/**
+ * Predicate that returns whether a reply is _not_ from the future,
+ * i.e. whether it should be visible now.
+ */
+val noFutureReply: (Reply<*>) -> Boolean =
+               { it.getTime() <= System.currentTimeMillis() }
index ab7f6ba..42239aa 100644 (file)
@@ -51,5 +51,5 @@ class PostAccessor(private val core: Core) : ReflectionAccessor() {
 
 }
 
-private fun Core.getReplies(post: Post) = getReplies(post.id).filter { Reply.FUTURE_REPLY_FILTER.apply(it) }
+private fun Core.getReplies(post: Post) = getReplies(post.id).filter(noFutureReply)
 private val TemplateContext?.currentSone: Sone? get() = this?.get("currentSone") as? Sone
index 7534bb4..9bbc3e5 100644 (file)
@@ -88,7 +88,7 @@ class SearchPage(webInterface: WebInterface, loaders: Loaders, templateRenderer:
 
        private fun Post.allText(soneNameCache: (Sone) -> String, getReplies: (String) -> Collection<PostReply>) =
                        (text + recipient.orNull()?.let { " ${soneNameCache(it)}" } + getReplies(id)
-                                       .filter { PostReply.FUTURE_REPLY_FILTER.apply(it) }
+                                       .filter(noFutureReply)
                                        .map { "${soneNameCache(it.sone)} ${it.text}" }.joinToString(" ", " ")).toLowerCase()
 
        private fun Iterable<Phrase>.indicesFor(text: String, predicate: (Phrase) -> Boolean) =
index 3289b7d..d7f7138 100644 (file)
@@ -22,6 +22,7 @@ import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
 import org.hamcrest.Matchers.greaterThan
 import org.hamcrest.Matchers.lessThan
+import java.util.concurrent.TimeUnit.DAYS
 import kotlin.test.Test
 
 class ReplyTest {
@@ -47,4 +48,16 @@ class ReplyTest {
                assertThat(newestReplyFirst.compare(reply1, reply2), equalTo(0))
        }
 
+       @Test
+       fun `noFutureReply filter recognizes reply from the future`() {
+               val futureReply = emptyPostReply(time = System.currentTimeMillis() + DAYS.toMillis(1))
+               assertThat(noFutureReply(futureReply), equalTo(false))
+       }
+
+       @Test
+       fun `noFutureReply filter recognizes reply from the present`() {
+               val futureReply = emptyPostReply(time = System.currentTimeMillis())
+               assertThat(noFutureReply(futureReply), equalTo(true))
+       }
+
 }