Add profile to Sone.
[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.
145          *
146          * @param profile
147          *            The profile to set
148          */
149         public synchronized void setProfile(Profile profile) {
150                 this.profile = profile;
151                 modificationCounter++;
152         }
153
154         /**
155          * Returns all friend Sones of this Sone.
156          *
157          * @return The friend Sones of this Sone
158          */
159         public Set<Sone> getFriendSones() {
160                 return Collections.unmodifiableSet(friendSones);
161         }
162
163         /**
164          * Returns whether this Sone has the given Sone as a friend Sone.
165          *
166          * @param friendSone
167          *            The friend Sone to check for
168          * @return {@code true} if this Sone has the given Sone as a friend,
169          *         {@code false} otherwise
170          */
171         public boolean hasFriendSone(Sone friendSone) {
172                 return friendSones.contains(friendSone);
173         }
174
175         /**
176          * Adds the given Sone as a friend Sone.
177          *
178          * @param friendSone
179          *            The friend Sone to add
180          * @return This Sone (for method chaining)
181          */
182         public synchronized Sone addFriendSone(Sone friendSone) {
183                 if (friendSones.add(friendSone)) {
184                         modificationCounter++;
185                 }
186                 return this;
187         }
188
189         /**
190          * Removes the given Sone as a friend Sone.
191          *
192          * @param friendSone
193          *            The friend Sone to remove
194          * @return This Sone (for method chaining)
195          */
196         public synchronized Sone removeFriendSone(Sone friendSone) {
197                 if (friendSones.remove(friendSone)) {
198                         modificationCounter++;
199                 }
200                 return this;
201         }
202
203         /**
204          * Returns the modification counter.
205          *
206          * @return The modification counter
207          */
208         public synchronized long getModificationCounter() {
209                 return modificationCounter;
210         }
211
212         /**
213          * Sets the modification counter.
214          *
215          * @param modificationCounter
216          *            The new modification counter
217          */
218         public synchronized void setModificationCounter(long modificationCounter) {
219                 this.modificationCounter = modificationCounter;
220         }
221
222         //
223         // OBJECT METHODS
224         //
225
226         /**
227          * {@inheritDoc}
228          */
229         @Override
230         public int hashCode() {
231                 return id.hashCode();
232         }
233
234         /**
235          * {@inheritDoc}
236          */
237         @Override
238         public boolean equals(Object object) {
239                 if (!(object instanceof Sone)) {
240                         return false;
241                 }
242                 return ((Sone) object).id.equals(id);
243         }
244
245 }