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;
27 * Container for image metadata.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class Image implements Fingerprintable {
33 /** The ID of the image. */
34 private final String id;
36 /** The Sone the image belongs to. */
39 /** The album this image belongs to. */
42 /** The request key of the image. */
45 /** The creation time of the image. */
46 private long creationTime;
48 /** The width of the image. */
51 /** The height of the image. */
54 /** The title of the image. */
57 /** The description of the image. */
58 private String description;
61 * Creates a new image with a random ID.
64 this(UUID.randomUUID().toString());
65 setCreationTime(System.currentTimeMillis());
69 * Creates a new image.
74 public Image(String id) {
75 this.id = checkNotNull(id, "id must not be null");
83 * Returns the ID of this image.
85 * @return The ID of this image
87 public String getId() {
92 * Returns the Sone this image belongs to.
94 * @return The Sone this image belongs to
96 public Sone getSone() {
101 * Sets the owner of this image. The owner can only be set if no owner has
105 * The new owner of this image
108 public Image setSone(Sone sone) {
109 checkNotNull(sone, "sone must not be null");
110 checkArgument((this.sone == null) || this.sone.equals(sone), "sone must not already be set to another sone");
116 * Returns the album this image belongs to.
118 * @return The album this image belongs to
120 public Album getAlbum() {
125 * Sets the album this image belongs to. The album of an image can only be
126 * set once, and it is usually called by {@link Album#addImage(Image)}.
129 * The album this image belongs to
132 public Image setAlbum(Album album) {
133 checkNotNull(album, "album must not be null");
134 checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image");
140 * Returns the request key of this image.
142 * @return The request key of this image
144 public String getKey() {
149 * Sets the request key of this image. The request key can only be set as
150 * long as no request key has yet been set.
153 * The new request key of this image
156 public Image setKey(String key) {
157 checkNotNull(key, "key must not be null");
158 checkState((this.key == null) || this.key.equals(key), "key must not be already set to another key");
164 * Returns whether the image has already been inserted. An image is
165 * considered as having been inserted it its {@link #getKey() key} is not
168 * @return {@code true} if there is a key for this image, {@code false}
171 public boolean isInserted() {
176 * Returns the creation time of this image.
178 * @return The creation time of this image (in milliseconds since 1970, Jan
181 public long getCreationTime() {
186 * Sets the new creation time of this image. The creation time can only be
187 * set as long as no creation time has been set yet.
189 * @param creationTime
190 * The new creation time of this image
193 public Image setCreationTime(long creationTime) {
194 checkArgument(creationTime > 0, "creationTime must be > 0");
195 checkState((this.creationTime == 0) || (this.creationTime == creationTime), "creationTime must not already be set");
196 this.creationTime = creationTime;
201 * Returns the width of this image.
203 * @return The width of this image (in pixels)
205 public int getWidth() {
210 * Sets the width of this image. The width can only be set as long as no
211 * width has been set yet.
214 * The new width of this image
217 public Image setWidth(int width) {
218 checkArgument(width > 0, "width must be > 0");
219 checkState((this.width == 0) || (this.width == width), "width must not already be set to another width");
225 * Returns the height of this image.
227 * @return The height of this image (in pixels)
229 public int getHeight() {
234 * Sets the new height of this image. The height can only be set as long as
235 * no height has yet been set.
238 * The new height of this image
241 public Image setHeight(int height) {
242 checkArgument(height > 0, "height must be > 0");
243 checkState((this.height == 0) || (this.height == height), "height must not already be set to another height");
244 this.height = height;
249 * Returns the title of this image.
251 * @return The title of this image
253 public String getTitle() {
258 * Sets the title of this image.
261 * The title of this image
264 public Image setTitle(String title) {
265 this.title = checkNotNull(title, "title must not be null");
270 * Returns the description of this image.
272 * @return The description of this image
274 public String getDescription() {
279 * Sets the description of this image.
282 * The description of this image
285 public Image setDescription(String description) {
286 this.description = checkNotNull(description, "description must not be null");
291 // FINGERPRINTABLE METHODS
298 public String getFingerprint() {
299 StringBuilder fingerprint = new StringBuilder();
300 fingerprint.append("Image(");
301 fingerprint.append("ID(").append(id).append(')');
302 fingerprint.append("Title(").append(title).append(')');
303 fingerprint.append("Description(").append(description).append(')');
304 fingerprint.append(')');
305 return fingerprint.toString();
316 public int hashCode() {
317 return id.hashCode();
324 public boolean equals(Object object) {
325 if (!(object instanceof Image)) {
328 return ((Image) object).id.equals(id);