Don’t let a Sone add itself as friend!
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
1 /*
2  * FreenetSone - Sone.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.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.UUID;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.util.logging.Logging;
31 import freenet.keys.FreenetURI;
32
33 /**
34  * A Sone defines everything about a user: her profile, her status updates, her
35  * replies, her likes and dislikes, etc.
36  * <p>
37  * Operations that modify the Sone need to synchronize on the Sone in question.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class Sone {
42
43         /** The logger. */
44         private static final Logger logger = Logging.getLogger(Sone.class);
45
46         /** A GUID for this Sone. */
47         private final UUID id;
48
49         /** The name of this Sone. */
50         private final String name;
51
52         /** The URI under which the Sone is stored in Freenet. */
53         private FreenetURI requestUri;
54
55         /** The URI used to insert a new version of this Sone. */
56         /* This will be null for remote Sones! */
57         private FreenetURI insertUri;
58
59         /** The profile of this Sone. */
60         private Profile profile;
61
62         /** All friend Sones. */
63         private final Set<Sone> friendSones = new HashSet<Sone>();
64
65         /** All posts. */
66         private final List<Post> posts = new ArrayList<Post>();
67
68         /** All replies. */
69         private final Set<Reply> replies = new HashSet<Reply>();
70
71         /** Modification count. */
72         private volatile long modificationCounter = 0;
73
74         /**
75          * Creates a new Sone.
76          *
77          * @param id
78          *            The ID of this Sone
79          * @param name
80          *            The name of the Sone
81          * @param requestUri
82          *            The request URI of the Sone
83          */
84         public Sone(UUID id, String name, FreenetURI requestUri) {
85                 this(id, name, requestUri, null);
86         }
87
88         /**
89          * Creates a new Sone.
90          *
91          * @param id
92          *            The ID of this Sone
93          * @param name
94          *            The name of the Sone
95          * @param requestUri
96          *            The request URI of the Sone
97          * @param insertUri
98          *            The insert URI of the Sone
99          */
100         public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
101                 this.id = id;
102                 this.name = name;
103                 this.requestUri = requestUri;
104                 this.insertUri = insertUri;
105         }
106
107         //
108         // ACCESSORS
109         //
110
111         /**
112          * Returns the ID of this Sone.
113          *
114          * @return The ID of this Sone
115          */
116         public String getId() {
117                 return id.toString();
118         }
119
120         /**
121          * Returns the name of this Sone.
122          *
123          * @return The name of this Sone
124          */
125         public String getName() {
126                 return name;
127         }
128
129         /**
130          * Returns the request URI of this Sone.
131          *
132          * @return The request URI of this Sone
133          */
134         public FreenetURI getRequestUri() {
135                 return requestUri;
136         }
137
138         /**
139          * Returns the insert URI of this Sone.
140          *
141          * @return The insert URI of this Sone
142          */
143         public FreenetURI getInsertUri() {
144                 return insertUri;
145         }
146
147         /**
148          * Returns a copy of the profile. If you want to update values in the
149          * profile of this Sone, update the values in the returned {@link Profile}
150          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
151          *
152          * @return A copy of the profile
153          */
154         public Profile getProfile() {
155                 return new Profile(profile);
156         }
157
158         /**
159          * Sets the profile of this Sone. A copy of the given profile is stored so
160          * that subsequent modifications of the given profile are not reflected in
161          * this Sone!
162          *
163          * @param profile
164          *            The profile to set
165          */
166         public synchronized void setProfile(Profile profile) {
167                 this.profile = new Profile(profile);
168                 modificationCounter++;
169         }
170
171         /**
172          * Returns all friend Sones of this Sone.
173          *
174          * @return The friend Sones of this Sone
175          */
176         public Set<Sone> getFriends() {
177                 return Collections.unmodifiableSet(friendSones);
178         }
179
180         /**
181          * Returns whether this Sone has the given Sone as a friend Sone.
182          *
183          * @param friendSone
184          *            The friend Sone to check for
185          * @return {@code true} if this Sone has the given Sone as a friend,
186          *         {@code false} otherwise
187          */
188         public boolean hasFriend(Sone friendSone) {
189                 return friendSones.contains(friendSone);
190         }
191
192         /**
193          * Adds the given Sone as a friend Sone.
194          *
195          * @param friendSone
196          *            The friend Sone to add
197          * @return This Sone (for method chaining)
198          */
199         public synchronized Sone addFriend(Sone friendSone) {
200                 if (!friendSone.equals(this) && friendSones.add(friendSone)) {
201                         modificationCounter++;
202                 }
203                 return this;
204         }
205
206         /**
207          * Removes the given Sone as a friend Sone.
208          *
209          * @param friendSone
210          *            The friend Sone to remove
211          * @return This Sone (for method chaining)
212          */
213         public synchronized Sone removeFriend(Sone friendSone) {
214                 if (friendSones.remove(friendSone)) {
215                         modificationCounter++;
216                 }
217                 return this;
218         }
219
220         /**
221          * Returns the list of posts of this Sone, sorted by time, newest first.
222          *
223          * @return All posts of this Sone
224          */
225         public List<Post> getPosts() {
226                 List<Post> sortedPosts = new ArrayList<Post>(posts);
227                 Collections.sort(sortedPosts, new Comparator<Post>() {
228
229                         @Override
230                         public int compare(Post leftPost, Post rightPost) {
231                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
232                         }
233
234                 });
235                 return sortedPosts;
236         }
237
238         /**
239          * Adds the given post to this Sone. The post will not be added if its
240          * {@link Post#getSone() Sone} is not this Sone.
241          *
242          * @param post
243          *            The post to add
244          */
245         public synchronized void addPost(Post post) {
246                 if (post.getSone().equals(this) && posts.add(post)) {
247                         logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
248                         modificationCounter++;
249                 }
250         }
251
252         /**
253          * Removes the given post from this Sone.
254          *
255          * @param post
256          *            The post to remove
257          */
258         public synchronized void removePost(Post post) {
259                 if (post.getSone().equals(this) && posts.remove(post)) {
260                         modificationCounter++;
261                 }
262         }
263
264         /**
265          * Returns all replies this Sone made.
266          *
267          * @return All replies this Sone made
268          */
269         public Set<Reply> getReplies() {
270                 return Collections.unmodifiableSet(replies);
271         }
272
273         /**
274          * Adds a reply to this Sone. If the given reply was not made by this Sone,
275          * nothing is added to this Sone.
276          *
277          * @param reply
278          *            The reply to add
279          */
280         public synchronized void addReply(Reply reply) {
281                 if (reply.getSone().equals(this) && replies.add(reply)) {
282                         modificationCounter++;
283                 }
284         }
285
286         /**
287          * Removes a reply from this Sone.
288          *
289          * @param reply
290          *            The reply to remove
291          */
292         public synchronized void removeReply(Reply reply) {
293                 if (reply.getSone().equals(this) && replies.remove(reply)) {
294                         modificationCounter++;
295                 }
296         }
297
298         /**
299          * Returns the modification counter.
300          *
301          * @return The modification counter
302          */
303         public synchronized long getModificationCounter() {
304                 return modificationCounter;
305         }
306
307         /**
308          * Sets the modification counter.
309          *
310          * @param modificationCounter
311          *            The new modification counter
312          */
313         public synchronized void setModificationCounter(long modificationCounter) {
314                 this.modificationCounter = modificationCounter;
315         }
316
317         /**
318          * Updates the suggested edition in both the request URI and the insert URI.
319          *
320          * @param requestUri
321          *            The request URI that resulted from an insert
322          */
323         public void updateUris(FreenetURI requestUri) {
324                 /* TODO - check for the correct URI. */
325                 long latestEdition = requestUri.getSuggestedEdition();
326                 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
327                 this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
328         }
329
330         //
331         // OBJECT METHODS
332         //
333
334         /**
335          * {@inheritDoc}
336          */
337         @Override
338         public int hashCode() {
339                 return id.hashCode();
340         }
341
342         /**
343          * {@inheritDoc}
344          */
345         @Override
346         public boolean equals(Object object) {
347                 if (!(object instanceof Sone)) {
348                         return false;
349                 }
350                 return ((Sone) object).id.equals(id);
351         }
352
353         /**
354          * {@inheritDoc}
355          */
356         @Override
357         public String toString() {
358                 return getClass().getName() + "[id=" + getId() + ",name=" + getName() + ",requestUri=" + getRequestUri() + ",insertUri=" + getInsertUri() + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";
359         }
360
361 }