2 * Sone - MemoryPost.java - Copyright © 2010–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.database.memory;
20 import static com.google.common.base.Optional.fromNullable;
22 import java.util.UUID;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.database.SoneProvider;
28 import com.google.common.base.Optional;
31 * A post is a short message that a user writes in his Sone to let other users
32 * know what is going on.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 class MemoryPost implements Post {
38 /** The post database. */
39 private final MemoryDatabase postDatabase;
41 /** The Sone provider. */
42 private final SoneProvider soneProvider;
44 /** The GUID of the post. */
45 private final UUID id;
47 /** The ID of the owning Sone. */
48 private final String soneId;
50 /** The ID of the recipient Sone. */
51 private final String recipientId;
53 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
54 private final long time;
56 /** The text of the post. */
57 private final String text;
69 * The ID of the Sone this post belongs to
71 * The ID of the recipient of the post
73 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
75 * The text of the post
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);
82 this.recipientId = recipientId;
95 public String getId() {
100 public boolean isLoaded() {
108 public Sone getSone() {
109 return soneProvider.getSone(soneId);
116 public Optional<String> getRecipientId() {
117 return fromNullable(recipientId);
124 public Optional<Sone> getRecipient() {
125 return fromNullable(soneProvider.getSone(recipientId));
132 public long getTime() {
140 public String getText() {
148 public boolean isKnown() {
149 return postDatabase.isPostKnown(this);
156 public MemoryPost setKnown(boolean known) {
157 postDatabase.setPostKnown(this, known);
169 public int hashCode() {
170 return id.hashCode();
177 public boolean equals(Object object) {
178 if (!(object instanceof MemoryPost)) {
181 MemoryPost post = (MemoryPost) object;
182 return post.id.equals(id);
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);