2 * Sone - SoneShell.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
25 import java.util.UUID;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.util.logging.Logging;
30 import freenet.keys.FreenetURI;
33 * {@link Shell} around a {@link Sone} that has not yet been retrieved.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class SoneShell extends Sone implements Shell<Sone> {
40 private static final Logger logger = Logging.getLogger(SoneShell.class);
42 /** The shell creator. */
43 public static final ShellCreator<Sone> creator = new ShellCreator<Sone>() {
46 public Shell<Sone> createShell(String id) {
47 return new SoneShell().setId(id);
51 /** A GUID for this Sone. */
54 /** The name of this Sone. */
57 /** The URI under which the Sone is stored in Freenet. */
58 private FreenetURI requestUri;
60 /** The profile of this Sone. */
61 private Profile profile;
63 /** All friend Sones. */
64 private final Set<Sone> friendSones = new HashSet<Sone>();
67 private final List<Post> posts = new ArrayList<Post>();
70 private final Set<Reply> replies = new HashSet<Reply>();
73 * Creates a new Sone shell.
76 super(null, null, null);
84 * Returns the ID of this Sone.
86 * @return The ID of this Sone
89 public String getId() {
94 * Sets the ID of the Sone.
98 * @return This Sone shell (for method chaining)
100 public SoneShell setId(String id) {
102 this.id = UUID.fromString(id);
103 } catch (IllegalArgumentException iae1) {
104 logger.log(Level.WARNING, "Invalid ID: “" + id + "”.", iae1);
105 this.id = UUID.randomUUID();
111 * Returns the name of this Sone.
113 * @return The name of this Sone
116 public String getName() {
121 * Sets the name of the Sone.
124 * The name of the Sone
125 * @return This Sone shell (for method chaining)
127 public SoneShell setName(String name) {
133 * Returns the request URI of this Sone.
135 * @return The request URI of this Sone
138 public FreenetURI getRequestUri() {
143 * Sets the request URI of the Sone.
146 * The request URI of the Sone
147 * @return This Sone shell (for method chaining)
149 public SoneShell setRequestUri(FreenetURI requestUri) {
150 this.requestUri = requestUri;
155 * Returns a copy of the profile. If you want to update values in the
156 * profile of this Sone, update the values in the returned {@link Profile}
157 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
159 * @return A copy of the profile
162 public Profile getProfile() {
163 return new Profile(profile);
170 public void setProfile(Profile profile) {
171 this.profile = profile;
175 * Returns all friend Sones of this Sone.
177 * @return The friend Sones of this Sone
180 public Set<Sone> getFriendSones() {
181 return Collections.unmodifiableSet(friendSones);
185 * Returns whether this Sone has the given Sone as a friend Sone.
188 * The friend Sone to check for
189 * @return {@code true} if this Sone has the given Sone as a friend,
190 * {@code false} otherwise
193 public boolean hasFriendSone(Sone friendSone) {
194 return friendSones.contains(friendSone);
198 * Adds the given Sone as a friend Sone.
201 * The friend Sone to add
202 * @return This Sone (for method chaining)
205 public Sone addFriendSone(Sone friendSone) {
206 friendSones.add(friendSone);
211 * Removes the given Sone as a friend Sone.
214 * The friend Sone to remove
215 * @return This Sone (for method chaining)
218 public Sone removeFriendSone(Sone friendSone) {
219 friendSones.remove(friendSone);
224 * Returns the list of posts of this Sone.
226 * @return All posts of this Sone
229 public List<Post> getPosts() {
230 return Collections.unmodifiableList(posts);
234 * Adds the given post to this Sone. The post will not be added if its
235 * {@link Post#getSone() Sone} is not this Sone.
241 public void addPost(Post post) {
246 * Removes the given post from this Sone.
252 public void removePost(Post post) {
257 * Returns all replies this Sone made.
259 * @return All replies this Sone made
262 public Set<Reply> getReplies() {
263 return Collections.unmodifiableSet(replies);
267 * Adds a reply to this Sone. If the given reply was not made by this Sone,
268 * nothing is added to this Sone.
274 public void addReply(Reply reply) {
279 * Removes a reply from this Sone.
282 * The reply to remove
285 public void removeReply(Reply reply) {
286 replies.remove(reply);
297 public boolean canUnshell() {
298 return (id != null) && (name != null) && (requestUri != null) && (profile != null);
305 public Sone getShelled() {
307 Sone sone = new Sone(id, name, requestUri);
308 sone.setProfile(profile);
309 for (Sone friendSone : friendSones) {
310 sone.addFriendSone(friendSone);
312 for (Post post : posts) {
315 for (Reply reply : replies) {
316 sone.addReply(reply);