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.util.logging.Logging;
32 import freenet.keys.FreenetURI;
35 * A Sone defines everything about a user: her profile, her status updates, her
36 * replies, her likes and dislikes, etc.
38 * Operations that modify the Sone need to synchronize on the Sone in question.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 private static final Logger logger = Logging.getLogger(Sone.class);
47 /** A GUID for this Sone. */
48 private final UUID id;
50 /** The name of this Sone. */
53 /** The URI under which the Sone is stored in Freenet. */
54 private FreenetURI requestUri;
56 /** The URI used to insert a new version of this Sone. */
57 /* This will be null for remote Sones! */
58 private FreenetURI insertUri;
60 /** The time of the last inserted update. */
63 /** The profile of this Sone. */
64 private Profile profile;
66 /** All friend Sones. */
67 private final Set<Sone> friendSones = new HashSet<Sone>();
70 private final Set<Post> posts = new HashSet<Post>();
73 private final Set<Reply> replies = new HashSet<Reply>();
75 /** Modification count. */
76 private volatile long modificationCounter = 0;
84 public Sone(String id) {
85 this.id = UUID.fromString(id);
93 * Returns the ID of this Sone.
95 * @return The ID of this Sone
97 public String getId() {
102 * Returns the name of this Sone.
104 * @return The name of this Sone
106 public String getName() {
111 * Sets the name of this Sone.
114 * The name of this Sone
115 * @return This sone (for method chaining)
117 public Sone setName(String name) {
123 * Returns the request URI of this Sone.
125 * @return The request URI of this Sone
127 public FreenetURI getRequestUri() {
132 * Sets the request URI of this Sone.
135 * The request URI of this Sone
136 * @return This Sone (for method chaining)
138 public Sone setRequestUri(FreenetURI requestUri) {
139 this.requestUri = requestUri;
144 * Returns the insert URI of this Sone.
146 * @return The insert URI of this Sone
148 public FreenetURI getInsertUri() {
153 * Sets the insert URI of this Sone.
156 * The insert URI of this Sone
157 * @return This Sone (for method chaining)
159 public Sone setInsertUri(FreenetURI insertUri) {
160 this.insertUri = insertUri;
165 * Return the time of the last inserted update of this Sone.
167 * @return The time of the update (in milliseconds since Jan 1, 1970 UTC)
169 public long getTime() {
174 * Sets the time of the last inserted update of this Sone.
177 * The time of the update (in milliseconds since Jan 1, 1970 UTC)
178 * @return This Sone (for method chaining)
180 public Sone setTime(long time) {
186 * Returns a copy of the profile. If you want to update values in the
187 * profile of this Sone, update the values in the returned {@link Profile}
188 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
190 * @return A copy of the profile
192 public Profile getProfile() {
193 return new Profile(profile);
197 * Sets the profile of this Sone. A copy of the given profile is stored so
198 * that subsequent modifications of the given profile are not reflected in
204 public synchronized void setProfile(Profile profile) {
205 this.profile = new Profile(profile);
206 modificationCounter++;
210 * Returns all friend Sones of this Sone.
212 * @return The friend Sones of this Sone
214 public Set<Sone> getFriends() {
215 return Collections.unmodifiableSet(friendSones);
219 * Sets all friends of this Sone at once.
222 * The new (and only) friends of this Sone
223 * @return This Sone (for method chaining)
225 public synchronized Sone setFriends(Collection<Sone> friends) {
227 friendSones.addAll(friends);
228 modificationCounter++;
233 * Returns whether this Sone has the given Sone as a friend Sone.
236 * The friend Sone to check for
237 * @return {@code true} if this Sone has the given Sone as a friend,
238 * {@code false} otherwise
240 public boolean hasFriend(Sone friendSone) {
241 return friendSones.contains(friendSone);
245 * Adds the given Sone as a friend Sone.
248 * The friend Sone to add
249 * @return This Sone (for method chaining)
251 public synchronized Sone addFriend(Sone friendSone) {
252 if (!friendSone.equals(this) && friendSones.add(friendSone)) {
253 modificationCounter++;
259 * Removes the given Sone as a friend Sone.
262 * The friend Sone to remove
263 * @return This Sone (for method chaining)
265 public synchronized Sone removeFriend(Sone friendSone) {
266 if (friendSones.remove(friendSone)) {
267 modificationCounter++;
273 * Returns the list of posts of this Sone, sorted by time, newest first.
275 * @return All posts of this Sone
277 public List<Post> getPosts() {
278 List<Post> sortedPosts = new ArrayList<Post>(posts);
279 Collections.sort(sortedPosts, new Comparator<Post>() {
282 public int compare(Post leftPost, Post rightPost) {
283 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
291 * Sets all posts of this Sone at once.
294 * The new (and only) posts of this Sone
295 * @return This Sone (for method chaining)
297 public synchronized Sone setPosts(Collection<Post> posts) {
299 this.posts.addAll(posts);
300 modificationCounter++;
305 * Adds the given post to this Sone. The post will not be added if its
306 * {@link Post#getSone() Sone} is not this Sone.
311 public synchronized void addPost(Post post) {
312 if (post.getSone().equals(this) && posts.add(post)) {
313 logger.log(Level.FINEST, "Adding %s to “%s”.", new Object[] { post, getName() });
314 modificationCounter++;
319 * Removes the given post from this Sone.
324 public synchronized void removePost(Post post) {
325 if (post.getSone().equals(this) && posts.remove(post)) {
326 modificationCounter++;
331 * Returns all replies this Sone made.
333 * @return All replies this Sone made
335 public Set<Reply> getReplies() {
336 logger.log(Level.FINEST, "Friends of %s: %s", new Object[] { this, friendSones });
337 return Collections.unmodifiableSet(replies);
341 * Sets all replies of this Sone at once.
344 * The new (and only) replies of this Sone
345 * @return This Sone (for method chaining)
347 public synchronized Sone setReplies(Collection<Reply> replies) {
348 this.replies.clear();
349 this.replies.addAll(replies);
350 modificationCounter++;
355 * Adds a reply to this Sone. If the given reply was not made by this Sone,
356 * nothing is added to this Sone.
361 public synchronized void addReply(Reply reply) {
362 if (reply.getSone().equals(this) && replies.add(reply)) {
363 modificationCounter++;
368 * Removes a reply from this Sone.
371 * The reply to remove
373 public synchronized void removeReply(Reply reply) {
374 if (reply.getSone().equals(this) && replies.remove(reply)) {
375 modificationCounter++;
380 * Returns the modification counter.
382 * @return The modification counter
384 public synchronized long getModificationCounter() {
385 return modificationCounter;
389 * Sets the modification counter.
391 * @param modificationCounter
392 * The new modification counter
394 public synchronized void setModificationCounter(long modificationCounter) {
395 this.modificationCounter = modificationCounter;
399 * Updates the suggested edition in both the request URI and the insert URI.
402 * The request URI that resulted from an insert
404 public void updateUris(FreenetURI requestUri) {
405 /* TODO - check for the correct URI. */
406 long latestEdition = requestUri.getSuggestedEdition();
407 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
408 if (this.insertUri != null) {
409 this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
421 public int hashCode() {
422 return id.hashCode();
429 public boolean equals(Object object) {
430 if (!(object instanceof Sone)) {
433 return ((Sone) object).id.equals(id);
440 public String toString() {
441 return getClass().getName() + "[id=" + id + ",name=" + name + ",requestUri=" + requestUri + ",insertUri=" + insertUri + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + ")]";