Override hashCode().
[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
24 import freenet.keys.FreenetURI;
25
26 /**
27  * A Sone defines everything about a user: the {@link User} itself, her profile,
28  * her status updates.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class Sone {
33
34         /** The URI under which the Sone is stored in Freenet. */
35         private final FreenetURI requestUri;
36
37         /** The URI used to insert a new version of this Sone. */
38         /* This will be null for remote Sones! */
39         private final FreenetURI insertUri;
40
41         /** All friend Sones. */
42         private final Set<Sone> friendSones = new HashSet<Sone>();
43
44         /**
45          * Creates a new Sone.
46          *
47          * @param requestUri
48          *            The request URI of the Sone
49          */
50         public Sone(FreenetURI requestUri) {
51                 this(requestUri, null);
52         }
53
54         /**
55          * Creates a new Sone.
56          *
57          * @param requestUri
58          *            The request URI of the Sone
59          * @param insertUri
60          *            The insert URI of the Sone
61          */
62         public Sone(FreenetURI requestUri, FreenetURI insertUri) {
63                 this.requestUri = requestUri;
64                 this.insertUri = insertUri;
65         }
66
67         //
68         // ACCESSORS
69         //
70
71         /**
72          * Returns the request URI of this Sone.
73          *
74          * @return The request URI of this Sone
75          */
76         public FreenetURI requestUri() {
77                 return requestUri;
78         }
79
80         /**
81          * Returns the insert URI of this Sone.
82          *
83          * @return The insert URI of this Sone
84          */
85         public FreenetURI insertUri() {
86                 return insertUri;
87         }
88
89         /**
90          * Returns all friend Sones of this Sone.
91          *
92          * @return The friend Sones of this Sone
93          */
94         public Set<Sone> getFriendSones() {
95                 return Collections.unmodifiableSet(friendSones);
96         }
97
98         /**
99          * Returns whether this Sone has the given Sone as a friend Sone.
100          *
101          * @param friendSone
102          *            The friend Sone to check for
103          * @return {@code true} if this Sone has the given Sone as a friend,
104          *         {@code false} otherwise
105          */
106         public boolean hasFriendSone(Sone friendSone) {
107                 return friendSones.contains(friendSone);
108         }
109
110         /**
111          * Adds the given Sone as a friend Sone.
112          *
113          * @param friendSone
114          *            The friend Sone to add
115          * @return This Sone (for method chaining)
116          */
117         public Sone addFriendSone(Sone friendSone) {
118                 friendSones.add(friendSone);
119                 return this;
120         }
121
122         /**
123          * Removes the given Sone as a friend Sone.
124          *
125          * @param friendSone
126          *            The friend Sone to remove
127          * @return This Sone (for method chaining)
128          */
129         public Sone removeFriendSone(Sone friendSone) {
130                 friendSones.remove(friendSone);
131                 return this;
132         }
133
134         //
135         // OBJECT METHODS
136         //
137
138         /**
139          * {@inheritDoc}
140          */
141         @Override
142         public int hashCode() {
143                 /* TODO improve */
144                 return requestUri.hashCode();
145         }
146
147 }