Turn post into an interface, add default implementation.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / PostImpl.java
1 /*
2  * Sone - PostImpl.java - Copyright © 2010–2012 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          * {@inheritDoc}
128          */
129         @Override
130         public PostImpl setSone(Sone sone) {
131                 this.sone = sone;
132                 return this;
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         @Override
139         public Sone getRecipient() {
140                 return recipient;
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public PostImpl setRecipient(Sone recipient) {
148                 if (!sone.equals(recipient)) {
149                         this.recipient = recipient;
150                 }
151                 return this;
152         }
153
154         /**
155          * {@inheritDoc}
156          */
157         @Override
158         public long getTime() {
159                 return time;
160         }
161
162         /**
163          * {@inheritDoc}
164          */
165         @Override
166         public PostImpl setTime(long time) {
167                 this.time = time;
168                 return this;
169         }
170
171         /**
172          * {@inheritDoc}
173          */
174         @Override
175         public String getText() {
176                 return text;
177         }
178
179         /**
180          * {@inheritDoc}
181          */
182         @Override
183         public PostImpl setText(String text) {
184                 this.text = text;
185                 return this;
186         }
187
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         public boolean isKnown() {
193                 return known;
194         }
195
196         /**
197          * {@inheritDoc}
198          */
199         @Override
200         public PostImpl setKnown(boolean known) {
201                 this.known = known;
202                 return this;
203         }
204
205         //
206         // OBJECT METHODS
207         //
208
209         /**
210          * {@inheritDoc}
211          */
212         @Override
213         public int hashCode() {
214                 return id.hashCode();
215         }
216
217         /**
218          * {@inheritDoc}
219          */
220         @Override
221         public boolean equals(Object object) {
222                 if (!(object instanceof PostImpl)) {
223                         return false;
224                 }
225                 PostImpl post = (PostImpl) object;
226                 return post.id.equals(id);
227         }
228
229         /**
230          * {@inheritDoc}
231          */
232         @Override
233         public String toString() {
234                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
235         }
236
237 }