Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Post.java
1 /*
2  * Sone - Post.java - Copyright © 2010–2015 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
66         /**
67          * Returns whether this post has already been loaded.
68          *
69          * @return {@code true} if this post has already been loaded, {@code
70          * false} otherwise
71          */
72         boolean isLoaded();
73
74         /**
75          * Returns the Sone this post belongs to.
76          *
77          * @return The Sone of this post
78          */
79         public Sone getSone();
80
81         /**
82          * Returns the ID of the recipient {@link Sone}, or
83          * {@link Optional#absent()} if this post does not have a recipient.
84          *
85          * @return The ID of the recipient, or {@link Optional#absent()}
86          */
87         public Optional<String> getRecipientId();
88
89         /**
90          * Returns the recipient of this post, if any.
91          *
92          * @return The recipient of this post, or {@link Optional#absent()} if there
93          *         is no recipient
94          */
95         public Optional<Sone> getRecipient();
96
97         /**
98          * Returns the time of the post.
99          *
100          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
101          */
102         public long getTime();
103
104         /**
105          * Returns the text of the post.
106          *
107          * @return The text of the post
108          */
109         public String getText();
110
111         /**
112          * Returns whether this post is known.
113          *
114          * @return {@code true} if this post is known, {@code false} otherwise
115          */
116         public boolean isKnown();
117
118         /**
119          * Sets whether this post is known.
120          *
121          * @param known
122          *            {@code true} if this post is known, {@code false} otherwise
123          * @return This post
124          */
125         public Post setKnown(boolean known);
126
127         /**
128          * Shell for a post that has not yet been loaded.
129          *
130          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
131          *         Roden</a>
132          */
133         public static class EmptyPost implements Post {
134
135                 private final String id;
136
137                 public EmptyPost(String id) {
138                         this.id = id;
139                 }
140
141                 @Override
142                 public String getId() {
143                         return id;
144                 }
145
146                 @Override
147                 public boolean isLoaded() {
148                         return false;
149                 }
150
151                 @Override
152                 public Sone getSone() {
153                         return null;
154                 }
155
156                 @Override
157                 public Optional<String> getRecipientId() {
158                         return absent();
159                 }
160
161                 @Override
162                 public Optional<Sone> getRecipient() {
163                         return absent();
164                 }
165
166                 @Override
167                 public long getTime() {
168                         return 0;
169                 }
170
171                 @Override
172                 public String getText() {
173                         return null;
174                 }
175
176                 @Override
177                 public boolean isKnown() {
178                         return false;
179                 }
180
181                 @Override
182                 public Post setKnown(boolean known) {
183                         return this;
184                 }
185
186         }
187
188 }