Use a unique ID for posts
[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 static com.google.common.base.Optional.absent;
21
22 import java.util.Comparator;
23
24 import com.google.common.base.Optional;
25 import com.google.common.base.Predicate;
26
27 /**
28  * A post is a short message that a user writes in his Sone to let other users
29  * know what is going on.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public interface Post extends Identified {
34
35         /** Comparator for posts, sorts descending by time. */
36         public static final Comparator<Post> TIME_COMPARATOR = new Comparator<Post>() {
37
38                 @Override
39                 public int compare(Post leftPost, Post rightPost) {
40                         return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
41                 }
42
43         };
44
45         /** Filter for posts with timestamps from the future. */
46         public static final Predicate<Post> FUTURE_POSTS_FILTER = new Predicate<Post>() {
47
48                 @Override
49                 public boolean apply(Post post) {
50                         return (post != null) && (post.getTime() <= System.currentTimeMillis());
51                 }
52
53         };
54
55         //
56         // ACCESSORS
57         //
58
59         /**
60          * Returns the ID of the post.
61          *
62          * @return The ID of the post
63          */
64         public String getId();
65         String getInternalId();
66
67         /**
68          * Returns whether this post has already been loaded.
69          *
70          * @return {@code true} if this post has already been loaded, {@code
71          * false} otherwise
72          */
73         boolean isLoaded();
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          * @param known
123          *            {@code true} if this post is known, {@code false} otherwise
124          * @return This post
125          */
126         public Post setKnown(boolean known);
127
128         /**
129          * Shell for a post that has not yet been loaded.
130          *
131          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
132          *         Roden</a>
133          */
134         public static class EmptyPost implements Post {
135
136                 private final String id;
137
138                 public EmptyPost(String id) {
139                         this.id = id;
140                 }
141
142                 @Override
143                 public String getId() {
144                         return id;
145                 }
146
147                 @Override
148                 public String getInternalId() {
149                         return id;
150                 }
151
152                 @Override
153                 public boolean isLoaded() {
154                         return false;
155                 }
156
157                 @Override
158                 public Sone getSone() {
159                         return null;
160                 }
161
162                 @Override
163                 public Optional<String> getRecipientId() {
164                         return absent();
165                 }
166
167                 @Override
168                 public Optional<Sone> getRecipient() {
169                         return absent();
170                 }
171
172                 @Override
173                 public long getTime() {
174                         return 0;
175                 }
176
177                 @Override
178                 public String getText() {
179                         return null;
180                 }
181
182                 @Override
183                 public boolean isKnown() {
184                         return false;
185                 }
186
187                 @Override
188                 public Post setKnown(boolean known) {
189                         return this;
190                 }
191
192         }
193
194 }