Store the “known” status of a post in the database.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Post.java
1 /*
2  * Sone - Post.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.Set;
24
25 import com.google.common.base.Function;
26 import com.google.common.base.Optional;
27 import com.google.common.base.Predicate;
28
29 /**
30  * A post is a short message that a user writes in his Sone to let other users
31  * know what is going on.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public interface Post extends Identified {
36
37         /** Comparator for posts, sorts descending by time. */
38         public static final Comparator<Post> TIME_COMPARATOR = new Comparator<Post>() {
39
40                 @Override
41                 public int compare(Post leftPost, Post rightPost) {
42                         return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
43                 }
44
45         };
46
47         /** Filter for posts with timestamps from the future. */
48         public static final Predicate<Post> FUTURE_POSTS_FILTER = new Predicate<Post>() {
49
50                 @Override
51                 public boolean apply(Post post) {
52                         return (post == null) ? false : post.getTime() <= System.currentTimeMillis();
53                 }
54
55         };
56
57         public static final Function<Post, List<PostReply>> TO_REPLIES = new Function<Post, List<PostReply>>() {
58                 @Override
59                 public List<PostReply> apply(Post post) {
60                         return (post == null) ? Collections.<PostReply>emptyList() : post.getReplies();
61                 }
62         };
63
64         //
65         // ACCESSORS
66         //
67
68         /**
69          * Returns the ID of the post.
70          *
71          * @return The ID of the post
72          */
73         public String getId();
74
75         /**
76          * Returns the Sone this post belongs to.
77          *
78          * @return The Sone of this post
79          */
80         public Sone getSone();
81
82         /**
83          * Returns the ID of the recipient {@link Sone}, or
84          * {@link Optional#absent()} if this post does not have a recipient.
85          *
86          * @return The ID of the recipient, or {@link Optional#absent()}
87          */
88         public Optional<String> getRecipientId();
89
90         /**
91          * Returns the recipient of this post, if any.
92          *
93          * @return The recipient of this post, or {@link Optional#absent()} if there
94          *         is no recipient
95          */
96         public Optional<Sone> getRecipient();
97
98         /**
99          * Returns the time of the post.
100          *
101          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
102          */
103         public long getTime();
104
105         /**
106          * Returns the text of the post.
107          *
108          * @return The text of the post
109          */
110         public String getText();
111
112         /**
113          * Returns whether this post is known.
114          *
115          * @return {@code true} if this post is known, {@code false} otherwise
116          */
117         public boolean isKnown();
118
119         /**
120          * Sets whether this post is known.
121          *
122          * @return This post
123          */
124         public Post setKnown();
125
126         void like(Sone localSone);
127         void unlike(Sone localSone);
128
129         boolean isLiked(Sone sone);
130         Set<Sone> getLikes();
131
132         List<PostReply> getReplies();
133
134 }