Override Object.toString().
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Post.java
1 /*
2  * FreenetSone - StatusUpdate.java - Copyright © 2010 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.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.UUID;
27
28 /**
29  * A post is a short message that a user writes in his Sone to let other users
30  * know what is going on.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class Post {
35
36         /** The GUID of the post. */
37         private final UUID id;
38
39         /** The Sone this post belongs to. */
40         private final Sone sone;
41
42         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
43         private final long time;
44
45         /** The text of the post. */
46         private final String text;
47
48         /** The replies that have been loaded for this post. */
49         private final Set<Reply> replies = new HashSet<Reply>();
50
51         /**
52          * Creates a new post.
53          *
54          * @param sone
55          *            The Sone this post belongs to
56          * @param text
57          *            The text of the post
58          */
59         public Post(Sone sone, String text) {
60                 this(sone, System.currentTimeMillis(), text);
61         }
62
63         /**
64          * Creates a new post.
65          *
66          * @param sone
67          *            The Sone this post belongs to
68          * @param time
69          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
70          * @param text
71          *            The text of the post
72          */
73         public Post(Sone sone, long time, String text) {
74                 this(UUID.randomUUID(), sone, time, text);
75         }
76
77         /**
78          * Creates a new post.
79          *
80          * @param id
81          *            The ID of the post
82          * @param sone
83          *            The Sone this post belongs to
84          * @param time
85          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
86          * @param text
87          *            The text of the post
88          */
89         public Post(UUID id, Sone sone, long time, String text) {
90                 this.id = id;
91                 this.sone = sone;
92                 this.time = time;
93                 this.text = text;
94         }
95
96         //
97         // ACCESSORS
98         //
99
100         /**
101          * Returns the ID of the post.
102          *
103          * @return The ID of the post
104          */
105         public String getId() {
106                 return id.toString();
107         }
108
109         /**
110          * Returns the Sone this post belongs to.
111          *
112          * @return The Sone of this post
113          */
114         public Sone getSone() {
115                 return sone;
116         }
117
118         /**
119          * Returns the time of the post.
120          *
121          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
122          */
123         public long getTime() {
124                 return time;
125         }
126
127         /**
128          * Returns the text of the post.
129          *
130          * @return The text of the post
131          */
132         public String getText() {
133                 return text;
134         }
135
136         /**
137          * Returns all replies to this post in unspecified order.
138          *
139          * @return All replies to this post
140          */
141         public List<Reply> getReplies() {
142                 List<Reply> sortedReplies = new ArrayList<Reply>(replies);
143                 Collections.sort(sortedReplies, new Comparator<Reply>() {
144
145                         @Override
146                         public int compare(Reply leftReply, Reply rightReply) {
147                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
148                         }
149
150                 });
151                 return sortedReplies;
152         }
153
154         /**
155          * Adds a reply to this post. The reply will not be added if its
156          * {@link Reply#getPost() post} is not equal to this post.
157          *
158          * @param reply
159          *            The reply to add
160          */
161         public void addReply(Reply reply) {
162                 if (reply.getPost().equals(this)) {
163                         replies.add(reply);
164                 }
165         }
166
167         /**
168          * Removes a reply from this post.
169          *
170          * @param reply
171          *            The reply to remove
172          */
173         public void removeReply(Reply reply) {
174                 if (reply.getPost().equals(this)) {
175                         replies.remove(reply);
176                 }
177         }
178
179         //
180         // OBJECT METHODS
181         //
182
183         /**
184          * {@inheritDoc}
185          */
186         @Override
187         public int hashCode() {
188                 return id.hashCode() ^ sone.hashCode() ^ (int) (time >> 32) ^ (int) (time & 0xffffffff) ^ text.hashCode();
189         }
190
191         /**
192          * {@inheritDoc}
193          */
194         @Override
195         public boolean equals(Object object) {
196                 if (!(object instanceof Post)) {
197                         return false;
198                 }
199                 Post post = (Post) object;
200                 return post.id.equals(id) && post.sone.equals(sone) && (post.time == time) && post.text.equals(text);
201         }
202
203         /**
204          * {@inheritDoc}
205          */
206         @Override
207         public String toString() {
208                 return getClass().getName() + "[id=" + getId() + ",sone=" + getSone() + ",time=" + getTime() + ",text=" + getText() + "]";
209         }
210
211 }