Merge branch 'next' into image-management
[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 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         }
61
62         /**
63          * Creates a new image.
64          *
65          * @param id
66          *            The ID of the image
67          */
68         public Image(String id) {
69                 Validation.begin().isNotNull("Image ID", id).check();
70                 this.id = id;
71         }
72
73         //
74         // ACCESSORS
75         //
76
77         /**
78          * Returns the ID of this image.
79          *
80          * @return The ID of this image
81          */
82         public String getId() {
83                 return id;
84         }
85
86         /**
87          * Returns the Sone this image belongs to.
88          *
89          * @return The Sone this image belongs to
90          */
91         public Sone getSone() {
92                 return sone;
93         }
94
95         /**
96          * Sets the owner of this image. The owner can only be set if no owner has
97          * yet been set.
98          *
99          * @param sone
100          *            The new owner of this image
101          * @return This image
102          */
103         public Image setSone(Sone sone) {
104                 Validation.begin().isNull("Current Image Owner", this.sone).isNotNull("New Image Owner", sone);
105                 this.sone = sone;
106                 return this;
107         }
108
109         /**
110          * Returns the key of this image.
111          *
112          * @return The key of this image
113          */
114         public String getKey() {
115                 return key;
116         }
117
118         /**
119          * Sets the key of this image. The key can only be set as long as no key has
120          * yet been set.
121          *
122          * @param key
123          *            The new key of this image
124          * @return This image
125          */
126         public Image setKey(String key) {
127                 Validation.begin().isNull("Current Image Key", this.key).isNotNull("New Image Key", key).check();
128                 this.key = key;
129                 return this;
130         }
131
132         /**
133          * Returns the creation time of this image.
134          *
135          * @return The creation time of this image (in milliseconds since 1970, Jan
136          *         1, UTC)
137          */
138         public long getCreationTime() {
139                 return creationTime;
140         }
141
142         /**
143          * Sets the new creation time of this image. The creation time can only be
144          * set as long as no creation time has been set yet.
145          *
146          * @param creationTime
147          *            The new creation time of this image
148          * @return This image
149          */
150         public Image setCreationTime(long creationTime) {
151                 Validation.begin().isEqual("Current Image Creation Time", this.creationTime, 0).isGreater("New Image Creation Time", creationTime, 0).check();
152                 this.creationTime = creationTime;
153                 return this;
154         }
155
156         /**
157          * Returns the width of this image.
158          *
159          * @return The width of this image (in pixels)
160          */
161         public int getWidth() {
162                 return width;
163         }
164
165         /**
166          * Sets the width of this image. The width can only be set as long as no
167          * width has been set yet.
168          *
169          * @param width
170          *            The new width of this image
171          * @return This image
172          */
173         public Image setWidth(int width) {
174                 Validation.begin().isEqual("Current Image Width", this.width, 0).isGreater("New Image Width", width, 0).check();
175                 this.width = width;
176                 return this;
177         }
178
179         /**
180          * Returns the height of this image.
181          *
182          * @return The height of this image (in pixels)
183          */
184         public int getHeight() {
185                 return height;
186         }
187
188         /**
189          * Sets the new height of this image. The height can only be set as long as
190          * no height has yet been set.
191          *
192          * @param height
193          *            The new height of this image
194          * @return This image
195          */
196         public Image setHeight(int height) {
197                 Validation.begin().isEqual("Current Image Height", this.height, 0).isGreater("New Image Height", height, 0);
198                 this.height = height;
199                 return this;
200         }
201
202         /**
203          * Returns the title of this image.
204          *
205          * @return The title of this image
206          */
207         public String getTitle() {
208                 return title;
209         }
210
211         /**
212          * Sets the title of this image.
213          *
214          * @param title
215          *            The title of this image
216          * @return This image
217          */
218         public Image setTitle(String title) {
219                 Validation.begin().isNotNull("Image Title", title).check();
220                 this.title = title;
221                 return this;
222         }
223
224         /**
225          * Returns the description of this image.
226          *
227          * @return The description of this image
228          */
229         public String getDescription() {
230                 return description;
231         }
232
233         /**
234          * Sets the description of this image.
235          *
236          * @param description
237          *            The description of this image
238          * @return This image
239          */
240         public Image setDescription(String description) {
241                 Validation.begin().isNotNull("Image Description", description).check();
242                 this.description = description;
243                 return this;
244         }
245
246         //
247         // FINGERPRINTABLE METHODS
248         //
249
250         /**
251          * {@inheritDoc}
252          */
253         @Override
254         public String getFingerprint() {
255                 StringBuilder fingerprint = new StringBuilder();
256                 fingerprint.append("Image(");
257                 fingerprint.append("ID(").append(id).append(')');
258                 fingerprint.append("Title(").append(title).append(')');
259                 fingerprint.append("Description(").append(description).append(')');
260                 fingerprint.append(')');
261                 return fingerprint.toString();
262         }
263
264 }