Store the “known” status of a post in the database.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultPost.java
1 /*
2  * Sone - PostImpl.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data.impl;
19
20 import static com.google.common.collect.FluentIterable.from;
21
22 import java.util.List;
23 import java.util.Set;
24
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;
30
31 import com.google.common.base.Optional;
32
33 /**
34  * A post is a short message that a user writes in his Sone to let other users
35  * know what is going on.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class DefaultPost implements Post {
40
41         private final Database database;
42
43         /** The ID of the post. */
44         private final String id;
45
46         /** The ID of the owning Sone. */
47         private final String soneId;
48
49         /** The ID of the recipient Sone. */
50         private final String recipientId;
51
52         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
53         private final long time;
54
55         /** The text of the post. */
56         private final String text;
57
58         /**
59          * Creates a new post.
60          *
61          * @param database
62          *            The database
63          * @param id
64          *            The ID of the post
65          * @param soneId
66          *            The ID of the Sone this post belongs to
67          * @param recipientId
68          *            The ID of the recipient of the post
69          * @param time
70          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
71          * @param text
72          *            The text of the post
73          */
74         public DefaultPost(Database database, String id, String soneId, String recipientId, long time, String text) {
75                 this.database = database;
76                 this.id = id;
77                 this.soneId = soneId;
78                 this.recipientId = recipientId;
79                 this.time = time;
80                 this.text = text;
81         }
82
83         //
84         // ACCESSORS
85         //
86
87         @Override
88         public String getId() {
89                 return id;
90         }
91
92         @Override
93         public Sone getSone() {
94                 return database.getSone(soneId).get();
95         }
96
97         @Override
98         public Optional<String> getRecipientId() {
99                 return Optional.fromNullable(recipientId);
100         }
101
102         @Override
103         public Optional<Sone> getRecipient() {
104                 return database.getSone(recipientId);
105         }
106
107         @Override
108         public long getTime() {
109                 return time;
110         }
111
112         @Override
113         public String getText() {
114                 return text;
115         }
116
117         @Override
118         public boolean isKnown() {
119                 return database.isPostKnown(this);
120         }
121
122         @Override
123         public DefaultPost setKnown() {
124                 database.setPostKnown(this);
125                 return this;
126         }
127
128         @Override
129         public void like(Sone localSone) {
130                 database.likePost(this, localSone);
131         }
132
133         @Override
134         public void unlike(Sone localSone) {
135                 database.unlikePost(this, localSone);
136         }
137
138         @Override
139         public boolean isLiked(Sone sone) {
140                 return database.isLiked(this, sone);
141         }
142
143         @Override
144         public Set<Sone> getLikes() {
145                 return database.getLikes(this);
146         }
147
148         @Override
149         public List<PostReply> getReplies() {
150                 return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
151         }
152
153         //
154         // OBJECT METHODS
155         //
156
157         @Override
158         public int hashCode() {
159                 return id.hashCode();
160         }
161
162         @Override
163         public boolean equals(Object object) {
164                 if (!(object instanceof DefaultPost)) {
165                         return false;
166                 }
167                 DefaultPost post = (DefaultPost) object;
168                 return post.id.equals(id);
169         }
170
171         @Override
172         public String toString() {
173                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
174         }
175
176 }