Merge branch 'release-0.9.8'
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / PostImpl.java
1 /*
2  * Sone - PostImpl.java - Copyright © 2010–2016 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.base.Optional.fromNullable;
21
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.database.SoneProvider;
25
26 import com.google.common.base.Optional;
27
28 /**
29  * A post is a short message that a user writes in his Sone to let other users
30  * know what is going on.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class PostImpl implements Post {
35
36         /** The Sone provider. */
37         private final SoneProvider soneProvider;
38
39         /** The GUID of the post. */
40         private final String id;
41
42         /** The ID of the owning Sone. */
43         private final String soneId;
44
45         /** The ID of the recipient Sone. */
46         private final String recipientId;
47
48         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
49         private final long time;
50
51         /** The text of the post. */
52         private final String text;
53
54         /** Whether the post is known. */
55         private volatile boolean known;
56
57         /**
58          * Creates a new post.
59          *
60          * @param soneProvider
61          *            The Sone provider
62          * @param id
63          *            The ID of the post
64          * @param soneId
65          *            The ID of the Sone this post belongs to
66          * @param recipientId
67          *            The ID of the recipient of the post
68          * @param time
69          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
70          * @param text
71          *            The text of the post
72          */
73         public PostImpl(SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
74                 this.soneProvider = soneProvider;
75                 this.id = id;
76                 this.soneId = soneId;
77                 this.recipientId = recipientId;
78                 this.time = time;
79                 this.text = text;
80         }
81
82         //
83         // ACCESSORS
84         //
85
86         /**
87          * {@inheritDoc}
88          */
89         @Override
90         public String getId() {
91                 return id;
92         }
93
94         @Override
95         public boolean isLoaded() {
96                 return true;
97         }
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
103         public Sone getSone() {
104                 return soneProvider.getSone(soneId);
105         }
106
107         /**
108          * {@inheritDocs}
109          */
110         @Override
111         public Optional<String> getRecipientId() {
112                 return fromNullable(recipientId);
113         }
114
115         /**
116          * {@inheritDoc}
117          */
118         @Override
119         public Optional<Sone> getRecipient() {
120                 return fromNullable(soneProvider.getSone(recipientId));
121         }
122
123         /**
124          * {@inheritDoc}
125          */
126         @Override
127         public long getTime() {
128                 return time;
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         @Override
135         public String getText() {
136                 return text;
137         }
138
139         /**
140          * {@inheritDoc}
141          */
142         @Override
143         public boolean isKnown() {
144                 return known;
145         }
146
147         /**
148          * {@inheritDoc}
149          */
150         @Override
151         public PostImpl setKnown(boolean known) {
152                 this.known = known;
153                 return this;
154         }
155
156         //
157         // OBJECT METHODS
158         //
159
160         /**
161          * {@inheritDoc}
162          */
163         @Override
164         public int hashCode() {
165                 return id.hashCode();
166         }
167
168         /**
169          * {@inheritDoc}
170          */
171         @Override
172         public boolean equals(Object object) {
173                 if (!(object instanceof PostImpl)) {
174                         return false;
175                 }
176                 PostImpl post = (PostImpl) object;
177                 return post.id.equals(id);
178         }
179
180         /**
181          * {@inheritDoc}
182          */
183         @Override
184         public String toString() {
185                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
186         }
187
188 }