2 * Sone - PostShell.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.Collections;
21 import java.util.HashSet;
23 import java.util.UUID;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
27 import net.pterodactylus.util.logging.Logging;
30 * {@link Shell} around a {@link Post} that has not yet been retrieved from
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class PostShell extends Post implements Shell<Post> {
38 private static final Logger logger = Logging.getLogger(PostShell.class);
40 /** The shell creator. */
41 public static final ShellCreator<Post> creator = new ShellCreator<Post>() {
44 public Shell<Post> createShell(String id) {
45 return new PostShell().setId(id);
49 /** The GUID of the post. */
52 /** The Sone this post belongs to. */
55 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
58 /** The text of the post. */
61 /** The replies that have been loaded for this post. */
62 private final Set<Reply> replies = new HashSet<Reply>();
65 * Creates a new post shell.
76 * Returns the ID of the post.
78 * @return The ID of the post
81 public String getId() {
86 * Sets the ID of the post.
90 * @return This post shell (for method chaining)
92 public PostShell setId(String id) {
94 this.id = UUID.fromString(id);
95 } catch (IllegalArgumentException iae1) {
96 logger.log(Level.WARNING, "Invalid ID: “" + id + "”.", iae1);
97 this.id = UUID.randomUUID();
103 * Returns the Sone this post belongs to.
105 * @return The Sone of this post
108 public Sone getSone() {
113 * Sets the Sone the post belongs to.
116 * The Sone the post belongs to
117 * @return This post shell (for method chaining)
119 public PostShell setSone(Sone sone) {
125 * Returns the time of the post.
127 * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
130 public long getTime() {
135 * Sets the time of the post.
138 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
139 * @return This post shell (for method chaining)
141 public PostShell setTime(long time) {
147 * Returns the text of the post.
149 * @return The text of the post
152 public String getText() {
157 * Sets the text of the post.
160 * The text of the post.
161 * @return This post shell (for method chaining)
163 public PostShell setText(String text) {
169 * Returns all replies to this post in unspecified order.
171 * @return All replies to this post
174 public Set<Reply> getReplies() {
175 return Collections.unmodifiableSet(replies);
179 * Adds a reply to this post. The reply will not be added if its
180 * {@link Reply#getPost() post} is not equal to this post.
186 public void addReply(Reply reply) {
187 if (reply.getPost().equals(this)) {
193 * Removes a reply from this post.
196 * The reply to remove
199 public void removeReply(Reply reply) {
200 if (reply.getPost().equals(this)) {
201 replies.remove(reply);
213 public boolean canUnshell() {
214 return (id != null) && (sone != null) && (!(sone instanceof Shell<?>)) && (time != null) && (text != null);
221 public Post getShelled() {
223 Post post = new Post(id, sone, time, text);
224 for (Reply reply : replies) {
225 post.addReply(reply);