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: the {@link User} itself, her profile,
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>();
61 /** Modification count. */
62 private volatile long modificationCounter = 0;
70 * The name of the Sone
72 * The request URI of the Sone
74 public Sone(UUID id, String name, FreenetURI requestUri) {
75 this(id, name, requestUri, null);
84 * The name of the Sone
86 * The request URI of the Sone
88 * The insert URI of the Sone
90 public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
93 this.requestUri = requestUri;
94 this.insertUri = insertUri;
102 * Returns the ID of this Sone.
104 * @return The ID of this Sone
106 public String getId() {
107 return id.toString();
111 * Returns the name of this Sone.
113 * @return The name of this Sone
115 public String getName() {
120 * Returns the request URI of this Sone.
122 * @return The request URI of this Sone
124 public FreenetURI getRequestUri() {
129 * Returns the insert URI of this Sone.
131 * @return The insert URI of this Sone
133 public FreenetURI getInsertUri() {
138 * Returns a copy of the profile. If you want to update values in the
139 * profile of this Sone, update the values in the returned {@link Profile}
140 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
142 * @return A copy of the profile
144 public Profile getProfile() {
145 return new Profile(profile);
149 * Sets the profile of this Sone. A copy of the given profile is stored so
150 * that subsequent modifications of the given profile are not reflected in
156 public synchronized void setProfile(Profile profile) {
157 this.profile = new Profile(profile);
158 modificationCounter++;
162 * Returns all friend Sones of this Sone.
164 * @return The friend Sones of this Sone
166 public Set<Sone> getFriendSones() {
167 return Collections.unmodifiableSet(friendSones);
171 * Returns whether this Sone has the given Sone as a friend Sone.
174 * The friend Sone to check for
175 * @return {@code true} if this Sone has the given Sone as a friend,
176 * {@code false} otherwise
178 public boolean hasFriendSone(Sone friendSone) {
179 return friendSones.contains(friendSone);
183 * Adds the given Sone as a friend Sone.
186 * The friend Sone to add
187 * @return This Sone (for method chaining)
189 public synchronized Sone addFriendSone(Sone friendSone) {
190 if (friendSones.add(friendSone)) {
191 modificationCounter++;
197 * Removes the given Sone as a friend Sone.
200 * The friend Sone to remove
201 * @return This Sone (for method chaining)
203 public synchronized Sone removeFriendSone(Sone friendSone) {
204 if (friendSones.remove(friendSone)) {
205 modificationCounter++;
211 * Returns the list of posts of this Sone.
213 * @return All posts of this Sone
215 public List<Post> getPosts() {
216 return Collections.unmodifiableList(posts);
220 * Adds a post with the given text to this Sone.
225 public synchronized void addPost(String text) {
226 Post post = new Post(this, System.currentTimeMillis(), text);
227 if (posts.add(post)) {
228 modificationCounter++;
233 * Removes the given post from this Sone.
238 public synchronized void removePost(Post post) {
239 if (post.getSone().equals(this) && posts.remove(post)) {
240 modificationCounter++;
245 * Returns the modification counter.
247 * @return The modification counter
249 public synchronized long getModificationCounter() {
250 return modificationCounter;
254 * Sets the modification counter.
256 * @param modificationCounter
257 * The new modification counter
259 public synchronized void setModificationCounter(long modificationCounter) {
260 this.modificationCounter = modificationCounter;
271 public int hashCode() {
272 return id.hashCode();
279 public boolean equals(Object object) {
280 if (!(object instanceof Sone)) {
283 return ((Sone) object).id.equals(id);