🎨 Clean up imports
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / DefaultReplyVisibilityFilter.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
16 /**
17  * Filter that checks a {@link PostReply} for visibility.
18  */
19 @Singleton
20 public class DefaultReplyVisibilityFilter implements ReplyVisibilityFilter {
21
22         private final PostVisibilityFilter postVisibilityFilter;
23
24         @Inject
25         public DefaultReplyVisibilityFilter(@Nonnull PostVisibilityFilter postVisibilityFilter) {
26                 this.postVisibilityFilter = postVisibilityFilter;
27         }
28
29         /**
30          * Checks whether a reply is visible to the given Sone. A reply is not
31          * considered visible if one of the following statements is true:
32          * <ul>
33          * <li>The reply does not have a post.</li>
34          * <li>The reply’s post {@link PostVisibilityFilter#isPostVisible(Sone, Post) is not visible}.</li>
35          * <li>The reply’s {@link PostReply#getTime() time} is in the future.</li>
36          * </ul>
37          * If none of these statements is true the reply is considered visible.
38          *
39          * @param sone
40          *              The Sone that checks for a post’s visibility (may be
41          *              {@code null} to skip Sone-specific checks, such as trust)
42          * @param reply
43          *              The reply to check for visibility
44          * @return {@code true} if the reply is considered visible, {@code false}
45          * otherwise
46          */
47         @Override
48         public boolean isReplyVisible(@Nullable Sone sone, @Nonnull PostReply reply) {
49                 checkNotNull(reply, "reply must not be null");
50                 Optional<Post> post = reply.getPost();
51                 if (!post.isPresent()) {
52                         return false;
53                 }
54                 if (!postVisibilityFilter.isPostVisible(sone, post.get())) {
55                         return false;
56                 }
57                 return reply.getTime() <= System.currentTimeMillis();
58         }
59
60 }