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 net.pterodactylus.sone.data.IdBuilder;
21 import net.pterodactylus.sone.data.Post;
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.database.SoneProvider;
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 public class PostImpl implements Post {
35 private final IdBuilder idBuilder = new IdBuilder();
37 /** The Sone provider. */
38 private final SoneProvider soneProvider;
40 /** The GUID of the post. */
41 private final String id;
43 /** The ID of the owning Sone. */
44 private final String soneId;
46 /** The ID of the recipient Sone. */
47 private final String recipientId;
49 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
50 private final long time;
52 /** The text of the post. */
53 private final String text;
55 /** Whether the post is known. */
56 private volatile boolean known;
66 * The ID of the Sone this post belongs to
68 * The ID of the recipient of the post
70 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
72 * The text of the post
74 public PostImpl(SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
75 this.soneProvider = soneProvider;
78 this.recipientId = recipientId;
91 public String getId() {
92 return idBuilder.buildId(soneId, id);
96 public String getInternalId() {
101 public boolean isLoaded() {
109 public Sone getSone() {
110 return soneProvider.getSone(soneId).get();
117 public Optional<String> getRecipientId() {
118 return Optional.fromNullable(recipientId);
125 public Optional<Sone> getRecipient() {
126 return soneProvider.getSone(recipientId);
133 public long getTime() {
141 public String getText() {
149 public boolean isKnown() {
157 public PostImpl setKnown(boolean known) {
170 public int hashCode() {
171 return id.hashCode();
178 public boolean equals(Object object) {
179 if (!(object instanceof PostImpl)) {
182 PostImpl post = (PostImpl) object;
183 return post.id.equals(id);
190 public String toString() {
191 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);