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.Collections;
22 import java.util.Comparator;
23 import java.util.HashSet;
24 import java.util.List;
26 import java.util.UUID;
28 import freenet.keys.FreenetURI;
31 * A Sone defines everything about a user: her profile, her status updates, her
32 * replies, her likes and dislikes, etc.
34 * Operations that modify the Sone need to synchronize on the Sone in question.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 /** A GUID for this Sone. */
41 private final UUID id;
43 /** The name of this Sone. */
44 private final String name;
46 /** The URI under which the Sone is stored in Freenet. */
47 private FreenetURI requestUri;
49 /** The URI used to insert a new version of this Sone. */
50 /* This will be null for remote Sones! */
51 private FreenetURI insertUri;
53 /** The profile of this Sone. */
54 private Profile profile;
56 /** All friend Sones. */
57 private final Set<Sone> friendSones = new HashSet<Sone>();
60 private final List<Post> posts = new ArrayList<Post>();
63 private final Set<Reply> replies = new HashSet<Reply>();
65 /** Modification count. */
66 private volatile long modificationCounter = 0;
74 * The name of the Sone
76 * The request URI of the Sone
78 public Sone(UUID id, String name, FreenetURI requestUri) {
79 this(id, name, requestUri, null);
88 * The name of the Sone
90 * The request URI of the Sone
92 * The insert URI of the Sone
94 public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
97 this.requestUri = requestUri;
98 this.insertUri = insertUri;
106 * Returns the ID of this Sone.
108 * @return The ID of this Sone
110 public String getId() {
111 return id.toString();
115 * Returns the name of this Sone.
117 * @return The name of this Sone
119 public String getName() {
124 * Returns the request URI of this Sone.
126 * @return The request URI of this Sone
128 public FreenetURI getRequestUri() {
133 * Returns the insert URI of this Sone.
135 * @return The insert URI of this Sone
137 public FreenetURI getInsertUri() {
142 * Returns a copy of the profile. If you want to update values in the
143 * profile of this Sone, update the values in the returned {@link Profile}
144 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
146 * @return A copy of the profile
148 public Profile getProfile() {
149 return new Profile(profile);
153 * Sets the profile of this Sone. A copy of the given profile is stored so
154 * that subsequent modifications of the given profile are not reflected in
160 public synchronized void setProfile(Profile profile) {
161 this.profile = new Profile(profile);
162 modificationCounter++;
166 * Returns all friend Sones of this Sone.
168 * @return The friend Sones of this Sone
170 public Set<Sone> getFriendSones() {
171 return Collections.unmodifiableSet(friendSones);
175 * Returns whether this Sone has the given Sone as a friend Sone.
178 * The friend Sone to check for
179 * @return {@code true} if this Sone has the given Sone as a friend,
180 * {@code false} otherwise
182 public boolean hasFriendSone(Sone friendSone) {
183 return friendSones.contains(friendSone);
187 * Adds the given Sone as a friend Sone.
190 * The friend Sone to add
191 * @return This Sone (for method chaining)
193 public synchronized Sone addFriendSone(Sone friendSone) {
194 if (friendSones.add(friendSone)) {
195 modificationCounter++;
201 * Removes the given Sone as a friend Sone.
204 * The friend Sone to remove
205 * @return This Sone (for method chaining)
207 public synchronized Sone removeFriendSone(Sone friendSone) {
208 if (friendSones.remove(friendSone)) {
209 modificationCounter++;
215 * Returns the list of posts of this Sone, sorted by time, newest first.
217 * @return All posts of this Sone
219 public List<Post> getPosts() {
220 List<Post> sortedPosts = new ArrayList<Post>(posts);
221 Collections.sort(sortedPosts, new Comparator<Post>() {
224 public int compare(Post leftPost, Post rightPost) {
225 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
233 * Adds the given post to this Sone. The post will not be added if its
234 * {@link Post#getSone() Sone} is not this Sone.
239 public synchronized void addPost(Post post) {
240 if (post.getSone().equals(this) && posts.add(post)) {
241 modificationCounter++;
246 * Removes the given post from this Sone.
251 public synchronized void removePost(Post post) {
252 if (post.getSone().equals(this) && posts.remove(post)) {
253 modificationCounter++;
258 * Returns all replies this Sone made.
260 * @return All replies this Sone made
262 public Set<Reply> getReplies() {
263 return Collections.unmodifiableSet(replies);
267 * Adds a reply to this Sone. If the given reply was not made by this Sone,
268 * nothing is added to this Sone.
273 public synchronized void addReply(Reply reply) {
274 if (reply.getSone().equals(this) && replies.add(reply)) {
275 modificationCounter++;
280 * Removes a reply from this Sone.
283 * The reply to remove
285 public synchronized void removeReply(Reply reply) {
286 if (reply.getSone().equals(this) && replies.remove(reply)) {
287 modificationCounter++;
292 * Returns the modification counter.
294 * @return The modification counter
296 public synchronized long getModificationCounter() {
297 return modificationCounter;
301 * Sets the modification counter.
303 * @param modificationCounter
304 * The new modification counter
306 public synchronized void setModificationCounter(long modificationCounter) {
307 this.modificationCounter = modificationCounter;
311 * Updates the suggested edition in both the request URI and the insert URI.
314 * The request URI that resulted from an insert
316 public void updateUris(FreenetURI requestUri) {
317 /* TODO - check for the correct URI. */
318 long latestEdition = requestUri.getSuggestedEdition();
319 this.requestUri = this.requestUri.setSuggestedEdition(latestEdition);
320 this.insertUri = this.insertUri.setSuggestedEdition(latestEdition);
331 public int hashCode() {
332 return id.hashCode();
339 public boolean equals(Object object) {
340 if (!(object instanceof Sone)) {
343 return ((Sone) object).id.equals(id);