991418595864c6dfa0272383a86ad15c40e57e41
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Post.java
1 /*
2  * Sone - Post.java - Copyright © 2010–2020 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
26 /**
27  * A post is a short message that a user writes in his Sone to let other users
28  * know what is going on.
29  */
30 public interface Post extends Identified {
31
32         /** Comparator for posts, sorts descending by time. */
33         public static final Comparator<Post> NEWEST_FIRST = new Comparator<Post>() {
34
35                 @Override
36                 public int compare(Post leftPost, Post rightPost) {
37                         return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
38                 }
39
40         };
41
42         //
43         // ACCESSORS
44         //
45
46         /**
47          * Returns the ID of the post.
48          *
49          * @return The ID of the post
50          */
51         public String getId();
52
53         /**
54          * Returns whether this post has already been loaded.
55          *
56          * @return {@code true} if this post has already been loaded, {@code
57          * false} otherwise
58          */
59         boolean isLoaded();
60
61         /**
62          * Returns the Sone this post belongs to.
63          *
64          * @return The Sone of this post
65          */
66         public Sone getSone();
67
68         /**
69          * Returns the ID of the recipient {@link Sone}, or
70          * {@link Optional#absent()} if this post does not have a recipient.
71          *
72          * @return The ID of the recipient, or {@link Optional#absent()}
73          */
74         public Optional<String> getRecipientId();
75
76         /**
77          * Returns the recipient of this post, if any.
78          *
79          * @return The recipient of this post, or {@link Optional#absent()} if there
80          *         is no recipient
81          */
82         public Optional<Sone> getRecipient();
83
84         /**
85          * Returns the time of the post.
86          *
87          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
88          */
89         public long getTime();
90
91         /**
92          * Returns the text of the post.
93          *
94          * @return The text of the post
95          */
96         public String getText();
97
98         /**
99          * Returns whether this post is known.
100          *
101          * @return {@code true} if this post is known, {@code false} otherwise
102          */
103         public boolean isKnown();
104
105         /**
106          * Sets whether this post is known.
107          *
108          * @param known
109          *            {@code true} if this post is known, {@code false} otherwise
110          * @return This post
111          */
112         public Post setKnown(boolean known);
113
114         /**
115          * Shell for a post that has not yet been loaded.
116          */
117         public static class EmptyPost implements Post {
118
119                 private final String id;
120
121                 public EmptyPost(String id) {
122                         this.id = id;
123                 }
124
125                 @Override
126                 public String getId() {
127                         return id;
128                 }
129
130                 @Override
131                 public boolean isLoaded() {
132                         return false;
133                 }
134
135                 @Override
136                 public Sone getSone() {
137                         return null;
138                 }
139
140                 @Override
141                 public Optional<String> getRecipientId() {
142                         return absent();
143                 }
144
145                 @Override
146                 public Optional<Sone> getRecipient() {
147                         return absent();
148                 }
149
150                 @Override
151                 public long getTime() {
152                         return 0;
153                 }
154
155                 @Override
156                 public String getText() {
157                         return null;
158                 }
159
160                 @Override
161                 public boolean isKnown() {
162                         return false;
163                 }
164
165                 @Override
166                 public Post setKnown(boolean known) {
167                         return this;
168                 }
169
170         }
171
172 }