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.Collections;
21 import java.util.HashSet;
23 import java.util.UUID;
25 import freenet.keys.FreenetURI;
28 * A Sone defines everything about a user: the {@link User} itself, her profile,
31 * Operations that modify the Sone need to synchronize on the Sone in question.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 /** A GUID for this Sone. */
38 private final UUID id;
40 /** The name of this Sone. */
41 private final String name;
43 /** The URI under which the Sone is stored in Freenet. */
44 private final FreenetURI requestUri;
46 /** The URI used to insert a new version of this Sone. */
47 /* This will be null for remote Sones! */
48 private final FreenetURI insertUri;
50 /** The profile of this Sone. */
51 private Profile profile;
53 /** All friend Sones. */
54 private final Set<Sone> friendSones = new HashSet<Sone>();
56 /** Modification count. */
57 private volatile long modificationCounter = 0;
65 * The name of the Sone
67 * The request URI of the Sone
69 public Sone(UUID id, String name, FreenetURI requestUri) {
70 this(id, name, requestUri, null);
79 * The name of the Sone
81 * The request URI of the Sone
83 * The insert URI of the Sone
85 public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
88 this.requestUri = requestUri;
89 this.insertUri = insertUri;
97 * Returns the ID of this Sone.
99 * @return The ID of this Sone
101 public String getId() {
102 return id.toString();
106 * Returns the name of this Sone.
108 * @return The name of this Sone
110 public String getName() {
115 * Returns the request URI of this Sone.
117 * @return The request URI of this Sone
119 public FreenetURI getRequestUri() {
124 * Returns the insert URI of this Sone.
126 * @return The insert URI of this Sone
128 public FreenetURI getInsertUri() {
133 * Returns a copy of the profile. If you want to update values in the
134 * profile of this Sone, update the values in the returned {@link Profile}
135 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
137 * @return A copy of the profile
139 public Profile getProfile() {
140 return new Profile(profile);
144 * Sets the profile of this Sone. A copy of the given profile is stored so
145 * that subsequent modifications of the given profile are not reflected in
151 public synchronized void setProfile(Profile profile) {
152 this.profile = new Profile(profile);
153 modificationCounter++;
157 * Returns all friend Sones of this Sone.
159 * @return The friend Sones of this Sone
161 public Set<Sone> getFriendSones() {
162 return Collections.unmodifiableSet(friendSones);
166 * Returns whether this Sone has the given Sone as a friend Sone.
169 * The friend Sone to check for
170 * @return {@code true} if this Sone has the given Sone as a friend,
171 * {@code false} otherwise
173 public boolean hasFriendSone(Sone friendSone) {
174 return friendSones.contains(friendSone);
178 * Adds the given Sone as a friend Sone.
181 * The friend Sone to add
182 * @return This Sone (for method chaining)
184 public synchronized Sone addFriendSone(Sone friendSone) {
185 if (friendSones.add(friendSone)) {
186 modificationCounter++;
192 * Removes the given Sone as a friend Sone.
195 * The friend Sone to remove
196 * @return This Sone (for method chaining)
198 public synchronized Sone removeFriendSone(Sone friendSone) {
199 if (friendSones.remove(friendSone)) {
200 modificationCounter++;
206 * Returns the modification counter.
208 * @return The modification counter
210 public synchronized long getModificationCounter() {
211 return modificationCounter;
215 * Sets the modification counter.
217 * @param modificationCounter
218 * The new modification counter
220 public synchronized void setModificationCounter(long modificationCounter) {
221 this.modificationCounter = modificationCounter;
232 public int hashCode() {
233 return id.hashCode();
240 public boolean equals(Object object) {
241 if (!(object instanceof Sone)) {
244 return ((Sone) object).id.equals(id);