Stop logging when starting the plugin failed.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / ReplyShell.java
1 /*
2  * Sone - ReplyShell.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.UUID;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 import net.pterodactylus.util.logging.Logging;
25
26 /**
27  * A shell around a {@link Reply} for replies that have not yet been retrieved
28  * from Freenet.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class ReplyShell extends Reply implements Shell<Reply> {
33
34         /** The logger. */
35         private static final Logger logger = Logging.getLogger(ReplyShell.class);
36
37         /** The shell creator. */
38         public static final ShellCreator<Reply> creator = new ShellCreator<Reply>() {
39
40                 @Override
41                 public Shell<Reply> createShell(String id) {
42                         return new ReplyShell().setId(id);
43                 }
44         };
45
46         /** The Sone that posted this reply. */
47         private Sone sone;
48
49         /** The ID of the reply. */
50         private UUID id;
51
52         /** The Post this reply refers to. */
53         private Post post;
54
55         /** The time of the reply. */
56         private Long time;
57
58         /** The text of the reply. */
59         private String text;
60
61         /**
62          * Creates a new reply shell.
63          */
64         public ReplyShell() {
65                 super(null, null, null);
66         }
67
68         //
69         // ACCESSORS
70         //
71
72         /**
73          * Returns the Sone that posted this reply.
74          *
75          * @return The Sone that posted this reply
76          */
77         @Override
78         public Sone getSone() {
79                 return sone;
80         }
81
82         /**
83          * Sets the Sone that posted this reply.
84          *
85          * @param sone
86          *            The sone that pasted this reply
87          * @return This reply shell (for method chaining)
88          */
89         public ReplyShell setSone(Sone sone) {
90                 this.sone = sone;
91                 return this;
92         }
93
94         /**
95          * Returns the ID of the reply.
96          *
97          * @return The ID of the reply
98          */
99         @Override
100         public String getId() {
101                 return id.toString();
102         }
103
104         /**
105          * Sets the ID of this reply.
106          *
107          * @param id
108          *            The ID of this reply
109          * @return This reply shell (for method chaining)
110          */
111         public ReplyShell setId(String id) {
112                 try {
113                         this.id = UUID.fromString(id);
114                 } catch (IllegalArgumentException iae1) {
115                         logger.log(Level.WARNING, "Invalid ID: “" + id + "”.", iae1);
116                         this.id = UUID.randomUUID();
117                 }
118                 return this;
119         }
120
121         /**
122          * Returns the post this reply refers to.
123          *
124          * @return The post this reply refers to
125          */
126         @Override
127         public Post getPost() {
128                 return post;
129         }
130
131         /**
132          * Sets the post this reply refers to.
133          *
134          * @param post
135          *            The post this reply refers to
136          * @return This reply shell (for method chaining)
137          */
138         public ReplyShell setPost(Post post) {
139                 this.post = post;
140                 return this;
141         }
142
143         /**
144          * Returns the time of the reply.
145          *
146          * @return The time of the reply (in milliseconds since Jan 1, 1970 UTC)
147          */
148         @Override
149         public long getTime() {
150                 return (time != null) ? time : 0;
151         }
152
153         /**
154          * Sets the time of this reply.
155          *
156          * @param time
157          *            The time of this reply (in milliseconds since Jan 1, 1970 UTC)
158          * @return This reply shell (for method chaining)
159          */
160         public ReplyShell setTime(long time) {
161                 this.time = time;
162                 return this;
163         }
164
165         /**
166          * Returns the text of the reply.
167          *
168          * @return The text of the reply
169          */
170         @Override
171         public String getText() {
172                 return text;
173         }
174
175         /**
176          * Sets the text of the reply.
177          *
178          * @param text
179          *            The text of the reply
180          * @return This reply shell (for method chaining)
181          */
182         public ReplyShell setText(String text) {
183                 this.text = text;
184                 return this;
185         }
186
187         //
188         // INTERFACE Shell
189         //
190
191         /**
192          * {@inheritDoc}
193          */
194         @Override
195         public boolean canUnshell() {
196                 return (sone != null) && (!(sone instanceof Shell<?>)) && (id != null) && (post != null) && (!(post instanceof Shell<?>)) && (time != null) && (text != null);
197         }
198
199         /**
200          * {@inheritDoc}
201          */
202         @Override
203         public Reply getShelled() {
204                 if (canUnshell()) {
205                         return new Reply(sone, id, post, time, text);
206                 }
207                 return this;
208         }
209
210 }