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 static com.google.common.collect.FluentIterable.from;
22 import java.util.List;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.PostReply;
26 import net.pterodactylus.sone.data.Reply;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.database.Database;
30 import com.google.common.base.Optional;
33 * A post is a short message that a user writes in his Sone to let other users
34 * know what is going on.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class DefaultPost implements Post {
40 private final Database database;
42 /** The ID of the post. */
43 private final String id;
45 /** The ID of the owning Sone. */
46 private final String soneId;
48 /** The ID of the recipient Sone. */
49 private final String recipientId;
51 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
52 private final long time;
54 /** The text of the post. */
55 private final String text;
57 /** Whether the post is known. */
58 private volatile boolean known;
68 * The ID of the Sone this post belongs to
70 * The ID of the recipient of the post
72 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
74 * The text of the post
76 public DefaultPost(Database database, String id, String soneId, String recipientId, long time, String text) {
77 this.database = database;
80 this.recipientId = recipientId;
90 public String getId() {
95 public Sone getSone() {
96 return database.getSone(soneId).get();
100 public Optional<String> getRecipientId() {
101 return Optional.fromNullable(recipientId);
105 public Optional<Sone> getRecipient() {
106 return database.getSone(recipientId);
110 public long getTime() {
115 public String getText() {
120 public boolean isKnown() {
125 public DefaultPost setKnown(boolean known) {
131 public void like(Sone localSone) {
132 database.likePost(this, localSone);
136 public void unlike(Sone localSone) {
137 database.unlikePost(this, localSone);
141 public List<PostReply> getReplies() {
142 return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
150 public int hashCode() {
151 return id.hashCode();
155 public boolean equals(Object object) {
156 if (!(object instanceof DefaultPost)) {
159 DefaultPost post = (DefaultPost) object;
160 return post.id.equals(id);
164 public String toString() {
165 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);