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