Use a unique ID for posts
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / PostImpl.java
1 /*
2  * Sone - PostImpl.java - Copyright © 2010–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.Post;
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.database.SoneProvider;
24
25 import com.google.common.base.Optional;
26
27 /**
28  * A post is a short message that a user writes in his Sone to let other users
29  * know what is going on.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class PostImpl implements Post {
34
35         private final IdBuilder idBuilder = new IdBuilder();
36
37         /** The Sone provider. */
38         private final SoneProvider soneProvider;
39
40         /** The GUID of the post. */
41         private final String id;
42
43         /** The ID of the owning Sone. */
44         private final String soneId;
45
46         /** The ID of the recipient Sone. */
47         private final String recipientId;
48
49         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
50         private final long time;
51
52         /** The text of the post. */
53         private final String text;
54
55         /** Whether the post is known. */
56         private volatile boolean known;
57
58         /**
59          * Creates a new post.
60          *
61          * @param soneProvider
62          *            The Sone provider
63          * @param id
64          *            The ID of the post
65          * @param soneId
66          *            The ID of the Sone this post belongs to
67          * @param recipientId
68          *            The ID of the recipient of the post
69          * @param time
70          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
71          * @param text
72          *            The text of the post
73          */
74         public PostImpl(SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
75                 this.soneProvider = soneProvider;
76                 this.id = id;
77                 this.soneId = soneId;
78                 this.recipientId = recipientId;
79                 this.time = time;
80                 this.text = text;
81         }
82
83         //
84         // ACCESSORS
85         //
86
87         /**
88          * {@inheritDoc}
89          */
90         @Override
91         public String getId() {
92                 return idBuilder.buildId(soneId, id);
93         }
94
95         @Override
96         public String getInternalId() {
97                 return id;
98         }
99
100         @Override
101         public boolean isLoaded() {
102                 return true;
103         }
104
105         /**
106          * {@inheritDoc}
107          */
108         @Override
109         public Sone getSone() {
110                 return soneProvider.getSone(soneId).get();
111         }
112
113         /**
114          * {@inheritDocs}
115          */
116         @Override
117         public Optional<String> getRecipientId() {
118                 return Optional.fromNullable(recipientId);
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public Optional<Sone> getRecipient() {
126                 return soneProvider.getSone(recipientId);
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         @Override
133         public long getTime() {
134                 return time;
135         }
136
137         /**
138          * {@inheritDoc}
139          */
140         @Override
141         public String getText() {
142                 return text;
143         }
144
145         /**
146          * {@inheritDoc}
147          */
148         @Override
149         public boolean isKnown() {
150                 return known;
151         }
152
153         /**
154          * {@inheritDoc}
155          */
156         @Override
157         public PostImpl setKnown(boolean known) {
158                 this.known = known;
159                 return this;
160         }
161
162         //
163         // OBJECT METHODS
164         //
165
166         /**
167          * {@inheritDoc}
168          */
169         @Override
170         public int hashCode() {
171                 return id.hashCode();
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         public boolean equals(Object object) {
179                 if (!(object instanceof PostImpl)) {
180                         return false;
181                 }
182                 PostImpl post = (PostImpl) object;
183                 return post.id.equals(id);
184         }
185
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         public String toString() {
191                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
192         }
193
194 }