Check if post belongs to this Sone before trying to delete it.
[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.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.UUID;
26
27 import freenet.keys.FreenetURI;
28
29 /**
30  * A Sone defines everything about a user: the {@link User} itself, her profile,
31  * her status updates.
32  * <p>
33  * Operations that modify the Sone need to synchronize on the Sone in question.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class Sone {
38
39         /** A GUID for this Sone. */
40         private final UUID id;
41
42         /** The name of this Sone. */
43         private final String name;
44
45         /** The URI under which the Sone is stored in Freenet. */
46         private final FreenetURI requestUri;
47
48         /** The URI used to insert a new version of this Sone. */
49         /* This will be null for remote Sones! */
50         private final FreenetURI insertUri;
51
52         /** The profile of this Sone. */
53         private Profile profile;
54
55         /** All friend Sones. */
56         private final Set<Sone> friendSones = new HashSet<Sone>();
57
58         /** All posts. */
59         private final List<Post> posts = new ArrayList<Post>();
60
61         /** Modification count. */
62         private volatile long modificationCounter = 0;
63
64         /**
65          * Creates a new Sone.
66          *
67          * @param id
68          *            The ID of this Sone
69          * @param name
70          *            The name of the Sone
71          * @param requestUri
72          *            The request URI of the Sone
73          */
74         public Sone(UUID id, String name, FreenetURI requestUri) {
75                 this(id, name, requestUri, null);
76         }
77
78         /**
79          * Creates a new Sone.
80          *
81          * @param id
82          *            The ID of this Sone
83          * @param name
84          *            The name of the Sone
85          * @param requestUri
86          *            The request URI of the Sone
87          * @param insertUri
88          *            The insert URI of the Sone
89          */
90         public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
91                 this.id = id;
92                 this.name = name;
93                 this.requestUri = requestUri;
94                 this.insertUri = insertUri;
95         }
96
97         //
98         // ACCESSORS
99         //
100
101         /**
102          * Returns the ID of this Sone.
103          *
104          * @return The ID of this Sone
105          */
106         public String getId() {
107                 return id.toString();
108         }
109
110         /**
111          * Returns the name of this Sone.
112          *
113          * @return The name of this Sone
114          */
115         public String getName() {
116                 return name;
117         }
118
119         /**
120          * Returns the request URI of this Sone.
121          *
122          * @return The request URI of this Sone
123          */
124         public FreenetURI getRequestUri() {
125                 return requestUri;
126         }
127
128         /**
129          * Returns the insert URI of this Sone.
130          *
131          * @return The insert URI of this Sone
132          */
133         public FreenetURI getInsertUri() {
134                 return insertUri;
135         }
136
137         /**
138          * Returns a copy of the profile. If you want to update values in the
139          * profile of this Sone, update the values in the returned {@link Profile}
140          * and use {@link #setProfile(Profile)} to change the profile in this Sone.
141          *
142          * @return A copy of the profile
143          */
144         public Profile getProfile() {
145                 return new Profile(profile);
146         }
147
148         /**
149          * Sets the profile of this Sone. A copy of the given profile is stored so
150          * that subsequent modifications of the given profile are not reflected in
151          * this Sone!
152          *
153          * @param profile
154          *            The profile to set
155          */
156         public synchronized void setProfile(Profile profile) {
157                 this.profile = new Profile(profile);
158                 modificationCounter++;
159         }
160
161         /**
162          * Returns all friend Sones of this Sone.
163          *
164          * @return The friend Sones of this Sone
165          */
166         public Set<Sone> getFriendSones() {
167                 return Collections.unmodifiableSet(friendSones);
168         }
169
170         /**
171          * Returns whether this Sone has the given Sone as a friend Sone.
172          *
173          * @param friendSone
174          *            The friend Sone to check for
175          * @return {@code true} if this Sone has the given Sone as a friend,
176          *         {@code false} otherwise
177          */
178         public boolean hasFriendSone(Sone friendSone) {
179                 return friendSones.contains(friendSone);
180         }
181
182         /**
183          * Adds the given Sone as a friend Sone.
184          *
185          * @param friendSone
186          *            The friend Sone to add
187          * @return This Sone (for method chaining)
188          */
189         public synchronized Sone addFriendSone(Sone friendSone) {
190                 if (friendSones.add(friendSone)) {
191                         modificationCounter++;
192                 }
193                 return this;
194         }
195
196         /**
197          * Removes the given Sone as a friend Sone.
198          *
199          * @param friendSone
200          *            The friend Sone to remove
201          * @return This Sone (for method chaining)
202          */
203         public synchronized Sone removeFriendSone(Sone friendSone) {
204                 if (friendSones.remove(friendSone)) {
205                         modificationCounter++;
206                 }
207                 return this;
208         }
209
210         /**
211          * Returns the list of posts of this Sone.
212          *
213          * @return All posts of this Sone
214          */
215         public List<Post> getPosts() {
216                 return Collections.unmodifiableList(posts);
217         }
218
219         /**
220          * Adds a post with the given text to this Sone.
221          *
222          * @param text
223          *            The text to post
224          */
225         public synchronized void addPost(String text) {
226                 Post post = new Post(this, System.currentTimeMillis(), text);
227                 if (posts.add(post)) {
228                         modificationCounter++;
229                 }
230         }
231
232         /**
233          * Removes the given post from this Sone.
234          *
235          * @param post
236          *            The post to remove
237          */
238         public synchronized void removePost(Post post) {
239                 if (post.getSone().equals(this) && posts.remove(post)) {
240                         modificationCounter++;
241                 }
242         }
243
244         /**
245          * Returns the modification counter.
246          *
247          * @return The modification counter
248          */
249         public synchronized long getModificationCounter() {
250                 return modificationCounter;
251         }
252
253         /**
254          * Sets the modification counter.
255          *
256          * @param modificationCounter
257          *            The new modification counter
258          */
259         public synchronized void setModificationCounter(long modificationCounter) {
260                 this.modificationCounter = modificationCounter;
261         }
262
263         //
264         // OBJECT METHODS
265         //
266
267         /**
268          * {@inheritDoc}
269          */
270         @Override
271         public int hashCode() {
272                 return id.hashCode();
273         }
274
275         /**
276          * {@inheritDoc}
277          */
278         @Override
279         public boolean equals(Object object) {
280                 if (!(object instanceof Sone)) {
281                         return false;
282                 }
283                 return ((Sone) object).id.equals(id);
284         }
285
286 }