♻️ Supply default implementation for predicate
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / DefaultReplyVisibilityFilter.java
1 package net.pterodactylus.sone.notify;
2
3 import java.util.function.Predicate;
4
5 import static com.google.common.base.Preconditions.checkNotNull;
6
7 import javax.annotation.Nonnull;
8 import javax.annotation.Nullable;
9 import javax.inject.Inject;
10 import javax.inject.Singleton;
11
12 import net.pterodactylus.sone.data.Post;
13 import net.pterodactylus.sone.data.PostReply;
14 import net.pterodactylus.sone.data.Sone;
15
16 import com.google.common.base.Optional;
17
18 /**
19  * Filter that checks a {@link PostReply} for visibility.
20  */
21 @Singleton
22 public class DefaultReplyVisibilityFilter implements ReplyVisibilityFilter {
23
24         private final PostVisibilityFilter postVisibilityFilter;
25
26         @Inject
27         public DefaultReplyVisibilityFilter(@Nonnull PostVisibilityFilter postVisibilityFilter) {
28                 this.postVisibilityFilter = postVisibilityFilter;
29         }
30
31         /**
32          * Checks whether a reply is visible to the given Sone. A reply is not
33          * considered visible if one of the following statements is true:
34          * <ul>
35          * <li>The reply does not have a post.</li>
36          * <li>The reply’s post {@link PostVisibilityFilter#isPostVisible(Sone, Post) is not visible}.</li>
37          * <li>The reply’s {@link PostReply#getTime() time} is in the future.</li>
38          * </ul>
39          * If none of these statements is true the reply is considered visible.
40          *
41          * @param sone
42          *              The Sone that checks for a post’s visibility (may be
43          *              {@code null} to skip Sone-specific checks, such as trust)
44          * @param reply
45          *              The reply to check for visibility
46          * @return {@code true} if the reply is considered visible, {@code false}
47          * otherwise
48          */
49         @Override
50         public boolean isReplyVisible(@Nullable Sone sone, @Nonnull PostReply reply) {
51                 checkNotNull(reply, "reply must not be null");
52                 Optional<Post> post = reply.getPost();
53                 if (!post.isPresent()) {
54                         return false;
55                 }
56                 if (!postVisibilityFilter.isPostVisible(sone, post.get())) {
57                         return false;
58                 }
59                 return reply.getTime() <= System.currentTimeMillis();
60         }
61
62 }