Add synchronization note.
[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         /** All friend Sones. */
51         private final Set<Sone> friendSones = new HashSet<Sone>();
52
53         /** Modification count. */
54         private volatile long modificationCounter = 0;
55
56         /**
57          * Creates a new Sone.
58          *
59          * @param id
60          *            The ID of this Sone
61          * @param name
62          *            The name of the Sone
63          * @param requestUri
64          *            The request URI of the Sone
65          */
66         public Sone(UUID id, String name, FreenetURI requestUri) {
67                 this(id, name, requestUri, null);
68         }
69
70         /**
71          * Creates a new Sone.
72          *
73          * @param id
74          *            The ID of this Sone
75          * @param name
76          *            The name of the Sone
77          * @param requestUri
78          *            The request URI of the Sone
79          * @param insertUri
80          *            The insert URI of the Sone
81          */
82         public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
83                 this.id = id;
84                 this.name = name;
85                 this.requestUri = requestUri;
86                 this.insertUri = insertUri;
87         }
88
89         //
90         // ACCESSORS
91         //
92
93         /**
94          * Returns the ID of this Sone.
95          *
96          * @return The ID of this Sone
97          */
98         public String getId() {
99                 return id.toString();
100         }
101
102         /**
103          * Returns the name of this Sone.
104          *
105          * @return The name of this Sone
106          */
107         public String getName() {
108                 return name;
109         }
110
111         /**
112          * Returns the request URI of this Sone.
113          *
114          * @return The request URI of this Sone
115          */
116         public FreenetURI getRequestUri() {
117                 return requestUri;
118         }
119
120         /**
121          * Returns the insert URI of this Sone.
122          *
123          * @return The insert URI of this Sone
124          */
125         public FreenetURI getInsertUri() {
126                 return insertUri;
127         }
128
129         /**
130          * Returns all friend Sones of this Sone.
131          *
132          * @return The friend Sones of this Sone
133          */
134         public Set<Sone> getFriendSones() {
135                 return Collections.unmodifiableSet(friendSones);
136         }
137
138         /**
139          * Returns whether this Sone has the given Sone as a friend Sone.
140          *
141          * @param friendSone
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
145          */
146         public boolean hasFriendSone(Sone friendSone) {
147                 return friendSones.contains(friendSone);
148         }
149
150         /**
151          * Adds the given Sone as a friend Sone.
152          *
153          * @param friendSone
154          *            The friend Sone to add
155          * @return This Sone (for method chaining)
156          */
157         public synchronized Sone addFriendSone(Sone friendSone) {
158                 if (friendSones.add(friendSone)) {
159                         modificationCounter++;
160                 }
161                 return this;
162         }
163
164         /**
165          * Removes the given Sone as a friend Sone.
166          *
167          * @param friendSone
168          *            The friend Sone to remove
169          * @return This Sone (for method chaining)
170          */
171         public synchronized Sone removeFriendSone(Sone friendSone) {
172                 if (friendSones.remove(friendSone)) {
173                         modificationCounter++;
174                 }
175                 return this;
176         }
177
178         /**
179          * Returns the modification counter.
180          *
181          * @return The modification counter
182          */
183         public synchronized long getModificationCounter() {
184                 return modificationCounter;
185         }
186
187         /**
188          * Sets the modification counter.
189          *
190          * @param modificationCounter
191          *            The new modification counter
192          */
193         public synchronized void setModificationCounter(long modificationCounter) {
194                 this.modificationCounter = modificationCounter;
195         }
196
197         //
198         // OBJECT METHODS
199         //
200
201         /**
202          * {@inheritDoc}
203          */
204         @Override
205         public int hashCode() {
206                 return id.hashCode();
207         }
208
209         /**
210          * {@inheritDoc}
211          */
212         @Override
213         public boolean equals(Object object) {
214                 if (!(object instanceof Sone)) {
215                         return false;
216                 }
217                 return ((Sone) object).id.equals(id);
218         }
219
220 }