2 * Sone - Image.java - Copyright © 2011–2013 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 static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
24 import java.util.UUID;
26 import com.google.common.hash.Hasher;
27 import com.google.common.hash.Hashing;
30 * Container for image metadata.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class Image implements Identified, Fingerprintable {
36 /** The ID of the image. */
37 private final String id;
39 /** The Sone the image belongs to. */
42 /** The album this image belongs to. */
45 /** The request key of the image. */
48 /** The creation time of the image. */
49 private long creationTime;
51 /** The width of the image. */
54 /** The height of the image. */
57 /** The title of the image. */
60 /** The description of the image. */
61 private String description;
64 * Creates a new image with a random ID.
67 this(UUID.randomUUID().toString());
68 setCreationTime(System.currentTimeMillis());
72 * Creates a new image.
77 public Image(String id) {
78 this.id = checkNotNull(id, "id must not be null");
86 * Returns the ID of this image.
88 * @return The ID of this image
90 public String getId() {
95 * Returns the Sone this image belongs to.
97 * @return The Sone this image belongs to
99 public Sone getSone() {
104 * Sets the owner of this image. The owner can only be set if no owner has
108 * The new owner of this image
111 public Image setSone(Sone sone) {
112 checkNotNull(sone, "sone must not be null");
113 checkArgument((this.sone == null) || this.sone.equals(sone), "sone must not already be set to another sone");
119 * Returns the album this image belongs to.
121 * @return The album this image belongs to
123 public Album getAlbum() {
128 * Sets the album this image belongs to. The album of an image can only be
129 * set once, and it is usually called by {@link Album#addImage(Image)}.
132 * The album this image belongs to
135 public Image setAlbum(Album album) {
136 checkNotNull(album, "album must not be null");
137 checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image");
143 * Returns the request key of this image.
145 * @return The request key of this image
147 public String getKey() {
152 * Sets the request key of this image. The request key can only be set as
153 * long as no request key has yet been set.
156 * The new request key of this image
159 public Image setKey(String key) {
160 checkNotNull(key, "key must not be null");
161 checkState((this.key == null) || this.key.equals(key), "key must not be already set to another key");
167 * Returns whether the image has already been inserted. An image is
168 * considered as having been inserted it its {@link #getKey() key} is not
171 * @return {@code true} if there is a key for this image, {@code false}
174 public boolean isInserted() {
179 * Returns the creation time of this image.
181 * @return The creation time of this image (in milliseconds since 1970, Jan
184 public long getCreationTime() {
189 * Sets the new creation time of this image. The creation time can only be
190 * set as long as no creation time has been set yet.
192 * @param creationTime
193 * The new creation time of this image
196 public Image setCreationTime(long creationTime) {
197 checkArgument(creationTime > 0, "creationTime must be > 0");
198 checkState((this.creationTime == 0) || (this.creationTime == creationTime), "creationTime must not already be set");
199 this.creationTime = creationTime;
204 * Returns the width of this image.
206 * @return The width of this image (in pixels)
208 public int getWidth() {
213 * Sets the width of this image. The width can only be set as long as no
214 * width has been set yet.
217 * The new width of this image
220 public Image setWidth(int width) {
221 checkArgument(width > 0, "width must be > 0");
222 checkState((this.width == 0) || (this.width == width), "width must not already be set to another width");
228 * Returns the height of this image.
230 * @return The height of this image (in pixels)
232 public int getHeight() {
237 * Sets the new height of this image. The height can only be set as long as
238 * no height has yet been set.
241 * The new height of this image
244 public Image setHeight(int height) {
245 checkArgument(height > 0, "height must be > 0");
246 checkState((this.height == 0) || (this.height == height), "height must not already be set to another height");
247 this.height = height;
252 * Returns the title of this image.
254 * @return The title of this image
256 public String getTitle() {
261 * Sets the title of this image.
264 * The title of this image
267 public Image setTitle(String title) {
268 this.title = checkNotNull(title, "title must not be null");
273 * Returns the description of this image.
275 * @return The description of this image
277 public String getDescription() {
282 * Sets the description of this image.
285 * The description of this image
288 public Image setDescription(String description) {
289 this.description = checkNotNull(description, "description must not be null");
294 // FINGERPRINTABLE METHODS
301 public String getFingerprint() {
302 Hasher hash = Hashing.sha256().newHasher();
303 hash.putString("Image(");
304 hash.putString("ID(").putString(id).putString(")");
305 hash.putString("Title(").putString(title).putString(")");
306 hash.putString("Description(").putString(description).putString(")");
308 return hash.hash().toString();
319 public int hashCode() {
320 return id.hashCode();
327 public boolean equals(Object object) {
328 if (!(object instanceof Image)) {
331 return ((Image) object).id.equals(id);