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