a1204f911cea67bc037e744724cdfbe28f1c62d3
[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         /** Whether the post is known. */
59         private volatile boolean known;
60
61         /**
62          * Creates a new post.
63          *
64          * @param database
65          *            The database
66          * @param id
67          *            The ID of the post
68          * @param soneId
69          *            The ID of the Sone this post belongs to
70          * @param recipientId
71          *            The ID of the recipient of the post
72          * @param time
73          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
74          * @param text
75          *            The text of the post
76          */
77         public DefaultPost(Database database, String id, String soneId, String recipientId, long time, String text) {
78                 this.database = database;
79                 this.id = id;
80                 this.soneId = soneId;
81                 this.recipientId = recipientId;
82                 this.time = time;
83                 this.text = text;
84         }
85
86         //
87         // ACCESSORS
88         //
89
90         @Override
91         public String getId() {
92                 return id;
93         }
94
95         @Override
96         public Sone getSone() {
97                 return database.getSone(soneId).get();
98         }
99
100         @Override
101         public Optional<String> getRecipientId() {
102                 return Optional.fromNullable(recipientId);
103         }
104
105         @Override
106         public Optional<Sone> getRecipient() {
107                 return database.getSone(recipientId);
108         }
109
110         @Override
111         public long getTime() {
112                 return time;
113         }
114
115         @Override
116         public String getText() {
117                 return text;
118         }
119
120         @Override
121         public boolean isKnown() {
122                 return known;
123         }
124
125         @Override
126         public DefaultPost setKnown(boolean known) {
127                 this.known = known;
128                 return this;
129         }
130
131         @Override
132         public void like(Sone localSone) {
133                 database.likePost(this, localSone);
134         }
135
136         @Override
137         public void unlike(Sone localSone) {
138                 database.unlikePost(this, localSone);
139         }
140
141         @Override
142         public boolean isLiked(Sone sone) {
143                 return database.isLiked(this, sone);
144         }
145
146         @Override
147         public Set<Sone> getLikes() {
148                 return database.getLikes(this);
149         }
150
151         @Override
152         public List<PostReply> getReplies() {
153                 return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
154         }
155
156         //
157         // OBJECT METHODS
158         //
159
160         @Override
161         public int hashCode() {
162                 return id.hashCode();
163         }
164
165         @Override
166         public boolean equals(Object object) {
167                 if (!(object instanceof DefaultPost)) {
168                         return false;
169                 }
170                 DefaultPost post = (DefaultPost) object;
171                 return post.id.equals(id);
172         }
173
174         @Override
175         public String toString() {
176                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
177         }
178
179 }