2 * FreenetSone - Sone.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
27 import java.util.UUID;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.util.logging.Logging;
33 import freenet.keys.FreenetURI;
36 * A Sone defines everything about a user: her profile, her status updates, her
37 * replies, her likes and dislikes, etc.
39 * Operations that modify the Sone need to synchronize on the Sone in question.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 private static final Logger logger = Logging.getLogger(Sone.class);
48 /** A GUID for this Sone. */
49 private final UUID id;
51 /** The name of this Sone. */
54 /** The URI under which the Sone is stored in Freenet. */
55 private FreenetURI requestUri;
57 /** The URI used to insert a new version of this Sone. */
58 /* This will be null for remote Sones! */
59 private FreenetURI insertUri;
61 /** The time of the last inserted update. */
64 /** The profile of this Sone. */
65 private Profile profile;
67 /** All friend Sones. */
68 private final Set<Sone> friendSones = new HashSet<Sone>();
71 private final Set<Post> posts = new HashSet<Post>();
74 private final Set<Reply> replies = new HashSet<Reply>();
76 /** The IDs of all blocked Sones. */
77 private final Set<String> blockedSoneIds = new HashSet<String>();
79 /** Modification count. */
80 private volatile long modificationCounter = 0;
88 public Sone(String id) {
89 this.id = UUID.fromString(id);
97 * Returns the ID of this Sone.
99 * @return The ID of this Sone
101 public String getId() {
102 return id.toString();
106 * Returns the name of this Sone.
108 * @return The name of this Sone
110 public String getName() {
115 * Sets the name of this Sone.
118 * The name of this Sone
119 * @return This sone (for method chaining)
121 public Sone setName(String name) {
127 * Returns the request URI of this Sone.
129 * @return The request URI of this Sone
131 public FreenetURI getRequestUri() {
136 * Sets the request URI of this Sone.
139 * The request URI of this Sone
140 * @return This Sone (for method chaining)
142 public Sone setRequestUri(FreenetURI requestUri) {
143 this.requestUri = requestUri;
148 * Returns the insert URI of this Sone.
150 * @return The insert URI of this Sone
152 public FreenetURI getInsertUri() {
157 * Sets the insert URI of this Sone.
160 * The insert URI of this Sone
161 * @return This Sone (for method chaining)
163 public Sone setInsertUri(FreenetURI insertUri) {
164 this.insertUri = insertUri;
169 * Return the time of the last inserted update of this Sone.
171 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
173 public long getTime() {
178 * Sets the time of the last inserted update of this Sone.
181 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
182 * @return This Sone (for method chaining)
184 public Sone setTime(long time) {
190 * Returns a copy of the profile. If you want to update values in the
191 * profile of this Sone, update the values in the returned {@link Profile}
192 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
194 * @return A copy of the profile
196 public Profile getProfile() {
197 return new Profile(profile);
201 * Sets the profile of this Sone. A copy of the given profile is stored so
202 * that subsequent modifications of the given profile are not reflected in
208 public synchronized void setProfile(Profile profile) {
209 this.profile = new Profile(profile);
210 modificationCounter++;
214 * Returns all friend Sones of this Sone.
216 * @return The friend Sones of this Sone
218 public List<Sone> getFriends() {
219 List<Sone> friends = new ArrayList<Sone>(friendSones);
220 Collections.sort(friends, new Comparator<Sone>() {
223 public int compare(Sone leftSone, Sone rightSone) {
224 int diff = SoneAccessor.getNiceName(leftSone).compareTo(SoneAccessor.getNiceName(rightSone));
228 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightSone.getTime() - leftSone.getTime()));
235 * Sets all friends of this Sone at once.
238 * The new (and only) friends of this Sone
239 * @return This Sone (for method chaining)
241 public synchronized Sone setFriends(Collection<Sone> friends) {
243 friendSones.addAll(friends);
244 modificationCounter++;
249 * Returns whether this Sone has the given Sone as a friend Sone.
252 * The friend Sone to check for
253 * @return {@code true} if this Sone has the given Sone as a friend,
254 * {@code false} otherwise
256 public boolean hasFriend(Sone friendSone) {
257 return friendSones.contains(friendSone);
261 * Adds the given Sone as a friend Sone.
264 * The friend Sone to add
265 * @return This Sone (for method chaining)
267 public synchronized Sone addFriend(Sone friendSone) {
268 if (!friendSone.equals(this) && friendSones.add(friendSone)) {
269 modificationCounter++;
275 * Removes the given Sone as a friend Sone.
278 * The friend Sone to remove
279 * @return This Sone (for method chaining)
281 public synchronized Sone removeFriend(Sone friendSone) {
282 if (friendSones.remove(friendSone)) {
283 modificationCounter++;
289 * Returns the list of posts of this Sone, sorted by time, newest first.
291 * @return All posts of this Sone
293 public List<Post> getPosts() {
294 List<Post> sortedPosts = new ArrayList<Post>(posts);
295 Collections.sort(sortedPosts, new Comparator<Post>() {
298 public int compare(Post leftPost, Post rightPost) {
299 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
307 * Sets all posts of this Sone at once.
310 * The new (and only) posts of this Sone
311 * @return This Sone (for method chaining)
313 public synchronized Sone setPosts(Collection<Post> posts) {
315 this.posts.addAll(posts);
316 modificationCounter++;
321 * Adds the given post to this Sone. The post will not be added if its
322 * {@link Post#getSone() Sone} is not this Sone.
327 public synchronized void addPost(Post post) {
328 if (post.getSone().equals(this) && posts.add(post)) {
329 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
330 modificationCounter++;
335 * Removes the given post from this Sone.
340 public synchronized void removePost(Post post) {
341 if (post.getSone().equals(this) && posts.remove(post)) {
342 modificationCounter++;
347 * Returns all replies this Sone made.
349 * @return All replies this Sone made
351 public Set<Reply> getReplies() {
352 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
353 return Collections.unmodifiableSet(replies);
357 * Sets all replies of this Sone at once.
360 * The new (and only) replies of this Sone
361 * @return This Sone (for method chaining)
363 public synchronized Sone setReplies(Collection<Reply> replies) {
364 this.replies.clear();
365 this.replies.addAll(replies);
366 modificationCounter++;
371 * Adds a reply to this Sone. If the given reply was not made by this Sone,
372 * nothing is added to this Sone.
377 public synchronized void addReply(Reply reply) {
378 if (reply.getSone().equals(this) && replies.add(reply)) {
379 modificationCounter++;
384 * Removes a reply from this Sone.
387 * The reply to remove
389 public synchronized void removeReply(Reply reply) {
390 if (reply.getSone().equals(this) && replies.remove(reply)) {
391 modificationCounter++;
396 * Returns the IDs of all blocked Sones. These Sones will not propagated
397 * using the “known Sones” mechanism.
399 * @return The IDs of all blocked Sones
401 public Set<String> getBlockedSoneIds() {
402 return Collections.unmodifiableSet(blockedSoneIds);
406 * Returns whether the given Sone ID is blocked.
409 * The Sone ID to check
410 * @return {@code true} if the given Sone ID is blocked, {@code false}
413 public boolean isSoneBlocked(String soneId) {
414 return blockedSoneIds.contains(soneId);
418 * Adds the given ID to the list of blocked IDs.
421 * The Sone ID to block
423 public synchronized void addBlockedSoneId(String soneId) {
424 if (blockedSoneIds.add(soneId)) {
425 modificationCounter++;
430 * Removes the given ID from the list of blocked IDs.
433 * The Sone ID to unblock
435 public synchronized void removeBlockedSoneId(String soneId) {
436 if (blockedSoneIds.remove(soneId)) {
437 modificationCounter++;
442 * Returns the modification counter.
444 * @return The modification counter
446 public synchronized long getModificationCounter() {
447 return modificationCounter;
451 * Sets the modification counter.
453 * @param modificationCounter
454 * The new modification counter
456 public synchronized void setModificationCounter(long modificationCounter) {
457 this.modificationCounter = modificationCounter;
461 * Updates the suggested edition in both the request URI and the insert URI.
464 * The request URI that resulted from an insert
466 public void updateUris(FreenetURI requestUri) {
467 /* TODO - check for the correct URI. */
468 long latestEdition = requestUri.getSuggestedEdition();
469 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
470 if (this.insertUri != null) {
471 this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
483 public int hashCode() {
484 return id.hashCode();
491 public boolean equals(Object object) {
492 if (!(object instanceof Sone)) {
495 return ((Sone) object).id.equals(id);
502 public String toString() {
503 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";