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;
25 import net.pterodactylus.sone.data.Post;
26 import net.pterodactylus.sone.data.PostReply;
27 import net.pterodactylus.sone.data.Reply;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.database.Database;
31 import com.google.common.base.Optional;
34 * A post is a short message that a user writes in his Sone to let other users
35 * know what is going on.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class DefaultPost implements Post {
41 private final Database database;
43 /** The ID of the post. */
44 private final String id;
46 /** The ID of the owning Sone. */
47 private final String soneId;
49 /** The ID of the recipient Sone. */
50 private final String recipientId;
52 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
53 private final long time;
55 /** The text of the post. */
56 private final String text;
58 /** Whether the post is known. */
59 private volatile boolean known;
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 DefaultPost(Database database, String id, String soneId, String recipientId, long time, String text) {
78 this.database = database;
81 this.recipientId = recipientId;
91 public String getId() {
96 public Sone getSone() {
97 return database.getSone(soneId).get();
101 public Optional<String> getRecipientId() {
102 return Optional.fromNullable(recipientId);
106 public Optional<Sone> getRecipient() {
107 return database.getSone(recipientId);
111 public long getTime() {
116 public String getText() {
121 public boolean isKnown() {
126 public DefaultPost setKnown(boolean known) {
132 public void like(Sone localSone) {
133 database.likePost(this, localSone);
137 public void unlike(Sone localSone) {
138 database.unlikePost(this, localSone);
142 public Set<Sone> getLikes() {
143 return database.getLikes(this);
147 public List<PostReply> getReplies() {
148 return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
156 public int hashCode() {
157 return id.hashCode();
161 public boolean equals(Object object) {
162 if (!(object instanceof DefaultPost)) {
165 DefaultPost post = (DefaultPost) object;
166 return post.id.equals(id);
170 public String toString() {
171 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);