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.HashSet;
23 import java.util.List;
25 import java.util.UUID;
27 import freenet.keys.FreenetURI;
30 * A Sone defines everything about a user: her profile, her status updates, her
31 * replies, her likes and dislikes, etc.
33 * Operations that modify the Sone need to synchronize on the Sone in question.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 /** A GUID for this Sone. */
40 private final UUID id;
42 /** The name of this Sone. */
43 private final String name;
45 /** The URI under which the Sone is stored in Freenet. */
46 private final FreenetURI requestUri;
48 /** The URI used to insert a new version of this Sone. */
49 /* This will be null for remote Sones! */
50 private final FreenetURI insertUri;
52 /** The profile of this Sone. */
53 private Profile profile;
55 /** All friend Sones. */
56 private final Set<Sone> friendSones = new HashSet<Sone>();
59 private final List<Post> posts = new ArrayList<Post>();
62 private final Set<Reply> replies = new HashSet<Reply>();
64 /** Modification count. */
65 private volatile long modificationCounter = 0;
73 * The name of the Sone
75 * The request URI of the Sone
77 public Sone(UUID id, String name, FreenetURI requestUri) {
78 this(id, name, requestUri, null);
87 * The name of the Sone
89 * The request URI of the Sone
91 * The insert URI of the Sone
93 public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
96 this.requestUri = requestUri;
97 this.insertUri = insertUri;
105 * Returns the ID of this Sone.
107 * @return The ID of this Sone
109 public String getId() {
110 return id.toString();
114 * Returns the name of this Sone.
116 * @return The name of this Sone
118 public String getName() {
123 * Returns the request URI of this Sone.
125 * @return The request URI of this Sone
127 public FreenetURI getRequestUri() {
132 * Returns the insert URI of this Sone.
134 * @return The insert URI of this Sone
136 public FreenetURI getInsertUri() {
141 * Returns a copy of the profile. If you want to update values in the
142 * profile of this Sone, update the values in the returned {@link Profile}
143 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
145 * @return A copy of the profile
147 public Profile getProfile() {
148 return new Profile(profile);
152 * Sets the profile of this Sone. A copy of the given profile is stored so
153 * that subsequent modifications of the given profile are not reflected in
159 public synchronized void setProfile(Profile profile) {
160 this.profile = new Profile(profile);
161 modificationCounter++;
165 * Returns all friend Sones of this Sone.
167 * @return The friend Sones of this Sone
169 public Set<Sone> getFriendSones() {
170 return Collections.unmodifiableSet(friendSones);
174 * Returns whether this Sone has the given Sone as a friend Sone.
177 * The friend Sone to check for
178 * @return {@code true} if this Sone has the given Sone as a friend,
179 * {@code false} otherwise
181 public boolean hasFriendSone(Sone friendSone) {
182 return friendSones.contains(friendSone);
186 * Adds the given Sone as a friend Sone.
189 * The friend Sone to add
190 * @return This Sone (for method chaining)
192 public synchronized Sone addFriendSone(Sone friendSone) {
193 if (friendSones.add(friendSone)) {
194 modificationCounter++;
200 * Removes the given Sone as a friend Sone.
203 * The friend Sone to remove
204 * @return This Sone (for method chaining)
206 public synchronized Sone removeFriendSone(Sone friendSone) {
207 if (friendSones.remove(friendSone)) {
208 modificationCounter++;
214 * Returns the list of posts of this Sone.
216 * @return All posts of this Sone
218 public List<Post> getPosts() {
219 return Collections.unmodifiableList(posts);
223 * Adds the given post to this Sone. The post will not be added if its
224 * {@link Post#getSone() Sone} is not this Sone.
229 public synchronized void addPost(Post post) {
230 if (post.getSone().equals(this) && posts.add(post)) {
231 modificationCounter++;
236 * Removes the given post from this Sone.
241 public synchronized void removePost(Post post) {
242 if (post.getSone().equals(this) && posts.remove(post)) {
243 modificationCounter++;
248 * Returns all replies this Sone made.
250 * @return All replies this Sone made
252 public Set<Reply> getReplies() {
253 return Collections.unmodifiableSet(replies);
257 * Adds a reply to this Sone. If the given reply was not made by this Sone,
258 * nothing is added to this Sone.
263 public synchronized void addReply(Reply reply) {
264 if (reply.getSone().equals(this) && replies.add(reply)) {
265 modificationCounter++;
270 * Removes a reply from this Sone.
273 * The reply to remove
275 public synchronized void removeReply(Reply reply) {
276 if (reply.getSone().equals(this) && replies.remove(reply)) {
277 modificationCounter++;
282 * Returns the modification counter.
284 * @return The modification counter
286 public synchronized long getModificationCounter() {
287 return modificationCounter;
291 * Sets the modification counter.
293 * @param modificationCounter
294 * The new modification counter
296 public synchronized void setModificationCounter(long modificationCounter) {
297 this.modificationCounter = modificationCounter;
308 public int hashCode() {
309 return id.hashCode();
316 public boolean equals(Object object) {
317 if (!(object instanceof Sone)) {
320 return ((Sone) object).id.equals(id);