Override hashCode() and equals().
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Image.java
index f38a4fa..0506172 100644 (file)
@@ -34,6 +34,9 @@ public class Image implements Fingerprintable {
        /** 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;
 
@@ -108,6 +111,29 @@ public class Image implements Fingerprintable {
        }
 
        /**
+        * 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)}.
+        *
+        * @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
@@ -274,4 +300,27 @@ public class Image implements Fingerprintable {
                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);
+       }
+
 }