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