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.Comparator;
23 import java.util.HashSet;
24 import java.util.List;
26 import java.util.UUID;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.util.logging.Logging;
31 import freenet.keys.FreenetURI;
34 * {@link Shell} around a {@link Sone} that has not yet been retrieved.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class SoneShell extends Sone implements Shell<Sone> {
41 private static final Logger logger = Logging.getLogger(SoneShell.class);
43 /** The shell creator. */
44 public static final ShellCreator<Sone> creator = new ShellCreator<Sone>() {
47 public Shell<Sone> createShell(String id) {
48 return new SoneShell().setId(id);
52 /** A GUID for this Sone. */
55 /** The name of this Sone. */
58 /** The URI under which the Sone is stored in Freenet. */
59 private FreenetURI requestUri;
61 /** The profile of this Sone. */
62 private Profile profile;
64 /** All friend Sones. */
65 private final Set<Sone> friendSones = new HashSet<Sone>();
68 private final List<Post> posts = new ArrayList<Post>();
71 private final Set<Reply> replies = new HashSet<Reply>();
74 * Creates a new Sone shell.
77 super(null, null, null);
85 * Returns the ID of this Sone.
87 * @return The ID of this Sone
90 public String getId() {
95 * Sets the ID of the Sone.
99 * @return This Sone shell (for method chaining)
101 public SoneShell setId(String id) {
103 this.id = UUID.fromString(id);
104 } catch (IllegalArgumentException iae1) {
105 logger.log(Level.WARNING, "Invalid ID: “" + id + "”.", iae1);
106 this.id = UUID.randomUUID();
112 * Returns the name of this Sone.
114 * @return The name of this Sone
117 public String getName() {
122 * Sets the name of the Sone.
125 * The name of the Sone
126 * @return This Sone shell (for method chaining)
128 public SoneShell setName(String name) {
134 * Returns the request URI of this Sone.
136 * @return The request URI of this Sone
139 public FreenetURI getRequestUri() {
144 * Sets the request URI of the Sone.
147 * The request URI of the Sone
148 * @return This Sone shell (for method chaining)
150 public SoneShell setRequestUri(FreenetURI requestUri) {
151 this.requestUri = requestUri;
156 * Returns a copy of the profile. If you want to update values in the
157 * profile of this Sone, update the values in the returned {@link Profile}
158 * and use {@link #setProfile(Profile)} to change the profile in this Sone.
160 * @return A copy of the profile
163 public Profile getProfile() {
164 return new Profile(profile);
171 public void setProfile(Profile profile) {
172 this.profile = profile;
176 * Returns all friend Sones of this Sone.
178 * @return The friend Sones of this Sone
181 public Set<Sone> getFriendSones() {
182 return Collections.unmodifiableSet(friendSones);
186 * Returns whether this Sone has the given Sone as a friend Sone.
189 * The friend Sone to check for
190 * @return {@code true} if this Sone has the given Sone as a friend,
191 * {@code false} otherwise
194 public boolean hasFriendSone(Sone friendSone) {
195 return friendSones.contains(friendSone);
199 * Adds the given Sone as a friend Sone.
202 * The friend Sone to add
203 * @return This Sone (for method chaining)
206 public Sone addFriendSone(Sone friendSone) {
207 friendSones.add(friendSone);
212 * Removes the given Sone as a friend Sone.
215 * The friend Sone to remove
216 * @return This Sone (for method chaining)
219 public Sone removeFriendSone(Sone friendSone) {
220 friendSones.remove(friendSone);
225 * Returns the list of posts of this Sone, sorted by time, newest first.
227 * @return All posts of this Sone
230 public List<Post> getPosts() {
231 List<Post> sortedPosts = new ArrayList<Post>(posts);
232 Collections.sort(sortedPosts, new Comparator<Post>() {
235 public int compare(Post leftPost, Post rightPost) {
236 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, rightPost.getTime() - leftPost.getTime()));
244 * Adds the given post to this Sone. The post will not be added if its
245 * {@link Post#getSone() Sone} is not this Sone.
251 public void addPost(Post post) {
256 * Removes the given post from this Sone.
262 public void removePost(Post post) {
267 * Returns all replies this Sone made.
269 * @return All replies this Sone made
272 public Set<Reply> getReplies() {
273 return Collections.unmodifiableSet(replies);
277 * Adds a reply to this Sone. If the given reply was not made by this Sone,
278 * nothing is added to this Sone.
284 public void addReply(Reply reply) {
289 * Removes a reply from this Sone.
292 * The reply to remove
295 public void removeReply(Reply reply) {
296 replies.remove(reply);
307 public boolean canUnshell() {
308 return (id != null) && (name != null) && (requestUri != null) && (profile != null);
315 public Sone getShelled() {
317 Sone sone = new Sone(id, name, requestUri);
318 sone.setProfile(profile);
319 for (Sone friendSone : friendSones) {
320 sone.addFriendSone(friendSone);
322 for (Post post : posts) {
325 for (Reply reply : replies) {
326 sone.addReply(reply);