Add method that returns whether an image has been inserted.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Image.java
1 /*
2  * Sone - Image.java - Copyright © 2011 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
22 import net.pterodactylus.util.validation.Validation;
23
24 /**
25  * Container for image metadata.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class Image implements Fingerprintable {
30
31         /** The ID of the image. */
32         private final String id;
33
34         /** The Sone the image belongs to. */
35         private Sone sone;
36
37         /** The request key of the image. */
38         private String key;
39
40         /** The creation time of the image. */
41         private long creationTime;
42
43         /** The width of the image. */
44         private int width;
45
46         /** The height of the image. */
47         private int height;
48
49         /** The title of the image. */
50         private String title;
51
52         /** The description of the image. */
53         private String description;
54
55         /**
56          * Creates a new image with a random ID.
57          */
58         public Image() {
59                 this(UUID.randomUUID().toString());
60                 setCreationTime(System.currentTimeMillis());
61         }
62
63         /**
64          * Creates a new image.
65          *
66          * @param id
67          *            The ID of the image
68          */
69         public Image(String id) {
70                 Validation.begin().isNotNull("Image ID", id).check();
71                 this.id = id;
72         }
73
74         //
75         // ACCESSORS
76         //
77
78         /**
79          * Returns the ID of this image.
80          *
81          * @return The ID of this image
82          */
83         public String getId() {
84                 return id;
85         }
86
87         /**
88          * Returns the Sone this image belongs to.
89          *
90          * @return The Sone this image belongs to
91          */
92         public Sone getSone() {
93                 return sone;
94         }
95
96         /**
97          * Sets the owner of this image. The owner can only be set if no owner has
98          * yet been set.
99          *
100          * @param sone
101          *            The new owner of this image
102          * @return This image
103          */
104         public Image setSone(Sone sone) {
105                 Validation.begin().isNull("Current Image Owner", this.sone).isNotNull("New Image Owner", sone);
106                 this.sone = sone;
107                 return this;
108         }
109
110         /**
111          * Returns the request key of this image.
112          *
113          * @return The request key of this image
114          */
115         public String getKey() {
116                 return key;
117         }
118
119         /**
120          * Sets the request key of this image. The request key can only be set as
121          * long as no request key has yet been set.
122          *
123          * @param key
124          *            The new request key of this image
125          * @return This image
126          */
127         public Image setKey(String key) {
128                 Validation.begin().isNull("Current Image Key", this.key).isNotNull("New Image Key", key).check();
129                 this.key = key;
130                 return this;
131         }
132
133         /**
134          * Returns whether the image has already been inserted. An image is
135          * considered as having been inserted it its {@link #getKey() key} is not
136          * {@code null}.
137          *
138          * @return {@code true} if there is a key for this image, {@code false}
139          *         otherwise
140          */
141         public boolean isInserted() {
142                 return key != null;
143         }
144
145         /**
146          * Returns the creation time of this image.
147          *
148          * @return The creation time of this image (in milliseconds since 1970, Jan
149          *         1, UTC)
150          */
151         public long getCreationTime() {
152                 return creationTime;
153         }
154
155         /**
156          * Sets the new creation time of this image. The creation time can only be
157          * set as long as no creation time has been set yet.
158          *
159          * @param creationTime
160          *            The new creation time of this image
161          * @return This image
162          */
163         public Image setCreationTime(long creationTime) {
164                 Validation.begin().isEqual("Current Image Creation Time", this.creationTime, 0).isGreater("New Image Creation Time", creationTime, 0).check();
165                 this.creationTime = creationTime;
166                 return this;
167         }
168
169         /**
170          * Returns the width of this image.
171          *
172          * @return The width of this image (in pixels)
173          */
174         public int getWidth() {
175                 return width;
176         }
177
178         /**
179          * Sets the width of this image. The width can only be set as long as no
180          * width has been set yet.
181          *
182          * @param width
183          *            The new width of this image
184          * @return This image
185          */
186         public Image setWidth(int width) {
187                 Validation.begin().isEqual("Current Image Width", this.width, 0).isGreater("New Image Width", width, 0).check();
188                 this.width = width;
189                 return this;
190         }
191
192         /**
193          * Returns the height of this image.
194          *
195          * @return The height of this image (in pixels)
196          */
197         public int getHeight() {
198                 return height;
199         }
200
201         /**
202          * Sets the new height of this image. The height can only be set as long as
203          * no height has yet been set.
204          *
205          * @param height
206          *            The new height of this image
207          * @return This image
208          */
209         public Image setHeight(int height) {
210                 Validation.begin().isEqual("Current Image Height", this.height, 0).isGreater("New Image Height", height, 0);
211                 this.height = height;
212                 return this;
213         }
214
215         /**
216          * Returns the title of this image.
217          *
218          * @return The title of this image
219          */
220         public String getTitle() {
221                 return title;
222         }
223
224         /**
225          * Sets the title of this image.
226          *
227          * @param title
228          *            The title of this image
229          * @return This image
230          */
231         public Image setTitle(String title) {
232                 Validation.begin().isNotNull("Image Title", title).check();
233                 this.title = title;
234                 return this;
235         }
236
237         /**
238          * Returns the description of this image.
239          *
240          * @return The description of this image
241          */
242         public String getDescription() {
243                 return description;
244         }
245
246         /**
247          * Sets the description of this image.
248          *
249          * @param description
250          *            The description of this image
251          * @return This image
252          */
253         public Image setDescription(String description) {
254                 Validation.begin().isNotNull("Image Description", description).check();
255                 this.description = description;
256                 return this;
257         }
258
259         //
260         // FINGERPRINTABLE METHODS
261         //
262
263         /**
264          * {@inheritDoc}
265          */
266         @Override
267         public String getFingerprint() {
268                 StringBuilder fingerprint = new StringBuilder();
269                 fingerprint.append("Image(");
270                 fingerprint.append("ID(").append(id).append(')');
271                 fingerprint.append("Title(").append(title).append(')');
272                 fingerprint.append("Description(").append(description).append(')');
273                 fingerprint.append(')');
274                 return fingerprint.toString();
275         }
276
277 }