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