2 * Sone - Image.java - Copyright © 2011 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
20 import java.util.UUID;
22 import net.pterodactylus.util.validation.Validation;
25 * Container for image metadata.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public class Image implements Fingerprintable {
31 /** The ID of the image. */
32 private final String id;
34 /** The Sone the image belongs to. */
37 /** The album this image belongs to. */
40 /** The request key of the image. */
43 /** The creation time of the image. */
44 private long creationTime;
46 /** The width of the image. */
49 /** The height of the image. */
52 /** The title of the image. */
55 /** The description of the image. */
56 private String description;
59 * Creates a new image with a random ID.
62 this(UUID.randomUUID().toString());
63 setCreationTime(System.currentTimeMillis());
67 * Creates a new image.
72 public Image(String id) {
73 Validation.begin().isNotNull("Image ID", id).check();
82 * Returns the ID of this image.
84 * @return The ID of this image
86 public String getId() {
91 * Returns the Sone this image belongs to.
93 * @return The Sone this image belongs to
95 public Sone getSone() {
100 * Sets the owner of this image. The owner can only be set if no owner has
104 * The new owner of this image
107 public Image setSone(Sone sone) {
108 Validation.begin().isNotNull("New Image Owner", sone).isEither("Old Image Owner", this.sone, null, sone).check();
114 * Returns the album this image belongs to.
116 * @return The album this image belongs to
118 public Album getAlbum() {
123 * Sets the album this image belongs to. The album of an image can only be
124 * set once, and it is usually called by {@link Album#addImage(Image)}.
127 * The album this image belongs to
130 public Image setAlbum(Album album) {
131 Validation.begin().isNotNull("New Album", album).check().isEqual("Album Owner and Image Owner", album.getSone(), getSone()).check();
137 * Returns the request key of this image.
139 * @return The request key of this image
141 public String getKey() {
146 * Sets the request key of this image. The request key can only be set as
147 * long as no request key has yet been set.
150 * The new request key of this image
153 public Image setKey(String key) {
154 Validation.begin().isNotNull("New Image Key", key).isEither("Old Image Key", this.key, null, key).check();
160 * Returns whether the image has already been inserted. An image is
161 * considered as having been inserted it its {@link #getKey() key} is not
164 * @return {@code true} if there is a key for this image, {@code false}
167 public boolean isInserted() {
172 * Returns the creation time of this image.
174 * @return The creation time of this image (in milliseconds since 1970, Jan
177 public long getCreationTime() {
182 * Sets the new creation time of this image. The creation time can only be
183 * set as long as no creation time has been set yet.
185 * @param creationTime
186 * The new creation time of this image
189 public Image setCreationTime(long creationTime) {
190 Validation.begin().isGreater("New Image Creation Time", creationTime, 0).isEither("Old Image Creation Time", this.creationTime, 0L, creationTime).check();
191 this.creationTime = creationTime;
196 * Returns the width of this image.
198 * @return The width of this image (in pixels)
200 public int getWidth() {
205 * Sets the width of this image. The width can only be set as long as no
206 * width has been set yet.
209 * The new width of this image
212 public Image setWidth(int width) {
213 Validation.begin().isGreater("New Image Width", width, 0).isEither("Old Image Width", this.width, 0, width).check();
219 * Returns the height of this image.
221 * @return The height of this image (in pixels)
223 public int getHeight() {
228 * Sets the new height of this image. The height can only be set as long as
229 * no height has yet been set.
232 * The new height of this image
235 public Image setHeight(int height) {
236 Validation.begin().isGreater("New Image Height", height, 0).isEither("Old Image Height", this.height, 0, height).check();
237 this.height = height;
242 * Returns the title of this image.
244 * @return The title of this image
246 public String getTitle() {
251 * Sets the title of this image.
254 * The title of this image
257 public Image setTitle(String title) {
258 Validation.begin().isNotNull("Image Title", title).check();
264 * Returns the description of this image.
266 * @return The description of this image
268 public String getDescription() {
273 * Sets the description of this image.
276 * The description of this image
279 public Image setDescription(String description) {
280 Validation.begin().isNotNull("Image Description", description).check();
281 this.description = description;
286 // FINGERPRINTABLE METHODS
293 public String getFingerprint() {
294 StringBuilder fingerprint = new StringBuilder();
295 fingerprint.append("Image(");
296 fingerprint.append("ID(").append(id).append(')');
297 fingerprint.append("Title(").append(title).append(')');
298 fingerprint.append("Description(").append(description).append(')');
299 fingerprint.append(')');
300 return fingerprint.toString();
311 public int hashCode() {
312 return id.hashCode();
319 public boolean equals(Object object) {
320 if (!(object instanceof Image)) {
323 return ((Image) object).id.equals(id);