Add modifier to post replies to mark them known.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultReply.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.Database;
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 DefaultReply<T extends Reply<T>> implements Reply<T> {
32
33         protected final Database database;
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         /**
48          * Creates a new reply.
49          *
50          * @param database
51          *              The database
52          * @param id
53          *              The ID of the reply
54          * @param soneId
55          *              The ID of the Sone of the reply
56          * @param time
57          *              The time of the reply
58          * @param text
59          */
60         protected DefaultReply(Database database, String id, String soneId, long time, String text) {
61                 this.database = database;
62                 this.id = id;
63                 this.soneId = soneId;
64                 this.time = time;
65                 this.text = text;
66         }
67
68         @Override
69         public String getId() {
70                 return id;
71         }
72
73         @Override
74         public Sone getSone() {
75                 return database.getSone(soneId).get();
76         }
77
78         @Override
79         public long getTime() {
80                 return time;
81         }
82
83         @Override
84         public String getText() {
85                 return text;
86         }
87
88         //
89         // OBJECT METHODS
90         //
91
92         @Override
93         public int hashCode() {
94                 return id.hashCode();
95         }
96
97         @Override
98         public boolean equals(Object object) {
99                 if (!(object instanceof Reply<?>)) {
100                         return false;
101                 }
102                 Reply<?> reply = (Reply<?>) object;
103                 return reply.getId().equals(id);
104         }
105
106         @Override
107         public String toString() {
108                 return String.format("%s[id=%s,sone=%s,time=%d,text=%s]", getClass().getName(), id, soneId, time, text);
109         }
110
111 }