From: David ‘Bombe’ Roden Date: Thu, 14 Oct 2010 11:40:03 +0000 (+0200) Subject: Wrap Post and Reply into a Shell. X-Git-Tag: 0.1-RC1~399 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3a14939c2d0d6f781e43bb25e7911018bafb96a7;p=Sone.git Wrap Post and Reply into a Shell. --- diff --git a/src/main/java/net/pterodactylus/sone/data/PostShell.java b/src/main/java/net/pterodactylus/sone/data/PostShell.java new file mode 100644 index 0000000..36dba42 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/PostShell.java @@ -0,0 +1,210 @@ +/* + * Sone - PostShell.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +/** + * {@link Shell} around a {@link Post} that has not yet been retrieved from + * Freenet. + * + * @author David ‘Bombe’ Roden + */ +public class PostShell extends Post implements Shell { + + /** The GUID of the post. */ + private UUID id; + + /** The Sone this post belongs to. */ + private Sone sone; + + /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */ + private Long time; + + /** The text of the post. */ + private String text; + + /** The replies that have been loaded for this post. */ + private final Set replies = new HashSet(); + + /** + * Creates a new post shell. + */ + public PostShell() { + super(null, null); + } + + // + // ACCESSORS + // + + /** + * Returns the ID of the post. + * + * @return The ID of the post + */ + @Override + public String getId() { + return id.toString(); + } + + /** + * Sets the ID of the post. + * + * @param id + * The ID of the post + * @return This post shell (for method chaining) + */ + public PostShell setId(UUID id) { + this.id = id; + return this; + } + + /** + * Returns the Sone this post belongs to. + * + * @return The Sone of this post + */ + @Override + public Sone getSone() { + return sone; + } + + /** + * Sets the Sone the post belongs to. + * + * @param sone + * The Sone the post belongs to + * @return This post shell (for method chaining) + */ + public PostShell setSone(Sone sone) { + this.sone = sone; + return this; + } + + /** + * Returns the time of the post. + * + * @return The time of the post (in milliseconds since Jan 1, 1970 UTC) + */ + @Override + public long getTime() { + return time; + } + + /** + * Sets the time of the post. + * + * @param time + * The time of the post (in milliseconds since Jan 1, 1970 UTC) + * @return This post shell (for method chaining) + */ + public PostShell setTime(long time) { + this.time = time; + return this; + } + + /** + * Returns the text of the post. + * + * @return The text of the post + */ + @Override + public String getText() { + return text; + } + + /** + * Sets the text of the post. + * + * @param text + * The text of the post. + * @return This post shell (for method chaining) + */ + public PostShell setText(String text) { + this.text = text; + return this; + } + + /** + * Returns all replies to this post in unspecified order. + * + * @return All replies to this post + */ + @Override + public Set getReplies() { + return Collections.unmodifiableSet(replies); + } + + /** + * Adds a reply to this post. The reply will not be added if its + * {@link Reply#getPost() post} is not equal to this post. + * + * @param reply + * The reply to add + */ + @Override + public void addReply(Reply reply) { + if (reply.getPost().equals(this)) { + replies.add(reply); + } + } + + /** + * Removes a reply from this post. + * + * @param reply + * The reply to remove + */ + @Override + public void removeReply(Reply reply) { + if (reply.getPost().equals(this)) { + replies.remove(reply); + } + } + + // + // INTERFACE Shell + // + + /** + * {@inheritDoc} + */ + @Override + public boolean canUnshell() { + return (id != null) && (sone != null) && (time != null) && (text != null); + } + + /** + * {@inheritDoc} + */ + @Override + public Post getShelled() { + if (canUnshell()) { + Post post = new Post(id, sone, time, text); + for (Reply reply : replies) { + post.addReply(reply); + } + } + return null; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/ReplyShell.java b/src/main/java/net/pterodactylus/sone/data/ReplyShell.java new file mode 100644 index 0000000..fcab34a --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/ReplyShell.java @@ -0,0 +1,189 @@ +/* + * Sone - ReplyShell.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data; + +import java.util.UUID; + +/** + * A shell around a {@link Reply} for replies that have not yet been retrieved + * from Freenet. + * + * @author David ‘Bombe’ Roden + */ +public class ReplyShell extends Reply implements Shell { + + /** The Sone that posted this reply. */ + private Sone sone; + + /** The ID of the reply. */ + private UUID id; + + /** The Post this reply refers to. */ + private Post post; + + /** The time of the reply. */ + private Long time; + + /** The text of the reply. */ + private String text; + + /** + * Creates a new reply shell. + */ + public ReplyShell() { + super(null, null, null); + } + + // + // ACCESSORS + // + + /** + * Returns the Sone that posted this reply. + * + * @return The Sone that posted this reply + */ + @Override + public Sone getSone() { + return sone; + } + + /** + * Sets the Sone that posted this reply. + * + * @param sone + * The sone that pasted this reply + * @return This reply shell (for method chaining) + */ + public ReplyShell setSone(Sone sone) { + this.sone = sone; + return this; + } + + /** + * Returns the ID of the reply. + * + * @return The ID of the reply + */ + @Override + public String getId() { + return id.toString(); + } + + /** + * Sets the ID of this reply. + * + * @param id + * The ID of this reply + * @return This reply shell (for method chaining) + */ + public ReplyShell setId(UUID id) { + this.id = id; + return this; + } + + /** + * Returns the post this reply refers to. + * + * @return The post this reply refers to + */ + @Override + public Post getPost() { + return post; + } + + /** + * Sets the post this reply refers to. + * + * @param post + * The post this reply refers to + * @return This reply shell (for method chaining) + */ + public ReplyShell setPost(Post post) { + this.post = post; + return this; + } + + /** + * Returns the time of the reply. + * + * @return The time of the reply (in milliseconds since Jan 1, 1970 UTC) + */ + @Override + public long getTime() { + return time; + } + + /** + * Sets the time of this reply. + * + * @param time + * The time of this reply (in milliseconds since Jan 1, 1970 UTC) + * @return This reply shell (for method chaining) + */ + public ReplyShell setTime(long time) { + this.time = time; + return this; + } + + /** + * Returns the text of the reply. + * + * @return The text of the reply + */ + @Override + public String getText() { + return text; + } + + /** + * Sets the text of the reply. + * + * @param text + * The text of the reply + * @return This reply shell (for method chaining) + */ + public ReplyShell setText(String text) { + this.text = text; + return this; + } + + // + // INTERFACE Shell + // + + /** + * {@inheritDoc} + */ + @Override + public boolean canUnshell() { + return (sone != null) && (id != null) && (post != null) && (time != null) && (text != null); + } + + /** + * {@inheritDoc} + */ + @Override + public Reply getShelled() { + if (canUnshell()) { + return new Reply(sone, id, post, time, text); + } + return null; + } + +}