Store the Sone ID instead of the Sone itself in the session.
[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         /**
52          * Creates a new Sone.
53          *
54          * @param id
55          *            The ID of this Sone
56          * @param name
57          *            The name of the Sone
58          * @param requestUri
59          *            The request URI of the Sone
60          */
61         public Sone(UUID id, String name, FreenetURI requestUri) {
62                 this(id, name, requestUri, null);
63         }
64
65         /**
66          * Creates a new Sone.
67          *
68          * @param id
69          *            The ID of this Sone
70          * @param name
71          *            The name of the Sone
72          * @param requestUri
73          *            The request URI of the Sone
74          * @param insertUri
75          *            The insert URI of the Sone
76          */
77         public Sone(UUID id, String name, FreenetURI requestUri, FreenetURI insertUri) {
78                 this.id = id;
79                 this.name = name;
80                 this.requestUri = requestUri;
81                 this.insertUri = insertUri;
82         }
83
84         //
85         // ACCESSORS
86         //
87
88         /**
89          * Returns the ID of this Sone.
90          *
91          * @return The ID of this Sone
92          */
93         public String getId() {
94                 return id.toString();
95         }
96
97         /**
98          * Returns the name of this Sone.
99          *
100          * @return The name of this Sone
101          */
102         public String getName() {
103                 return name;
104         }
105
106         /**
107          * Returns the request URI of this Sone.
108          *
109          * @return The request URI of this Sone
110          */
111         public FreenetURI getRequestUri() {
112                 return requestUri;
113         }
114
115         /**
116          * Returns the insert URI of this Sone.
117          *
118          * @return The insert URI of this Sone
119          */
120         public FreenetURI getInsertUri() {
121                 return insertUri;
122         }
123
124         /**
125          * Returns all friend Sones of this Sone.
126          *
127          * @return The friend Sones of this Sone
128          */
129         public Set<Sone> getFriendSones() {
130                 return Collections.unmodifiableSet(friendSones);
131         }
132
133         /**
134          * Returns whether this Sone has the given Sone as a friend Sone.
135          *
136          * @param friendSone
137          *            The friend Sone to check for
138          * @return {@code true} if this Sone has the given Sone as a friend,
139          *         {@code false} otherwise
140          */
141         public boolean hasFriendSone(Sone friendSone) {
142                 return friendSones.contains(friendSone);
143         }
144
145         /**
146          * Adds the given Sone as a friend Sone.
147          *
148          * @param friendSone
149          *            The friend Sone to add
150          * @return This Sone (for method chaining)
151          */
152         public Sone addFriendSone(Sone friendSone) {
153                 friendSones.add(friendSone);
154                 return this;
155         }
156
157         /**
158          * Removes the given Sone as a friend Sone.
159          *
160          * @param friendSone
161          *            The friend Sone to remove
162          * @return This Sone (for method chaining)
163          */
164         public Sone removeFriendSone(Sone friendSone) {
165                 friendSones.remove(friendSone);
166                 return this;
167         }
168
169         //
170         // OBJECT METHODS
171         //
172
173         /**
174          * {@inheritDoc}
175          */
176         @Override
177         public int hashCode() {
178                 return id.hashCode();
179         }
180
181 }