Store replies in post.
[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.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.Set;
24 import java.util.UUID;
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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class Post {
33
34         /** The GUID of the post. */
35         private final UUID id;
36
37         /** The Sone this post belongs to. */
38         private final Sone sone;
39
40         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
41         private final long time;
42
43         /** The text of the post. */
44         private final String text;
45
46         /** The replies that have been loaded for this post. */
47         private final Set<Reply> replies = new HashSet<Reply>();
48
49         /**
50          * Creates a new post.
51          *
52          * @param sone
53          *            The Sone this post belongs to
54          * @param text
55          *            The text of the post
56          */
57         public Post(Sone sone, String text) {
58                 this(sone, System.currentTimeMillis(), text);
59         }
60
61         /**
62          * Creates a new post.
63          *
64          * @param sone
65          *            The Sone this post belongs to
66          * @param time
67          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
68          * @param text
69          *            The text of the post
70          */
71         public Post(Sone sone, long time, String text) {
72                 this(UUID.randomUUID(), sone, time, text);
73         }
74
75         /**
76          * Creates a new post.
77          *
78          * @param id
79          *            The ID of the post
80          * @param sone
81          *            The Sone this post belongs to
82          * @param time
83          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
84          * @param text
85          *            The text of the post
86          */
87         public Post(UUID id, Sone sone, long time, String text) {
88                 this.id = id;
89                 this.sone = sone;
90                 this.time = time;
91                 this.text = text;
92         }
93
94         //
95         // ACCESSORS
96         //
97
98         /**
99          * Returns the ID of the post.
100          *
101          * @return The ID of the post
102          */
103         public String getId() {
104                 return id.toString();
105         }
106
107         /**
108          * Returns the Sone this post belongs to.
109          *
110          * @return The Sone of this post
111          */
112         public Sone getSone() {
113                 return sone;
114         }
115
116         /**
117          * Returns the time of the post.
118          *
119          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
120          */
121         public long getTime() {
122                 return time;
123         }
124
125         /**
126          * Returns the text of the post.
127          *
128          * @return The text of the post
129          */
130         public String getText() {
131                 return text;
132         }
133
134         /**
135          * Returns all replies to this post in unspecified order.
136          *
137          * @return All replies to this post
138          */
139         public Collection<Reply> getReplies() {
140                 return Collections.unmodifiableSet(replies);
141         }
142
143         /**
144          * Adds a reply to this post. The reply will not be added if its
145          * {@link Reply#getPost() post} is not equal to this post.
146          *
147          * @param reply
148          *            The reply to add
149          */
150         public void addReply(Reply reply) {
151                 if (reply.getPost().equals(this)) {
152                         replies.add(reply);
153                 }
154         }
155
156         /**
157          * Removes a reply from this post.
158          *
159          * @param reply
160          *            The reply to remove
161          */
162         public void removeReply(Reply reply) {
163                 if (reply.getPost().equals(this)) {
164                         replies.remove(reply);
165                 }
166         }
167
168         //
169         // OBJECT METHODS
170         //
171
172         /**
173          * {@inheritDoc}
174          */
175         @Override
176         public int hashCode() {
177                 return id.hashCode();
178         }
179
180         /**
181          * {@inheritDoc}
182          */
183         @Override
184         public boolean equals(Object object) {
185                 if (!(object instanceof Post)) {
186                         return false;
187                 }
188                 return ((Post) object).id.equals(id);
189         }
190
191 }