2 * Sone - PostImpl.java - Copyright © 2010–2013 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 java.util.UUID;
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.Sone;
25 import com.google.common.base.Optional;
28 * A post is a short message that a user writes in his Sone to let other users
29 * know what is going on.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 class MemoryPost implements Post {
35 /** The post database. */
36 private final MemoryDatabase memoryDatabase;
38 /** The GUID of the post. */
39 private final UUID id;
41 /** The ID of the owning Sone. */
42 private final String soneId;
44 /** The ID of the recipient Sone. */
45 private final String recipientId;
47 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
48 private final long time;
50 /** The text of the post. */
51 private final String text;
56 * @param memoryDatabase
61 * The ID of the Sone this post belongs to
63 * The ID of the recipient of the post
65 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
67 * The text of the post
69 public MemoryPost(MemoryDatabase memoryDatabase, String id, String soneId, String recipientId, long time, String text) {
70 this.memoryDatabase = memoryDatabase;
71 this.id = UUID.fromString(id);
73 this.recipientId = recipientId;
84 public String getId() {
90 public Sone getSone() {
91 return memoryDatabase.getSone(soneId).get();
96 public Optional<String> getRecipientId() {
97 return Optional.fromNullable(recipientId);
102 public Optional<Sone> getRecipient() {
103 return memoryDatabase.getSone(recipientId);
108 public long getTime() {
114 public String getText() {
120 public boolean isKnown() {
121 return memoryDatabase.isPostKnown(this);
126 public MemoryPost setKnown(boolean known) {
127 memoryDatabase.setPostKnown(this, known);
137 public int hashCode() {
138 return id.hashCode();
143 public boolean equals(Object object) {
144 if (!(object instanceof MemoryPost)) {
147 MemoryPost post = (MemoryPost) object;
148 return post.id.equals(id);
153 public String toString() {
154 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);