Change all copyright headers to include 2012.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Image.java
index cae2520..25a36e3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - Image.java - Copyright © 2011 David Roden
+ * Sone - Image.java - Copyright © 2011–2012 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,27 +19,35 @@ package net.pterodactylus.sone.data;
 
 import java.util.UUID;
 
+import net.pterodactylus.util.validation.Validation;
+
 /**
  * Container for image metadata.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class Image {
+public class Image implements Fingerprintable {
 
        /** The ID of the image. */
        private final String id;
 
-       /** The key of the image. */
-       private final String key;
+       /** The Sone the image belongs to. */
+       private Sone sone;
+
+       /** The album this image belongs to. */
+       private Album album;
+
+       /** The request key of the image. */
+       private String key;
 
        /** The creation time of the image. */
-       private final long creationTime;
+       private long creationTime;
 
        /** The width of the image. */
-       private final int width;
+       private int width;
 
        /** The height of the image. */
-       private final int height;
+       private int height;
 
        /** The title of the image. */
        private String title;
@@ -48,19 +56,11 @@ public class Image {
        private String description;
 
        /**
-        * Creates a new image.
-        *
-        * @param key
-        *            The key of the image
-        * @param creationTime
-        *            The creation time of the image
-        * @param width
-        *            The width of the image
-        * @param height
-        *            The height of the image
+        * Creates a new image with a random ID.
         */
-       public Image(String key, long creationTime, int width, int height) {
-               this(UUID.randomUUID().toString(), key, creationTime, width, height);
+       public Image() {
+               this(UUID.randomUUID().toString());
+               setCreationTime(System.currentTimeMillis());
        }
 
        /**
@@ -68,21 +68,10 @@ public class Image {
         *
         * @param id
         *            The ID of the image
-        * @param key
-        *            The key of the image
-        * @param creationTime
-        *            The creation time of the image
-        * @param width
-        *            The width of the image
-        * @param height
-        *            The height of the image
         */
-       public Image(String id, String key, long creationTime, int width, int height) {
+       public Image(String id) {
+               Validation.begin().isNotNull("Image ID", id).check();
                this.id = id;
-               this.key = key;
-               this.creationTime = creationTime;
-               this.width = width;
-               this.height = height;
        }
 
        //
@@ -99,15 +88,87 @@ public class Image {
        }
 
        /**
-        * Returns the key of this image.
+        * Returns the Sone this image belongs to.
+        *
+        * @return The Sone this image belongs to
+        */
+       public Sone getSone() {
+               return sone;
+       }
+
+       /**
+        * Sets the owner of this image. The owner can only be set if no owner has
+        * yet been set.
+        *
+        * @param sone
+        *            The new owner of this image
+        * @return This image
+        */
+       public Image setSone(Sone sone) {
+               Validation.begin().isNotNull("New Image Owner", sone).isEither("Old Image Owner", this.sone, null, sone).check();
+               this.sone = sone;
+               return this;
+       }
+
+       /**
+        * Returns the album this image belongs to.
         *
-        * @return The key of this image
+        * @return The album this image belongs to
+        */
+       public Album getAlbum() {
+               return album;
+       }
+
+       /**
+        * Sets the album this image belongs to. The album of an image can only be
+        * set once, and it is usually called by {@link Album#addImage(Image)}.
+        *
+        * @param album
+        *            The album this image belongs to
+        * @return This image
+        */
+       public Image setAlbum(Album album) {
+               Validation.begin().isNotNull("New Album", album).check().isEqual("Album Owner and Image Owner", album.getSone(), getSone()).check();
+               this.album = album;
+               return this;
+       }
+
+       /**
+        * Returns the request key of this image.
+        *
+        * @return The request key of this image
         */
        public String getKey() {
                return key;
        }
 
        /**
+        * Sets the request key of this image. The request key can only be set as
+        * long as no request key has yet been set.
+        *
+        * @param key
+        *            The new request key of this image
+        * @return This image
+        */
+       public Image setKey(String key) {
+               Validation.begin().isNotNull("New Image Key", key).isEither("Old Image Key", this.key, null, key).check();
+               this.key = key;
+               return this;
+       }
+
+       /**
+        * Returns whether the image has already been inserted. An image is
+        * considered as having been inserted it its {@link #getKey() key} is not
+        * {@code null}.
+        *
+        * @return {@code true} if there is a key for this image, {@code false}
+        *         otherwise
+        */
+       public boolean isInserted() {
+               return key != null;
+       }
+
+       /**
         * Returns the creation time of this image.
         *
         * @return The creation time of this image (in milliseconds since 1970, Jan
@@ -118,6 +179,20 @@ public class Image {
        }
 
        /**
+        * Sets the new creation time of this image. The creation time can only be
+        * set as long as no creation time has been set yet.
+        *
+        * @param creationTime
+        *            The new creation time of this image
+        * @return This image
+        */
+       public Image setCreationTime(long creationTime) {
+               Validation.begin().isGreater("New Image Creation Time", creationTime, 0).isEither("Old Image Creation Time", this.creationTime, 0L, creationTime).check();
+               this.creationTime = creationTime;
+               return this;
+       }
+
+       /**
         * Returns the width of this image.
         *
         * @return The width of this image (in pixels)
@@ -127,6 +202,20 @@ public class Image {
        }
 
        /**
+        * Sets the width of this image. The width can only be set as long as no
+        * width has been set yet.
+        *
+        * @param width
+        *            The new width of this image
+        * @return This image
+        */
+       public Image setWidth(int width) {
+               Validation.begin().isGreater("New Image Width", width, 0).isEither("Old Image Width", this.width, 0, width).check();
+               this.width = width;
+               return this;
+       }
+
+       /**
         * Returns the height of this image.
         *
         * @return The height of this image (in pixels)
@@ -136,6 +225,20 @@ public class Image {
        }
 
        /**
+        * Sets the new height of this image. The height can only be set as long as
+        * no height has yet been set.
+        *
+        * @param height
+        *            The new height of this image
+        * @return This image
+        */
+       public Image setHeight(int height) {
+               Validation.begin().isGreater("New Image Height", height, 0).isEither("Old Image Height", this.height, 0, height).check();
+               this.height = height;
+               return this;
+       }
+
+       /**
         * Returns the title of this image.
         *
         * @return The title of this image
@@ -152,6 +255,7 @@ public class Image {
         * @return This image
         */
        public Image setTitle(String title) {
+               Validation.begin().isNotNull("Image Title", title).check();
                this.title = title;
                return this;
        }
@@ -173,8 +277,50 @@ public class Image {
         * @return This image
         */
        public Image setDescription(String description) {
+               Validation.begin().isNotNull("Image Description", description).check();
                this.description = description;
                return this;
        }
 
+       //
+       // FINGERPRINTABLE METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public String getFingerprint() {
+               StringBuilder fingerprint = new StringBuilder();
+               fingerprint.append("Image(");
+               fingerprint.append("ID(").append(id).append(')');
+               fingerprint.append("Title(").append(title).append(')');
+               fingerprint.append("Description(").append(description).append(')');
+               fingerprint.append(')');
+               return fingerprint.toString();
+       }
+
+       //
+       // OBJECT METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public int hashCode() {
+               return id.hashCode();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean equals(Object object) {
+               if (!(object instanceof Image)) {
+                       return false;
+               }
+               return ((Image) object).id.equals(id);
+       }
+
 }