package net.pterodactylus.sone.data;
-import java.util.Comparator;
-
-import com.google.common.base.Predicate;
-
/**
* Defines methods common for all replies.
*
*/
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.
*
@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() }
}
-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
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) =
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 {
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))
+ }
+
}