From 30223db6e2ad13491be1b46c6c13b41fbcc3937f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 20 Jun 2020 20:49:59 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Move=20predicate=20method?= =?utf8?q?=20into=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../sone/notify/DefaultPostVisibilityFilter.java | 11 ----------- .../sone/notify/PostVisibilityFilter.java | 20 -------------------- .../sone/notify/PostVisibilityFilter.kt | 17 +++++++++++++++++ .../sone/notify/ListNotificationFilterTest.kt | 1 - 5 files changed, 18 insertions(+), 32 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt diff --git a/build.gradle b/build.gradle index d36b87c..d0b159c 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,7 @@ tasks.withType(JavaCompile) { tasks.withType(KotlinCompile) { kotlinOptions { jvmTarget = "1.8" + freeCompilerArgs += '-Xjvm-default=enable' } } diff --git a/src/main/java/net/pterodactylus/sone/notify/DefaultPostVisibilityFilter.java b/src/main/java/net/pterodactylus/sone/notify/DefaultPostVisibilityFilter.java index a1ecfb7..7479d31 100644 --- a/src/main/java/net/pterodactylus/sone/notify/DefaultPostVisibilityFilter.java +++ b/src/main/java/net/pterodactylus/sone/notify/DefaultPostVisibilityFilter.java @@ -1,9 +1,5 @@ package net.pterodactylus.sone.notify; -import java.util.function.Predicate; - -import static com.google.common.base.Preconditions.checkNotNull; - import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Singleton; @@ -53,7 +49,6 @@ public class DefaultPostVisibilityFilter implements PostVisibilityFilter { */ @Override public boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) { - checkNotNull(post, "post must not be null"); if (!post.isLoaded()) { return false; } @@ -83,10 +78,4 @@ public class DefaultPostVisibilityFilter implements PostVisibilityFilter { return post.getTime() <= System.currentTimeMillis(); } - @Override - @Nonnull - public Predicate isVisible(@Nullable final Sone currentSone) { - return post -> (post != null) && isPostVisible(currentSone, post); - } - } diff --git a/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java b/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java deleted file mode 100644 index 308074a..0000000 --- a/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.pterodactylus.sone.notify; - -import java.util.function.Predicate; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; - -import com.google.inject.ImplementedBy; - -@ImplementedBy(DefaultPostVisibilityFilter.class) -public interface PostVisibilityFilter { - - boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post); - - @Nonnull - Predicate isVisible(@Nullable Sone currentSone); - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt b/src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt new file mode 100644 index 0000000..64df7b8 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt @@ -0,0 +1,17 @@ +package net.pterodactylus.sone.notify + +import com.google.inject.ImplementedBy +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.Sone +import java.util.function.Predicate + +@ImplementedBy(DefaultPostVisibilityFilter::class) +interface PostVisibilityFilter { + + fun isPostVisible(sone: Sone?, post: Post): Boolean + + @JvmDefault + fun isVisible(currentSone: Sone?): Predicate = + Predicate { p: Post? -> p != null && isPostVisible(currentSone, p) } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt index 327732e..3a7f47f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt @@ -215,7 +215,6 @@ private val showNoPosts = createPostVisibilityFilter { _, _ -> false } private fun createPostVisibilityFilter(visible: (Sone?, Post) -> Boolean) = object : PostVisibilityFilter { override fun isPostVisible(sone: Sone?, post: Post) = visible(sone, post) - override fun isVisible(currentSone: Sone?) = Predicate { p -> p != null && isPostVisible(currentSone, p) } } private fun matchThisReply(reply: PostReply) = createReplyVisibilityFilter(showAllPosts) { _, r -> r == reply } -- 2.7.4