4860a0197a1954e85cc60de438133703cfb6b24b
[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.Comparator;
21
22 import com.google.common.base.Optional;
23 import com.google.common.base.Predicate;
24
25 /**
26  * A post is a short message that a user writes in his Sone to let other users
27  * know what is going on.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public interface Post extends Identified {
32
33         /** Comparator for posts, sorts descending by time. */
34         public static final Comparator<Post> TIME_COMPARATOR = new Comparator<Post>() {
35
36                 @Override
37                 public int compare(Post leftPost, Post rightPost) {
38                         return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
39                 }
40
41         };
42
43         /** Filter for posts with timestamps from the future. */
44         public static final Predicate<Post> FUTURE_POSTS_FILTER = new Predicate<Post>() {
45
46                 @Override
47                 public boolean apply(Post post) {
48                         return post.getTime() <= System.currentTimeMillis();
49                 }
50
51         };
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Returns the ID of the post.
59          *
60          * @return The ID of the post
61          */
62         public String getId();
63
64         /**
65          * Returns the Sone this post belongs to.
66          *
67          * @return The Sone of this post
68          */
69         public Sone getSone();
70
71         /**
72          * Returns the ID of the recipient {@link Sone}, or
73          * {@link Optional#absent()} if this post does not have a recipient.
74          *
75          * @return The ID of the recipient, or {@link Optional#absent()}
76          */
77         public Optional<String> getRecipientId();
78
79         /**
80          * Returns the recipient of this post, if any. As this method can return
81          * {@link Optional#absent()} if the post has a recipient which has not yet
82          * been loaded, it is recommended to use {@link #hasRecipient()} to check
83          * for the presence of a recipient.
84          *
85          * @return The recipient of this post, or {@link Optional#absent()} if there
86          *         is no recipient
87          */
88         public Optional<Sone> getRecipient();
89
90         /**
91          * Returns the time of the post.
92          *
93          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
94          */
95         public long getTime();
96
97         /**
98          * Returns the text of the post.
99          *
100          * @return The text of the post
101          */
102         public String getText();
103
104         /**
105          * Returns whether this post is known.
106          *
107          * @return {@code true} if this post is known, {@code false} otherwise
108          */
109         public boolean isKnown();
110
111         /**
112          * Sets whether this post is known.
113          *
114          * @param known
115          *            {@code true} if this post is known, {@code false} otherwise
116          * @return This post
117          */
118         public Post setKnown(boolean known);
119
120 }