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.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;
33 * {@link Shell} around a {@link Post} that has not yet been retrieved from
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class PostShell extends Post implements Shell<Post> {
41 private static final Logger logger = Logging.getLogger(PostShell.class);
43 /** The shell creator. */
44 public static final ShellCreator<Post> creator = new ShellCreator<Post>() {
47 public Shell<Post> createShell(String id) {
48 return new PostShell().setId(id);
52 /** The GUID of the post. */
55 /** The Sone this post belongs to. */
58 /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
61 /** The text of the post. */
64 /** The replies that have been loaded for this post. */
65 private final Set<Reply> replies = new HashSet<Reply>();
68 * Creates a new post shell.
79 * Returns the ID of the post.
81 * @return The ID of the post
84 public String getId() {
89 * Sets the ID of the post.
93 * @return This post shell (for method chaining)
95 public PostShell setId(String id) {
97 this.id = UUID.fromString(id);
98 } catch (IllegalArgumentException iae1) {
99 logger.log(Level.WARNING, "Invalid ID: “" + id + "”.", iae1);
100 this.id = UUID.randomUUID();
106 * Returns the Sone this post belongs to.
108 * @return The Sone of this post
111 public Sone getSone() {
116 * Sets the Sone the post belongs to.
119 * The Sone the post belongs to
120 * @return This post shell (for method chaining)
122 public PostShell setSone(Sone sone) {
128 * Returns the time of the post.
130 * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
133 public long getTime() {
134 return (time != null) ? time : 0;
138 * Sets the time of the post.
141 * The time of the post (in milliseconds since Jan 1, 1970 UTC)
142 * @return This post shell (for method chaining)
144 public PostShell setTime(long time) {
150 * Returns the text of the post.
152 * @return The text of the post
155 public String getText() {
160 * Sets the text of the post.
163 * The text of the post.
164 * @return This post shell (for method chaining)
166 public PostShell setText(String text) {
172 * Returns all replies to this post in unspecified order.
174 * @return All replies to this post
177 public List<Reply> getReplies() {
178 List<Reply> sortedReplies = new ArrayList<Reply>(replies);
179 Collections.sort(sortedReplies, new Comparator<Reply>() {
182 public int compare(Reply leftReply, Reply rightReply) {
183 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
187 return sortedReplies;
191 * Adds a reply to this post. The reply will not be added if its
192 * {@link Reply#getPost() post} is not equal to this post.
198 public void addReply(Reply reply) {
199 if (reply.getPost().equals(this)) {
205 * Removes a reply from this post.
208 * The reply to remove
211 public void removeReply(Reply reply) {
212 if (reply.getPost().equals(this)) {
213 replies.remove(reply);
225 public boolean canUnshell() {
226 return (id != null) && (sone != null) && (!(sone instanceof Shell<?>)) && (time != null) && (text != null);
233 public Post getShelled() {
235 Post post = new Post(id, sone, time, text);
236 for (Reply reply : replies) {
237 post.addReply(reply);