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