Add abstract base class for replies, move post reply into its own class.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / PostReply.java
1 /*
2  * Sone - PostReply.java - Copyright © 2010–2011 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.UUID;
21
22 /**
23  * A reply is like a {@link Post} but can never be posted on its own, it always
24  * refers to another {@link Post}.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class PostReply extends Reply<PostReply> {
29
30         /** The ID of the reply. */
31         private final UUID id;
32
33         /** The Sone that posted this reply. */
34         private volatile Sone sone;
35
36         /** The Post this reply refers to. */
37         private volatile Post post;
38
39         /** The time of the reply. */
40         private volatile long time;
41
42         /** The text of the reply. */
43         private volatile String text;
44
45         /**
46          * Creates a new reply.
47          *
48          * @param id
49          *            The ID of the reply
50          */
51         public PostReply(String id) {
52                 this(id, null, null, 0, null);
53         }
54
55         /**
56          * Creates a new reply.
57          *
58          * @param sone
59          *            The sone that posted the reply
60          * @param post
61          *            The post to reply to
62          * @param text
63          *            The text of the reply
64          */
65         public PostReply(Sone sone, Post post, String text) {
66                 this(sone, post, System.currentTimeMillis(), text);
67         }
68
69         /**
70          * Creates a new reply-
71          *
72          * @param sone
73          *            The sone that posted the reply
74          * @param post
75          *            The post to reply to
76          * @param time
77          *            The time of the reply
78          * @param text
79          *            The text of the reply
80          */
81         public PostReply(Sone sone, Post post, long time, String text) {
82                 this(UUID.randomUUID().toString(), sone, post, time, text);
83         }
84
85         /**
86          * Creates a new reply-
87          *
88          * @param sone
89          *            The sone that posted the reply
90          * @param id
91          *            The ID of the reply
92          * @param post
93          *            The post to reply to
94          * @param time
95          *            The time of the reply
96          * @param text
97          *            The text of the reply
98          */
99         public PostReply(String id, Sone sone, Post post, long time, String text) {
100                 this.id = UUID.fromString(id);
101                 this.sone = sone;
102                 this.post = post;
103                 this.time = time;
104                 this.text = text;
105         }
106
107         //
108         // ACCESSORS
109         //
110
111         /**
112          * {@inheritDoc}
113          */
114         @Override
115         public String getId() {
116                 return id.toString();
117         }
118
119         /**
120          * {@inheritDoc}
121          */
122         @Override
123         public Sone getSone() {
124                 return sone;
125         }
126
127         /**
128          * Sets the Sone that posted this reply.
129          *
130          * @param sone
131          *            The Sone that posted this reply
132          * @return This reply (for method chaining)
133          */
134         public PostReply setSone(Sone sone) {
135                 this.sone = sone;
136                 return this;
137         }
138
139         /**
140          * Returns the post this reply refers to.
141          *
142          * @return The post this reply refers to
143          */
144         public Post getPost() {
145                 return post;
146         }
147
148         /**
149          * Sets the post this reply refers to.
150          *
151          * @param post
152          *            The post this reply refers to
153          * @return This reply (for method chaining)
154          */
155         public PostReply setPost(Post post) {
156                 this.post = post;
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 reply.
170          *
171          * @param time
172          *            The time of this reply (in milliseconds since Jan 1, 1970 UTC)
173          * @return This reply (for method chaining)
174          */
175         public PostReply 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 reply.
190          *
191          * @param text
192          *            The text of this reply
193          * @return This reply (for method chaining)
194          */
195         public PostReply setText(String text) {
196                 this.text = text;
197                 return this;
198         }
199
200         //
201         // OBJECT METHODS
202         //
203
204         /**
205          * {@inheritDoc}
206          */
207         @Override
208         public int hashCode() {
209                 return id.hashCode();
210         }
211
212         /**
213          * {@inheritDoc}
214          */
215         @Override
216         public boolean equals(Object object) {
217                 if (!(object instanceof PostReply)) {
218                         return false;
219                 }
220                 PostReply reply = (PostReply) object;
221                 return reply.id.equals(id);
222         }
223
224         /**
225          * {@inheritDoc}
226          */
227         @Override
228         public String toString() {
229                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",post=" + post + ",time=" + time + ",text=" + text + "]";
230         }
231
232 }