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;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.util.logging.Logging;
31 import freenet.keys.FreenetURI;
34 * A Sone defines everything about a user: her profile, her status updates, her
35 * replies, her likes and dislikes, etc.
37 * Operations that modify the Sone need to synchronize on the Sone in question.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 private static final Logger logger = Logging.getLogger(Sone.class);
46 /** A GUID for this Sone. */
47 private final UUID id;
49 /** The name of this Sone. */
50 private final String name;
52 /** The URI under which the Sone is stored in Freenet. */
53 private FreenetURI requestUri;
55 /** The URI used to insert a new version of this Sone. */
56 /* This will be null for remote Sones! */
57 private FreenetURI insertUri;
59 /** The profile of this Sone. */
60 private Profile profile;
62 /** All friend Sones. */
63 private final Set<Sone> friendSones = new HashSet<Sone>();
66 private final List<Post> posts = new ArrayList<Post>();
69 private final Set<Reply> replies = new HashSet<Reply>();
71 /** Modification count. */
72 private volatile long modificationCounter = 0;
80 * The name of the Sone
82 * The request URI of the Sone
84 public Sone(UUID id, String name, FreenetURI requestUri) {
85 this(id, name, requestUri, null);
94 * The name of the Sone
96 * The request URI of the Sone
98 * The insert URI of the Sone
100 public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
103 this.requestUri = requestUri;
104 this.insertUri = insertUri;
112 * Returns the ID of this Sone.
114 * @return The ID of this Sone
116 public String getId() {
117 return id.toString();
121 * Returns the name of this Sone.
123 * @return The name of this Sone
125 public String getName() {
130 * Returns the request URI of this Sone.
132 * @return The request URI of this Sone
134 public FreenetURI getRequestUri() {
139 * Returns the insert URI of this Sone.
141 * @return The insert URI of this Sone
143 public FreenetURI getInsertUri() {
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.
152 * @return A copy of the profile
154 public Profile getProfile() {
155 return new Profile(profile);
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
166 public synchronized void setProfile(Profile profile) {
167 this.profile = new Profile(profile);
168 modificationCounter++;
172 * Returns all friend Sones of this Sone.
174 * @return The friend Sones of this Sone
176 public Set<Sone> getFriendSones() {
177 return Collections.unmodifiableSet(friendSones);
181 * Returns whether this Sone has the given Sone as a friend Sone.
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
188 public boolean hasFriendSone(Sone friendSone) {
189 return friendSones.contains(friendSone);
193 * Adds the given Sone as a friend Sone.
196 * The friend Sone to add
197 * @return This Sone (for method chaining)
199 public synchronized Sone addFriendSone(Sone friendSone) {
200 if (friendSones.add(friendSone)) {
201 modificationCounter++;
207 * Removes the given Sone as a friend Sone.
210 * The friend Sone to remove
211 * @return This Sone (for method chaining)
213 public synchronized Sone removeFriendSone(Sone friendSone) {
214 if (friendSones.remove(friendSone)) {
215 modificationCounter++;
221 * Returns the list of posts of this Sone, sorted by time, newest first.
223 * @return All posts of this Sone
225 public List<Post> getPosts() {
226 List<Post> sortedPosts = new ArrayList<Post>(posts);
227 Collections.sort(sortedPosts, new Comparator<Post>() {
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()));
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.
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++;
253 * Removes the given post from this Sone.
258 public synchronized void removePost(Post post) {
259 if (post.getSone().equals(this) && posts.remove(post)) {
260 modificationCounter++;
265 * Returns all replies this Sone made.
267 * @return All replies this Sone made
269 public Set<Reply> getReplies() {
270 return Collections.unmodifiableSet(replies);
274 * Adds a reply to this Sone. If the given reply was not made by this Sone,
275 * nothing is added to this Sone.
280 public synchronized void addReply(Reply reply) {
281 if (reply.getSone().equals(this) && replies.add(reply)) {
282 modificationCounter++;
287 * Removes a reply from this Sone.
290 * The reply to remove
292 public synchronized void removeReply(Reply reply) {
293 if (reply.getSone().equals(this) && replies.remove(reply)) {
294 modificationCounter++;
299 * Returns the modification counter.
301 * @return The modification counter
303 public synchronized long getModificationCounter() {
304 return modificationCounter;
308 * Sets the modification counter.
310 * @param modificationCounter
311 * The new modification counter
313 public synchronized void setModificationCounter(long modificationCounter) {
314 this.modificationCounter = modificationCounter;
318 * Updates the suggested edition in both the request URI and the insert URI.
321 * The request URI that resulted from an insert
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);
338 public int hashCode() {
339 return id.hashCode();
346 public boolean equals(Object object) {
347 if (!(object instanceof Sone)) {
350 return ((Sone) object).id.equals(id);
357 public String toString() {
358 return getClass().getName() + "[id=" + getId() + ",name=" + getName() + ",requestUri=" + getRequestUri() + ",insertUri=" + getInsertUri() + ",posts(" + posts.size() + "),replies(" + replies.size() + ")]";