Store parent album in image.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Image.java
index 587c15e..d6e7114 100644 (file)
@@ -34,7 +34,10 @@ public class Image implements Fingerprintable {
        /** The Sone the image belongs to. */
        private Sone sone;
 
-       /** The key of the image. */
+       /** The album this image belongs to. */
+       private Album album;
+
+       /** The request key of the image. */
        private String key;
 
        /** The creation time of the image. */
@@ -57,6 +60,7 @@ public class Image implements Fingerprintable {
         */
        public Image() {
                this(UUID.randomUUID().toString());
+               setCreationTime(System.currentTimeMillis());
        }
 
        /**
@@ -107,20 +111,43 @@ public class Image implements Fingerprintable {
        }
 
        /**
-        * Returns the key of this image.
+        * Returns the album this image belongs to.
+        *
+        * @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)}.
         *
-        * @return The key of this image
+        * @param album
+        *            The album this image belongs to
+        * @return This image
+        */
+       public Image setAlbum(Album album) {
+               Validation.begin().isNull("Current Album", this.album).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 key of this image. The key can only be set as long as no key has
-        * yet been set.
+        * 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 key of this image
+        *            The new request key of this image
         * @return This image
         */
        public Image setKey(String key) {
@@ -130,6 +157,18 @@ public class Image implements Fingerprintable {
        }
 
        /**
+        * 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