Make replies (more or less) immutable after creation.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / ReplyImpl.java
1 /*
2  * Sone - ReplyImpl.java - Copyright © 2011–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 net.pterodactylus.sone.data.Reply;
21 import net.pterodactylus.sone.data.Sone;
22
23 /**
24  * Abstract base class for all replies.
25  *
26  * @param <T>
27  *            The type of the reply
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public abstract class ReplyImpl<T extends Reply<T>> implements Reply<T> {
31
32         /** The ID of the reply. */
33         private final String id;
34
35         /** The Sone that created this reply. */
36         private final Sone sone;
37
38         /** The time of the reply. */
39         private final long time;
40
41         /** The text of the reply. */
42         private final String text;
43
44         /** Whether the reply is known. */
45         private volatile boolean known;
46
47         /**
48          * Creates a new reply.
49          *
50          * @param id
51          *            The ID of the reply
52          * @param sone
53          *            The Sone of the reply
54          * @param time
55          *            The time of the reply
56          * @param text
57          *            The text of the reply
58          */
59         protected ReplyImpl(String id, Sone sone, long time, String text) {
60                 this.id = id;
61                 this.sone = sone;
62                 this.time = time;
63                 this.text = text;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         @Override
70         public String getId() {
71                 return id;
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         public Sone getSone() {
79                 return sone;
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         public long getTime() {
87                 return time;
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         @Override
94         public String getText() {
95                 return text;
96         }
97
98         /**
99          * {@inheritDoc}
100          */
101         @Override
102         public boolean isKnown() {
103                 return known;
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         @Override
110         @SuppressWarnings("unchecked")
111         public T setKnown(boolean known) {
112                 this.known = known;
113                 return (T) this;
114         }
115
116         //
117         // OBJECT METHODS
118         //
119
120         /**
121          * {@inheritDoc}
122          */
123         @Override
124         public int hashCode() {
125                 return id.hashCode();
126         }
127
128         /**
129          * {@inheritDoc}
130          */
131         @Override
132         public boolean equals(Object object) {
133                 if (!(object instanceof Reply<?>)) {
134                         return false;
135                 }
136                 Reply<?> reply = (Reply<?>) object;
137                 return reply.getId().equals(id);
138         }
139
140         /**
141          * {@inheritDoc}
142          */
143         @Override
144         public String toString() {
145                 return getClass().getName() + "[id=" + id + ",sone=" + sone + ",time=" + time + ",text=" + text + "]";
146         }
147
148 }