X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FImage.java;h=952d540baa6156374da22759973472d2fa6edd77;hb=e3a3dccdfcb0f7390f8bf30ec1af0f9d9fb69496;hp=2afb0983694e727b3f04582ef52113ea7eaccb39;hpb=443400257166817e417a67c6e2630a540b1fa075;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/data/Image.java b/src/main/java/net/pterodactylus/sone/data/Image.java index 2afb098..952d540 100644 --- a/src/main/java/net/pterodactylus/sone/data/Image.java +++ b/src/main/java/net/pterodactylus/sone/data/Image.java @@ -1,5 +1,5 @@ /* - * Sone - Image.java - Copyright © 2011 David Roden + * Sone - Image.java - Copyright © 2011–2013 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 @@ -17,9 +17,14 @@ package net.pterodactylus.sone.data; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + import java.util.UUID; -import net.pterodactylus.util.validation.Validation; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; /** * Container for image metadata. @@ -34,6 +39,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; @@ -67,8 +75,7 @@ public class Image implements Fingerprintable { * The ID of the image */ public Image(String id) { - Validation.begin().isNotNull("Image ID", id).check(); - this.id = id; + this.id = checkNotNull(id, "id must not be null"); } // @@ -102,12 +109,37 @@ public class Image implements Fingerprintable { * @return This image */ public Image setSone(Sone sone) { - Validation.begin().isNull("Current Image Owner", this.sone).isNotNull("New Image Owner", sone); + checkNotNull(sone, "sone must not be null"); + checkArgument((this.sone == null) || this.sone.equals(sone), "sone must not already be set to another sone"); this.sone = sone; return this; } /** + * 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) { + checkNotNull(album, "album must not be null"); + checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image"); + this.album = album; + return this; + } + + /** * Returns the request key of this image. * * @return The request key of this image @@ -125,12 +157,25 @@ public class Image implements Fingerprintable { * @return This image */ public Image setKey(String key) { - Validation.begin().isNull("Current Image Key", this.key).isNotNull("New Image Key", key).check(); + checkNotNull(key, "key must not be null"); + checkState((this.key == null) || this.key.equals(key), "key must not be already set to another key"); 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 @@ -149,7 +194,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setCreationTime(long creationTime) { - Validation.begin().isEqual("Current Image Creation Time", this.creationTime, 0).isGreater("New Image Creation Time", creationTime, 0).check(); + checkArgument(creationTime > 0, "creationTime must be > 0"); + checkState((this.creationTime == 0) || (this.creationTime == creationTime), "creationTime must not already be set"); this.creationTime = creationTime; return this; } @@ -172,7 +218,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setWidth(int width) { - Validation.begin().isEqual("Current Image Width", this.width, 0).isGreater("New Image Width", width, 0).check(); + checkArgument(width > 0, "width must be > 0"); + checkState((this.width == 0) || (this.width == width), "width must not already be set to another width"); this.width = width; return this; } @@ -195,7 +242,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setHeight(int height) { - Validation.begin().isEqual("Current Image Height", this.height, 0).isGreater("New Image Height", height, 0); + checkArgument(height > 0, "height must be > 0"); + checkState((this.height == 0) || (this.height == height), "height must not already be set to another height"); this.height = height; return this; } @@ -217,8 +265,7 @@ public class Image implements Fingerprintable { * @return This image */ public Image setTitle(String title) { - Validation.begin().isNotNull("Image Title", title).check(); - this.title = title; + this.title = checkNotNull(title, "title must not be null"); return this; } @@ -239,8 +286,7 @@ public class Image implements Fingerprintable { * @return This image */ public Image setDescription(String description) { - Validation.begin().isNotNull("Image Description", description).check(); - this.description = description; + this.description = checkNotNull(description, "description must not be null"); return this; } @@ -253,13 +299,36 @@ public class Image implements Fingerprintable { */ @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(); + Hasher hash = Hashing.sha256().newHasher(); + hash.putString("Image("); + hash.putString("ID(").putString(id).putString(")"); + hash.putString("Title(").putString(title).putString(")"); + hash.putString("Description(").putString(description).putString(")"); + hash.putString(")"); + return hash.hash().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); } }