Add a GUID to Post.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Post.java
1 /*
2  * FreenetSone - StatusUpdate.java - Copyright © 2010 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;
19
20 import java.util.UUID;
21
22 /**
23  * A post is a short message that a user writes in his Sone to let other users
24  * know what is going on.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class Post {
29
30         /** The GUID of the post. */
31         private final UUID id;
32
33         /** The Sone this post belongs to. */
34         private final Sone sone;
35
36         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
37         private final long time;
38
39         /** The text of the post. */
40         private final String text;
41
42         /**
43          * Creates a new post.
44          *
45          * @param sone
46          *            The Sone this post belongs to
47          * @param text
48          *            The text of the post
49          */
50         public Post(Sone sone, String text) {
51                 this(sone, System.currentTimeMillis(), text);
52         }
53
54         /**
55          * Creates a new post.
56          *
57          * @param sone
58          *            The Sone this post belongs to
59          * @param time
60          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
61          * @param text
62          *            The text of the post
63          */
64         public Post(Sone sone, long time, String text) {
65                 this(UUID.randomUUID(), sone, time, text);
66         }
67
68         /**
69          * Creates a new post.
70          *
71          * @param id
72          *            The ID of the post
73          * @param sone
74          *            The Sone this post belongs to
75          * @param time
76          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
77          * @param text
78          *            The text of the post
79          */
80         public Post(UUID id, Sone sone, long time, String text) {
81                 this.id = id;
82                 this.sone = sone;
83                 this.time = time;
84                 this.text = text;
85         }
86
87         /**
88          * Returns the ID of the post.
89          *
90          * @return The ID of the post
91          */
92         public String getId() {
93                 return id.toString();
94         }
95
96         /**
97          * Returns the Sone this post belongs to.
98          *
99          * @return The Sone of this post
100          */
101         public Sone getSone() {
102                 return sone;
103         }
104
105         /**
106          * Returns the time of the post.
107          *
108          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
109          */
110         public long getTime() {
111                 return time;
112         }
113
114         /**
115          * Returns the text of the post.
116          *
117          * @return The text of the post
118          */
119         public String getText() {
120                 return text;
121         }
122
123         //
124         // OBJECT METHODS
125         //
126
127         /**
128          * {@inheritDoc}
129          */
130         @Override
131         public int hashCode() {
132                 return text.hashCode() ^ (int) (time >> 32) ^ (int) (time & 0xffffffff);
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         @Override
139         public boolean equals(Object object) {
140                 if (!(object instanceof Post)) {
141                         return false;
142                 }
143                 return ((Post) object).id.equals(id);
144         }
145
146 }