f0f9d35086dcc41d4caf879f6bc385b4a3a26eb3
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / PostImpl.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 java.util.UUID;
21
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.database.Database;
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         private final Database database;
37
38         /** The GUID of the post. */
39         private final UUID id;
40
41         /** The ID of the owning Sone. */
42         private final String soneId;
43
44         /** The ID of the recipient Sone. */
45         private final String recipientId;
46
47         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
48         private final long time;
49
50         /** The text of the post. */
51         private final String text;
52
53         /** Whether the post is known. */
54         private volatile boolean known;
55
56         /**
57          * Creates a new post.
58          *
59          * @param database
60          *            The database
61          * @param id
62          *            The ID of the post
63          * @param soneId
64          *            The ID of the Sone this post belongs to
65          * @param recipientId
66          *            The ID of the recipient of the post
67          * @param time
68          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
69          * @param text
70          *            The text of the post
71          */
72         public PostImpl(Database database, String id, String soneId, String recipientId, long time, String text) {
73                 this.database = database;
74                 this.id = UUID.fromString(id);
75                 this.soneId = soneId;
76                 this.recipientId = recipientId;
77                 this.time = time;
78                 this.text = text;
79         }
80
81         //
82         // ACCESSORS
83         //
84
85         /**
86          * {@inheritDoc}
87          */
88         @Override
89         public String getId() {
90                 return id.toString();
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         @Override
97         public Sone getSone() {
98                 return database.getSone(soneId).get();
99         }
100
101         /**
102          * {@inheritDocs}
103          */
104         @Override
105         public Optional<String> getRecipientId() {
106                 return Optional.fromNullable(recipientId);
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public Optional<Sone> getRecipient() {
114                 return database.getSone(recipientId);
115         }
116
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         public long getTime() {
122                 return time;
123         }
124
125         /**
126          * {@inheritDoc}
127          */
128         @Override
129         public String getText() {
130                 return text;
131         }
132
133         /**
134          * {@inheritDoc}
135          */
136         @Override
137         public boolean isKnown() {
138                 return known;
139         }
140
141         /**
142          * {@inheritDoc}
143          */
144         @Override
145         public PostImpl setKnown(boolean known) {
146                 this.known = known;
147                 return this;
148         }
149
150         //
151         // OBJECT METHODS
152         //
153
154         /**
155          * {@inheritDoc}
156          */
157         @Override
158         public int hashCode() {
159                 return id.hashCode();
160         }
161
162         /**
163          * {@inheritDoc}
164          */
165         @Override
166         public boolean equals(Object object) {
167                 if (!(object instanceof PostImpl)) {
168                         return false;
169                 }
170                 PostImpl post = (PostImpl) object;
171                 return post.id.equals(id);
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         public String toString() {
179                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
180         }
181
182 }