Rename list notification filters class
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ReplyVisibilityFilter.java
1 package net.pterodactylus.sone.notify;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4
5 import javax.annotation.Nonnull;
6 import javax.annotation.Nullable;
7 import javax.inject.Inject;
8 import javax.inject.Singleton;
9
10 import net.pterodactylus.sone.data.Post;
11 import net.pterodactylus.sone.data.PostReply;
12 import net.pterodactylus.sone.data.Sone;
13
14 import com.google.common.base.Optional;
15 import com.google.common.base.Predicate;
16
17 /**
18  * Filter that checks a {@link PostReply} for visibility.
19  *
20  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21  */
22 @Singleton
23 public class ReplyVisibilityFilter {
24
25         private final PostVisibilityFilter postVisibilityFilter;
26
27         @Inject
28         public ReplyVisibilityFilter(@Nonnull PostVisibilityFilter postVisibilityFilter) {
29                 this.postVisibilityFilter = postVisibilityFilter;
30         }
31
32         /**
33          * Checks whether a reply is visible to the given Sone. A reply is not
34          * considered visible if one of the following statements is true:
35          * <ul>
36          * <li>The reply does not have a post.</li>
37          * <li>The reply’s post {@link PostVisibilityFilter#isPostVisible(Sone, Post) is not visible}.</li>
38          * <li>The reply’s {@link PostReply#getTime() time} is in the future.</li>
39          * </ul>
40          * If none of these statements is true the reply is considered visible.
41          *
42          * @param sone
43          *              The Sone that checks for a post’s visibility (may be
44          *              {@code null} to skip Sone-specific checks, such as trust)
45          * @param reply
46          *              The reply to check for visibility
47          * @return {@code true} if the reply is considered visible, {@code false}
48          * otherwise
49          */
50         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         @Nonnull
63         public Predicate<PostReply> isVisible(@Nullable final Sone currentSone) {
64                 return new Predicate<PostReply>() {
65                         @Nonnull
66                         @Override
67                         public boolean apply(@Nullable PostReply postReply) {
68                                 return isReplyVisible(currentSone, postReply);
69                         }
70                 };
71         }
72
73 }