Use unique IDs for replies, too
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryPostReply.java
1 /*
2  * Sone - MemoryPostReply.java - Copyright © 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.database.memory;
19
20 import net.pterodactylus.sone.data.IdBuilder;
21 import net.pterodactylus.sone.data.Post;
22 import net.pterodactylus.sone.data.PostReply;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.database.SoneProvider;
25
26 import com.google.common.base.Optional;
27
28 /**
29  * Memory-based {@link PostReply} implementation.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 class MemoryPostReply implements PostReply {
34
35         private final IdBuilder idBuilder = new IdBuilder();
36
37         /** The database. */
38         private final MemoryDatabase database;
39
40         /** The Sone provider. */
41         private final SoneProvider soneProvider;
42
43         /** The ID of the post reply. */
44         private final String id;
45
46         /** The ID of the owning Sone. */
47         private final String soneId;
48
49         /** The time of the post reply. */
50         private final long time;
51
52         /** The text of the post reply. */
53         private final String text;
54
55         /** The ID of the post this post reply refers to. */
56         private final String postId;
57
58         /**
59          * Creates a new memory-based {@link PostReply} implementation.
60          *
61          * @param database
62          *            The database
63          * @param soneProvider
64          *            The Sone provider
65          * @param id
66          *            The ID of the post reply
67          * @param soneId
68          *            The ID of the owning Sone
69          * @param time
70          *            The time of the post reply
71          * @param text
72          *            The text of the post reply
73          * @param postId
74          *            The ID of the post this post reply refers to
75          */
76         public MemoryPostReply(MemoryDatabase database, SoneProvider soneProvider, String id, String soneId, long time, String text, String postId) {
77                 this.database = database;
78                 this.soneProvider = soneProvider;
79                 this.id = id;
80                 this.soneId = soneId;
81                 this.time = time;
82                 this.text = text;
83                 this.postId = postId;
84         }
85
86         //
87         // REPLY METHODS
88         //
89
90         /**
91          * {@inheritDocs}
92          */
93         @Override
94         public String getId() {
95                 return idBuilder.buildId(soneId, id);
96         }
97
98         @Override
99         public String getInternalId() {
100                 return id;
101         }
102
103         /**
104          * {@inheritDocs}
105          */
106         @Override
107         public Sone getSone() {
108                 return soneProvider.getSone(soneId).get();
109         }
110
111         /**
112          * {@inheritDocs}
113          */
114         @Override
115         public long getTime() {
116                 return time;
117         }
118
119         /**
120          * {@inheritDocs}
121          */
122         @Override
123         public String getText() {
124                 return text;
125         }
126
127         /**
128          * {@inheritDocs}
129          */
130         @Override
131         public boolean isKnown() {
132                 return database.isPostReplyKnown(this);
133         }
134
135         /**
136          * {@inheritDocs}
137          */
138         @Override
139         public PostReply setKnown(boolean known) {
140                 database.setPostReplyKnown(this, known);
141                 return this;
142         }
143
144         //
145         // POSTREPLY METHODS
146         //
147
148         /**
149          * {@inheritDocs}
150          */
151         @Override
152         public String getPostId() {
153                 return postId;
154         }
155
156         /**
157          * {@inheritDocs}
158          */
159         @Override
160         public Optional<Post> getPost() {
161                 return database.getPost(postId);
162         }
163
164         //
165         // OBJECT METHODS
166         //
167
168         /**
169          * {@inheritDocs}
170          */
171         @Override
172         public int hashCode() {
173                 return id.hashCode();
174         }
175
176         /**
177          * {@inheritDocs}
178          */
179         @Override
180         public boolean equals(Object object) {
181                 if (!(object instanceof MemoryPostReply)) {
182                         return false;
183                 }
184                 MemoryPostReply memoryPostReply = (MemoryPostReply) object;
185                 return memoryPostReply.id.equals(id);
186         }
187
188 }