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