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