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 /** All friend Sones. */
51 private final Set<Sone> friendSones = new HashSet<Sone>();
53 /** Modification count. */
54 private volatile long modificationCounter = 0;
62 * The name of the Sone
64 * The request URI of the Sone
66 public Sone(UUID id, String name, FreenetURI requestUri) {
67 this(id, name, requestUri, null);
76 * The name of the Sone
78 * The request URI of the Sone
80 * The insert URI of the Sone
82 public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
85 this.requestUri = requestUri;
86 this.insertUri = insertUri;
94 * Returns the ID of this Sone.
96 * @return The ID of this Sone
98 public String getId() {
103 * Returns the name of this Sone.
105 * @return The name of this Sone
107 public String getName() {
112 * Returns the request URI of this Sone.
114 * @return The request URI of this Sone
116 public FreenetURI getRequestUri() {
121 * Returns the insert URI of this Sone.
123 * @return The insert URI of this Sone
125 public FreenetURI getInsertUri() {
130 * Returns all friend Sones of this Sone.
132 * @return The friend Sones of this Sone
134 public Set<Sone> getFriendSones() {
135 return Collections.unmodifiableSet(friendSones);
139 * Returns whether this Sone has the given Sone as a friend Sone.
142 * The friend Sone to check for
143 * @return {@code true} if this Sone has the given Sone as a friend,
144 * {@code false} otherwise
146 public boolean hasFriendSone(Sone friendSone) {
147 return friendSones.contains(friendSone);
151 * Adds the given Sone as a friend Sone.
154 * The friend Sone to add
155 * @return This Sone (for method chaining)
157 public synchronized Sone addFriendSone(Sone friendSone) {
158 if (friendSones.add(friendSone)) {
159 modificationCounter++;
165 * Removes the given Sone as a friend Sone.
168 * The friend Sone to remove
169 * @return This Sone (for method chaining)
171 public synchronized Sone removeFriendSone(Sone friendSone) {
172 if (friendSones.remove(friendSone)) {
173 modificationCounter++;
179 * Returns the modification counter.
181 * @return The modification counter
183 public synchronized long getModificationCounter() {
184 return modificationCounter;
188 * Sets the modification counter.
190 * @param modificationCounter
191 * The new modification counter
193 public synchronized void setModificationCounter(long modificationCounter) {
194 this.modificationCounter = modificationCounter;
205 public int hashCode() {
206 return id.hashCode();
213 public boolean equals(Object object) {
214 if (!(object instanceof Sone)) {
217 return ((Sone) object).id.equals(id);