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