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