Merge branch 'less-critical' into run
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / PostImpl.java
1 /*
2  * Sone - PostImpl.java - Copyright © 2010–2013 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.impl;
19
20 import java.util.UUID;
21
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.Sone;
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 PostImpl implements Post {
32
33         /** The GUID of the post. */
34         private final UUID id;
35
36         /** The Sone this post belongs to. */
37         private volatile Sone sone;
38
39         /** The Sone of the recipient. */
40         private volatile Sone recipient;
41
42         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
43         private volatile long time;
44
45         /** The text of the post. */
46         private volatile String text;
47
48         /** Whether the post is known. */
49         private volatile boolean known;
50
51         /**
52          * Creates a new post.
53          *
54          * @param id
55          *            The ID of the post
56          */
57         public PostImpl(String id) {
58                 this(id, null, 0, null);
59         }
60
61         /**
62          * Creates a new post.
63          *
64          * @param sone
65          *            The Sone this post belongs to
66          * @param text
67          *            The text of the post
68          */
69         public PostImpl(Sone sone, String text) {
70                 this(sone, System.currentTimeMillis(), text);
71         }
72
73         /**
74          * Creates a new post.
75          *
76          * @param sone
77          *            The Sone this post belongs to
78          * @param time
79          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
80          * @param text
81          *            The text of the post
82          */
83         public PostImpl(Sone sone, long time, String text) {
84                 this(UUID.randomUUID().toString(), sone, time, text);
85         }
86
87         /**
88          * Creates a new post.
89          *
90          * @param id
91          *            The ID of the post
92          * @param sone
93          *            The Sone this post belongs to
94          * @param time
95          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
96          * @param text
97          *            The text of the post
98          */
99         public PostImpl(String id, Sone sone, long time, String text) {
100                 this.id = UUID.fromString(id);
101                 this.sone = sone;
102                 this.time = time;
103                 this.text = text;
104         }
105
106         //
107         // ACCESSORS
108         //
109
110         /**
111          * {@inheritDoc}
112          */
113         @Override
114         public String getId() {
115                 return id.toString();
116         }
117
118         /**
119          * {@inheritDoc}
120          */
121         @Override
122         public Sone getSone() {
123                 return sone;
124         }
125
126         /**
127          * Sets the Sone of this post.
128          *
129          * @param sone
130          *            The Sone of this post
131          * @return This post (for method chaining)
132          */
133         public PostImpl setSone(Sone sone) {
134                 this.sone = sone;
135                 return this;
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         @Override
142         public Sone getRecipient() {
143                 return recipient;
144         }
145
146         /**
147          * Sets the recipient of this post.
148          *
149          * @param recipient
150          *            The recipient of this post, or {@code null}
151          * @return This post (for method chaining)
152          */
153         public PostImpl setRecipient(Sone recipient) {
154                 if (!sone.equals(recipient)) {
155                         this.recipient = recipient;
156                 }
157                 return this;
158         }
159
160         /**
161          * {@inheritDoc}
162          */
163         @Override
164         public long getTime() {
165                 return time;
166         }
167
168         /**
169          * Sets the time of this post.
170          *
171          * @param time
172          *            The time of this post (in milliseconds since Jan 1, 1970 UTC)
173          * @return This post (for method chaining)
174          */
175         public PostImpl setTime(long time) {
176                 this.time = time;
177                 return this;
178         }
179
180         /**
181          * {@inheritDoc}
182          */
183         @Override
184         public String getText() {
185                 return text;
186         }
187
188         /**
189          * Sets the text of this post.
190          *
191          * @param text
192          *            The text of this post
193          * @return This post (for method chaining)
194          */
195         public PostImpl setText(String text) {
196                 this.text = text;
197                 return this;
198         }
199
200         /**
201          * {@inheritDoc}
202          */
203         @Override
204         public boolean isKnown() {
205                 return known;
206         }
207
208         /**
209          * {@inheritDoc}
210          */
211         @Override
212         public PostImpl setKnown(boolean known) {
213                 this.known = known;
214                 return this;
215         }
216
217         //
218         // OBJECT METHODS
219         //
220
221         /**
222          * {@inheritDoc}
223          */
224         @Override
225         public int hashCode() {
226                 return id.hashCode();
227         }
228
229         /**
230          * {@inheritDoc}
231          */
232         @Override
233         public boolean equals(Object object) {
234                 if (!(object instanceof PostImpl)) {
235                         return false;
236                 }
237                 PostImpl post = (PostImpl) object;
238                 return post.id.equals(id);
239         }
240
241         /**
242          * {@inheritDoc}
243          */
244         @Override
245         public String toString() {
246                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
247         }
248
249 }