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.data.impl;
20 import java.util.UUID;
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.database.Database;
26 import com.google.common.base.Optional;
29 * A post is a short message that a user writes in his Sone to let other users
30 * know what is going on.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class PostImpl implements Post {
36 private final Database database;
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;
53 /** Whether the post is known. */
54 private volatile boolean known;
64 * The ID of the Sone this post belongs to
66 * The ID of the recipient of the post
68 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
70 * The text of the post
72 public PostImpl(Database database, String id, String soneId, String recipientId, long time, String text) {
73 this.database = database;
74 this.id = UUID.fromString(id);
76 this.recipientId = recipientId;
89 public String getId() {
97 public Sone getSone() {
98 return database.getSone(soneId).get();
105 public Optional<String> getRecipientId() {
106 return Optional.fromNullable(recipientId);
113 public Optional<Sone> getRecipient() {
114 return database.getSone(recipientId);
121 public long getTime() {
129 public String getText() {
137 public boolean isKnown() {
145 public PostImpl setKnown(boolean known) {
158 public int hashCode() {
159 return id.hashCode();
166 public boolean equals(Object object) {
167 if (!(object instanceof PostImpl)) {
170 PostImpl post = (PostImpl) object;
171 return post.id.equals(id);
178 public String toString() {
179 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);