♻️ Move predicate method into interface
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 20 Jun 2020 18:49:59 +0000 (20:49 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 20 Jun 2020 19:44:01 +0000 (21:44 +0200)
build.gradle
src/main/java/net/pterodactylus/sone/notify/DefaultPostVisibilityFilter.java
src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilter.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationFilterTest.kt

index d36b87c..d0b159c 100644 (file)
@@ -26,6 +26,7 @@ tasks.withType(JavaCompile) {
 tasks.withType(KotlinCompile) {
     kotlinOptions {
         jvmTarget = "1.8"
+        freeCompilerArgs += '-Xjvm-default=enable'
     }
 }
 
index a1ecfb7..7479d31 100644 (file)
@@ -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<Post> 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 (file)
index 308074a..0000000
+++ /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<Post> 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 (file)
index 0000000..64df7b8
--- /dev/null
@@ -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<Post?> =
+                       Predicate { p: Post? -> p != null && isPostVisible(currentSone, p) }
+
+}
index 327732e..3a7f47f 100644 (file)
@@ -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<Post> { p -> p != null && isPostVisible(currentSone, p) }
 }
 
 private fun matchThisReply(reply: PostReply) = createReplyVisibilityFilter(showAllPosts) { _, r -> r == reply }