Fix header comments.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / ReplyImpl.java
1 /*
2  * Sone - ReplyImpl.java - Copyright © 2011–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.Reply;
23 import net.pterodactylus.sone.data.Sone;
24
25 /**
26  * Abstract base class for all replies.
27  *
28  * @param <T>
29  *            The type of the reply
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public abstract class ReplyImpl<T extends Reply<T>> implements Reply<T> {
33
34         /** The ID of the reply. */
35         private final String id;
36
37         /** The Sone that created this reply. */
38         private volatile Sone sone;
39
40         /** The time of the reply. */
41         private volatile long time;
42
43         /** The text of the reply. */
44         private volatile String text;
45
46         /** Whether the reply is known. */
47         private volatile boolean known;
48
49         /**
50          * Creates a new reply with the given ID.
51          *
52          * @param id
53          *            The ID of the reply
54          */
55         protected ReplyImpl(String id) {
56                 this(id, null, 0, null);
57         }
58
59         /**
60          * Creates a new reply with a new random ID.
61          *
62          * @param sone
63          *            The Sone of the reply
64          * @param time
65          *            The time of the reply
66          * @param text
67          *            The text of the reply
68          */
69         protected ReplyImpl(Sone sone, long time, String text) {
70                 this(UUID.randomUUID().toString(), sone, time, text);
71         }
72
73         /**
74          * Creates a new reply.
75          *
76          * @param id
77          *            The ID of the reply
78          * @param sone
79          *            The Sone of the reply
80          * @param time
81          *            The time of the reply
82          * @param text
83          *            The text of the reply
84          */
85         protected ReplyImpl(String id, Sone sone, long time, String text) {
86                 this.id = id;
87                 this.sone = sone;
88                 this.time = time;
89                 this.text = text;
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public String getId() {
97                 return id;
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         @Override
104         public Sone getSone() {
105                 return sone;
106         }
107
108         /**
109          * Sets the Sone that posted this reply.
110          *
111          * @param sone
112          *            The Sone that posted this reply
113          * @return This reply (for method chaining)
114          */
115         @Override
116         @SuppressWarnings("unchecked")
117         public T setSone(Sone sone) {
118                 this.sone = sone;
119                 return (T) this;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public long getTime() {
127                 return time;
128         }
129
130         /**
131          * Sets the time of this reply.
132          *
133          * @param time
134          *            The time of this reply (in milliseconds since Jan 1, 1970 UTC)
135          * @return This reply (for method chaining)
136          */
137         @Override
138         @SuppressWarnings("unchecked")
139         public T setTime(long time) {
140                 this.time = time;
141                 return (T) this;
142         }
143
144         /**
145          * {@inheritDoc}
146          */
147         @Override
148         public String getText() {
149                 return text;
150         }
151
152         /**
153          * Sets the text of this reply.
154          *
155          * @param text
156          *            The text of this reply
157          * @return This reply (for method chaining)
158          */
159         @Override
160         @SuppressWarnings("unchecked")
161         public T setText(String text) {
162                 this.text = text;
163                 return (T) this;
164         }
165
166         /**
167          * {@inheritDoc}
168          */
169         @Override
170         public boolean isKnown() {
171                 return known;
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         @SuppressWarnings("unchecked")
179         public T setKnown(boolean known) {
180                 this.known = known;
181                 return (T) this;
182         }
183
184         //
185         // OBJECT METHODS
186         //
187
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         public int hashCode() {
193                 return id.hashCode();
194         }
195
196         /**
197          * {@inheritDoc}
198          */
199         @Override
200         public boolean equals(Object object) {
201                 if (!(object instanceof Reply<?>)) {
202                         return false;
203                 }
204                 Reply<?> reply = (Reply<?>) object;
205                 return reply.getId().equals(id);
206         }
207
208         /**
209          * {@inheritDoc}
210          */
211         @Override
212         public String toString() {
213                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
214         }
215
216 }