Set creation time when a new image is created.
[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 the creation time of this image.
135          *
136          * @return The creation time of this image (in milliseconds since 1970, Jan
137          *         1, UTC)
138          */
139         public long getCreationTime() {
140                 return creationTime;
141         }
142
143         /**
144          * Sets the new creation time of this image. The creation time can only be
145          * set as long as no creation time has been set yet.
146          *
147          * @param creationTime
148          *            The new creation time of this image
149          * @return This image
150          */
151         public Image setCreationTime(long creationTime) {
152                 Validation.begin().isEqual("Current Image Creation Time", this.creationTime, 0).isGreater("New Image Creation Time", creationTime, 0).check();
153                 this.creationTime = creationTime;
154                 return this;
155         }
156
157         /**
158          * Returns the width of this image.
159          *
160          * @return The width of this image (in pixels)
161          */
162         public int getWidth() {
163                 return width;
164         }
165
166         /**
167          * Sets the width of this image. The width can only be set as long as no
168          * width has been set yet.
169          *
170          * @param width
171          *            The new width of this image
172          * @return This image
173          */
174         public Image setWidth(int width) {
175                 Validation.begin().isEqual("Current Image Width", this.width, 0).isGreater("New Image Width", width, 0).check();
176                 this.width = width;
177                 return this;
178         }
179
180         /**
181          * Returns the height of this image.
182          *
183          * @return The height of this image (in pixels)
184          */
185         public int getHeight() {
186                 return height;
187         }
188
189         /**
190          * Sets the new height of this image. The height can only be set as long as
191          * no height has yet been set.
192          *
193          * @param height
194          *            The new height of this image
195          * @return This image
196          */
197         public Image setHeight(int height) {
198                 Validation.begin().isEqual("Current Image Height", this.height, 0).isGreater("New Image Height", height, 0);
199                 this.height = height;
200                 return this;
201         }
202
203         /**
204          * Returns the title of this image.
205          *
206          * @return The title of this image
207          */
208         public String getTitle() {
209                 return title;
210         }
211
212         /**
213          * Sets the title of this image.
214          *
215          * @param title
216          *            The title of this image
217          * @return This image
218          */
219         public Image setTitle(String title) {
220                 Validation.begin().isNotNull("Image Title", title).check();
221                 this.title = title;
222                 return this;
223         }
224
225         /**
226          * Returns the description of this image.
227          *
228          * @return The description of this image
229          */
230         public String getDescription() {
231                 return description;
232         }
233
234         /**
235          * Sets the description of this image.
236          *
237          * @param description
238          *            The description of this image
239          * @return This image
240          */
241         public Image setDescription(String description) {
242                 Validation.begin().isNotNull("Image Description", description).check();
243                 this.description = description;
244                 return this;
245         }
246
247         //
248         // FINGERPRINTABLE METHODS
249         //
250
251         /**
252          * {@inheritDoc}
253          */
254         @Override
255         public String getFingerprint() {
256                 StringBuilder fingerprint = new StringBuilder();
257                 fingerprint.append("Image(");
258                 fingerprint.append("ID(").append(id).append(')');
259                 fingerprint.append("Title(").append(title).append(')');
260                 fingerprint.append("Description(").append(description).append(')');
261                 fingerprint.append(')');
262                 return fingerprint.toString();
263         }
264
265 }