Store a copy of the given profile.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
1 /*
2  * FreenetSone - Sone.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.UUID;
24
25 import freenet.keys.FreenetURI;
26
27 /**
28  * A Sone defines everything about a user: the {@link User} itself, her profile,
29  * her status updates.
30  * <p>
31  * Operations that modify the Sone need to synchronize on the Sone in question.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class Sone {
36
37         /** A GUID for this Sone. */
38         private final UUID id;
39
40         /** The name of this Sone. */
41         private final String name;
42
43         /** The URI under which the Sone is stored in Freenet. */
44         private final FreenetURI requestUri;
45
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;
49
50         /** The profile of this Sone. */
51         private Profile profile;
52
53         /** All friend Sones. */
54         private final Set<Sone> friendSones = new HashSet<Sone>();
55
56         /** Modification count. */
57         private volatile long modificationCounter = 0;
58
59         /**
60          * Creates a new Sone.
61          *
62          * @param id
63          *            The ID of this Sone
64          * @param name
65          *            The name of the Sone
66          * @param requestUri
67          *            The request URI of the Sone
68          */
69         public Sone(UUID id, String name, FreenetURI requestUri) {
70                 this(id, name, requestUri, null);
71         }
72
73         /**
74          * Creates a new Sone.
75          *
76          * @param id
77          *            The ID of this Sone
78          * @param name
79          *            The name of the Sone
80          * @param requestUri
81          *            The request URI of the Sone
82          * @param insertUri
83          *            The insert URI of the Sone
84          */
85         public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
86                 this.id = id;
87                 this.name = name;
88                 this.requestUri = requestUri;
89                 this.insertUri = insertUri;
90         }
91
92         //
93         // ACCESSORS
94         //
95
96         /**
97          * Returns the ID of this Sone.
98          *
99          * @return The ID of this Sone
100          */
101         public String getId() {
102                 return id.toString();
103         }
104
105         /**
106          * Returns the name of this Sone.
107          *
108          * @return The name of this Sone
109          */
110         public String getName() {
111                 return name;
112         }
113
114         /**
115          * Returns the request URI of this Sone.
116          *
117          * @return The request URI of this Sone
118          */
119         public FreenetURI getRequestUri() {
120                 return requestUri;
121         }
122
123         /**
124          * Returns the insert URI of this Sone.
125          *
126          * @return The insert URI of this Sone
127          */
128         public FreenetURI getInsertUri() {
129                 return insertUri;
130         }
131
132         /**
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.
136          *
137          * @return A copy of the profile
138          */
139         public Profile getProfile() {
140                 return new Profile(profile);
141         }
142
143         /**
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
146          * this Sone!
147          *
148          * @param profile
149          *            The profile to set
150          */
151         public synchronized void setProfile(Profile profile) {
152                 this.profile = new Profile(profile);
153                 modificationCounter++;
154         }
155
156         /**
157          * Returns all friend Sones of this Sone.
158          *
159          * @return The friend Sones of this Sone
160          */
161         public Set<Sone> getFriendSones() {
162                 return Collections.unmodifiableSet(friendSones);
163         }
164
165         /**
166          * Returns whether this Sone has the given Sone as a friend Sone.
167          *
168          * @param friendSone
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
172          */
173         public boolean hasFriendSone(Sone friendSone) {
174                 return friendSones.contains(friendSone);
175         }
176
177         /**
178          * Adds the given Sone as a friend Sone.
179          *
180          * @param friendSone
181          *            The friend Sone to add
182          * @return This Sone (for method chaining)
183          */
184         public synchronized Sone addFriendSone(Sone friendSone) {
185                 if (friendSones.add(friendSone)) {
186                         modificationCounter++;
187                 }
188                 return this;
189         }
190
191         /**
192          * Removes the given Sone as a friend Sone.
193          *
194          * @param friendSone
195          *            The friend Sone to remove
196          * @return This Sone (for method chaining)
197          */
198         public synchronized Sone removeFriendSone(Sone friendSone) {
199                 if (friendSones.remove(friendSone)) {
200                         modificationCounter++;
201                 }
202                 return this;
203         }
204
205         /**
206          * Returns the modification counter.
207          *
208          * @return The modification counter
209          */
210         public synchronized long getModificationCounter() {
211                 return modificationCounter;
212         }
213
214         /**
215          * Sets the modification counter.
216          *
217          * @param modificationCounter
218          *            The new modification counter
219          */
220         public synchronized void setModificationCounter(long modificationCounter) {
221                 this.modificationCounter = modificationCounter;
222         }
223
224         //
225         // OBJECT METHODS
226         //
227
228         /**
229          * {@inheritDoc}
230          */
231         @Override
232         public int hashCode() {
233                 return id.hashCode();
234         }
235
236         /**
237          * {@inheritDoc}
238          */
239         @Override
240         public boolean equals(Object object) {
241                 if (!(object instanceof Sone)) {
242                         return false;
243                 }
244                 return ((Sone) object).id.equals(id);
245         }
246
247 }