Don’t force ID to be a UUID.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryPost.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.database.memory;
19
20 import net.pterodactylus.sone.data.Post;
21 import net.pterodactylus.sone.data.Sone;
22 import net.pterodactylus.sone.database.SoneProvider;
23
24 import com.google.common.base.Optional;
25
26 /**
27  * A post is a short message that a user writes in his Sone to let other users
28  * know what is going on.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 class MemoryPost implements Post {
33
34         /** The post database. */
35         private final MemoryDatabase postDatabase;
36
37         /** The Sone provider. */
38         private final SoneProvider soneProvider;
39
40         /** The ID 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         /**
56          * Creates a new post.
57          *
58          * @param postDatabase
59          *            The post database
60          * @param soneProvider
61          *            The Sone provider
62          * @param id
63          *            The ID of the post
64          * @param soneId
65          *            The ID of the Sone this post belongs to
66          * @param recipientId
67          *            The ID of the recipient of the post
68          * @param time
69          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
70          * @param text
71          *            The text of the post
72          */
73         public MemoryPost(MemoryDatabase postDatabase, SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
74                 this.postDatabase = postDatabase;
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 id;
93         }
94
95         @Override
96         public boolean isLoaded() {
97                 return true;
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         @Override
104         public Sone getSone() {
105                 return soneProvider.getSone(soneId).get();
106         }
107
108         /**
109          * {@inheritDocs}
110          */
111         @Override
112         public Optional<String> getRecipientId() {
113                 return Optional.fromNullable(recipientId);
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         @Override
120         public Optional<Sone> getRecipient() {
121                 return soneProvider.getSone(recipientId);
122         }
123
124         /**
125          * {@inheritDoc}
126          */
127         @Override
128         public long getTime() {
129                 return time;
130         }
131
132         /**
133          * {@inheritDoc}
134          */
135         @Override
136         public String getText() {
137                 return text;
138         }
139
140         /**
141          * {@inheritDoc}
142          */
143         @Override
144         public boolean isKnown() {
145                 return postDatabase.isPostKnown(this);
146         }
147
148         /**
149          * {@inheritDoc}
150          */
151         @Override
152         public MemoryPost setKnown(boolean known) {
153                 postDatabase.setPostKnown(this, known);
154                 return this;
155         }
156
157         //
158         // OBJECT METHODS
159         //
160
161         /**
162          * {@inheritDoc}
163          */
164         @Override
165         public int hashCode() {
166                 return id.hashCode();
167         }
168
169         /**
170          * {@inheritDoc}
171          */
172         @Override
173         public boolean equals(Object object) {
174                 if (!(object instanceof MemoryPost)) {
175                         return false;
176                 }
177                 MemoryPost post = (MemoryPost) object;
178                 return post.id.equals(id);
179         }
180
181         /**
182          * {@inheritDoc}
183          */
184         @Override
185         public String toString() {
186                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
187         }
188
189 }