Remove obsolete User class.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / PostShell.java
1 /*
2  * Sone - PostShell.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.UUID;
24
25 /**
26  * {@link Shell} around a {@link Post} that has not yet been retrieved from
27  * Freenet.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class PostShell extends Post implements Shell<Post> {
32
33         /** The GUID of the post. */
34         private UUID id;
35
36         /** The Sone this post belongs to. */
37         private Sone sone;
38
39         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
40         private Long time;
41
42         /** The text of the post. */
43         private String text;
44
45         /** The replies that have been loaded for this post. */
46         private final Set<Reply> replies = new HashSet<Reply>();
47
48         /**
49          * Creates a new post shell.
50          */
51         public PostShell() {
52                 super(null, null);
53         }
54
55         //
56         // ACCESSORS
57         //
58
59         /**
60          * Returns the ID of the post.
61          *
62          * @return The ID of the post
63          */
64         @Override
65         public String getId() {
66                 return id.toString();
67         }
68
69         /**
70          * Sets the ID of the post.
71          *
72          * @param id
73          *            The ID of the post
74          * @return This post shell (for method chaining)
75          */
76         public PostShell setId(UUID id) {
77                 this.id = id;
78                 return this;
79         }
80
81         /**
82          * Returns the Sone this post belongs to.
83          *
84          * @return The Sone of this post
85          */
86         @Override
87         public Sone getSone() {
88                 return sone;
89         }
90
91         /**
92          * Sets the Sone the post belongs to.
93          *
94          * @param sone
95          *            The Sone the post belongs to
96          * @return This post shell (for method chaining)
97          */
98         public PostShell setSone(Sone sone) {
99                 this.sone = sone;
100                 return this;
101         }
102
103         /**
104          * Returns the time of the post.
105          *
106          * @return The time of the post (in milliseconds since Jan 1, 1970 UTC)
107          */
108         @Override
109         public long getTime() {
110                 return time;
111         }
112
113         /**
114          * Sets the time of the post.
115          *
116          * @param time
117          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
118          * @return This post shell (for method chaining)
119          */
120         public PostShell setTime(long time) {
121                 this.time = time;
122                 return this;
123         }
124
125         /**
126          * Returns the text of the post.
127          *
128          * @return The text of the post
129          */
130         @Override
131         public String getText() {
132                 return text;
133         }
134
135         /**
136          * Sets the text of the post.
137          *
138          * @param text
139          *            The text of the post.
140          * @return This post shell (for method chaining)
141          */
142         public PostShell setText(String text) {
143                 this.text = text;
144                 return this;
145         }
146
147         /**
148          * Returns all replies to this post in unspecified order.
149          *
150          * @return All replies to this post
151          */
152         @Override
153         public Set<Reply> getReplies() {
154                 return Collections.unmodifiableSet(replies);
155         }
156
157         /**
158          * Adds a reply to this post. The reply will not be added if its
159          * {@link Reply#getPost() post} is not equal to this post.
160          *
161          * @param reply
162          *            The reply to add
163          */
164         @Override
165         public void addReply(Reply reply) {
166                 if (reply.getPost().equals(this)) {
167                         replies.add(reply);
168                 }
169         }
170
171         /**
172          * Removes a reply from this post.
173          *
174          * @param reply
175          *            The reply to remove
176          */
177         @Override
178         public void removeReply(Reply reply) {
179                 if (reply.getPost().equals(this)) {
180                         replies.remove(reply);
181                 }
182         }
183
184         //
185         // INTERFACE Shell
186         //
187
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         public boolean canUnshell() {
193                 return (id != null) && (sone != null) && (time != null) && (text != null);
194         }
195
196         /**
197          * {@inheritDoc}
198          */
199         @Override
200         public Post getShelled() {
201                 if (canUnshell()) {
202                         Post post = new Post(id, sone, time, text);
203                         for (Reply reply : replies) {
204                                 post.addReply(reply);
205                         }
206                 }
207                 return null;
208         }
209
210 }