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