Add unique ID for the Sone.
[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 = UUID.randomUUID();
37
38         /** The URI under which the Sone is stored in Freenet. */
39         private final FreenetURI requestUri;
40
41         /** The URI used to insert a new version of this Sone. */
42         /* This will be null for remote Sones! */
43         private final FreenetURI insertUri;
44
45         /** All friend Sones. */
46         private final Set<Sone> friendSones = new HashSet<Sone>();
47
48         /**
49          * Creates a new Sone.
50          *
51          * @param requestUri
52          *            The request URI of the Sone
53          */
54         public Sone(FreenetURI requestUri) {
55                 this(requestUri, null);
56         }
57
58         /**
59          * Creates a new Sone.
60          *
61          * @param requestUri
62          *            The request URI of the Sone
63          * @param insertUri
64          *            The insert URI of the Sone
65          */
66         public Sone(FreenetURI requestUri, FreenetURI insertUri) {
67                 this.requestUri = requestUri;
68                 this.insertUri = insertUri;
69         }
70
71         //
72         // ACCESSORS
73         //
74
75         /**
76          * Returns the ID of this Sone.
77          *
78          * @return The ID of this Sone
79          */
80         public String getId() {
81                 return id.toString();
82         }
83
84         /**
85          * Returns the request URI of this Sone.
86          *
87          * @return The request URI of this Sone
88          */
89         public FreenetURI getRequestUri() {
90                 return requestUri;
91         }
92
93         /**
94          * Returns the insert URI of this Sone.
95          *
96          * @return The insert URI of this Sone
97          */
98         public FreenetURI getInsertUri() {
99                 return insertUri;
100         }
101
102         /**
103          * Returns all friend Sones of this Sone.
104          *
105          * @return The friend Sones of this Sone
106          */
107         public Set<Sone> getFriendSones() {
108                 return Collections.unmodifiableSet(friendSones);
109         }
110
111         /**
112          * Returns whether this Sone has the given Sone as a friend Sone.
113          *
114          * @param friendSone
115          *            The friend Sone to check for
116          * @return {@code true} if this Sone has the given Sone as a friend,
117          *         {@code false} otherwise
118          */
119         public boolean hasFriendSone(Sone friendSone) {
120                 return friendSones.contains(friendSone);
121         }
122
123         /**
124          * Adds the given Sone as a friend Sone.
125          *
126          * @param friendSone
127          *            The friend Sone to add
128          * @return This Sone (for method chaining)
129          */
130         public Sone addFriendSone(Sone friendSone) {
131                 friendSones.add(friendSone);
132                 return this;
133         }
134
135         /**
136          * Removes the given Sone as a friend Sone.
137          *
138          * @param friendSone
139          *            The friend Sone to remove
140          * @return This Sone (for method chaining)
141          */
142         public Sone removeFriendSone(Sone friendSone) {
143                 friendSones.remove(friendSone);
144                 return this;
145         }
146
147         //
148         // OBJECT METHODS
149         //
150
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         public int hashCode() {
156                 return id.hashCode();
157         }
158
159 }