X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FImage.java;h=d8a8ab192d9770c693b64caa7949fc80b2f375ee;hb=20436264b1f2a05c6e3e3e64290656c732e77dba;hp=0ae373afc5345d1cd022135223b6eaebbc9c88a9;hpb=f76f59c8cf8dbc38a24a7450e1a78f5bb4791fec;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 0ae373a..d8a8ab1 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,32 +17,42 @@ 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 com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; + /** * Container for image metadata. * * @author David ‘Bombe’ Roden */ -public class Image { +public class Image implements Identified, Fingerprintable { /** The ID of the image. */ private final String id; /** The Sone the image belongs to. */ - private final Sone sone; + private Sone sone; - /** The key of the image. */ - private final String key; + /** 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; @@ -51,21 +61,11 @@ public class Image { private String description; /** - * Creates a new image. - * - * @param sone - * The Sone the image belongs to - * @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(Sone sone, String key, long creationTime, int width, int height) { - this(UUID.randomUUID().toString(), sone, key, creationTime, width, height); + public Image() { + this(UUID.randomUUID().toString()); + setCreationTime(System.currentTimeMillis()); } /** @@ -73,24 +73,9 @@ public class Image { * * @param id * The ID of the image - * @param sone - * The Sone the image belongs to - * @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, Sone sone, String key, long creationTime, int width, int height) { - this.id = id; - this.sone = sone; - this.key = key; - this.creationTime = creationTime; - this.width = width; - this.height = height; + public Image(String id) { + this.id = checkNotNull(id, "id must not be null"); } // @@ -116,15 +101,81 @@ public class Image { } /** - * Returns the key of this image. + * 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) { + 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 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) { + 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 */ 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) { + 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 @@ -135,6 +186,21 @@ 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) { + 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; + } + + /** * Returns the width of this image. * * @return The width of this image (in pixels) @@ -144,6 +210,21 @@ 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) { + 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; + } + + /** * Returns the height of this image. * * @return The height of this image (in pixels) @@ -153,6 +234,21 @@ 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) { + 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; + } + + /** * Returns the title of this image. * * @return The title of this image @@ -169,7 +265,7 @@ public class Image { * @return This image */ public Image setTitle(String title) { - this.title = title; + this.title = checkNotNull(title, "title must not be null"); return this; } @@ -190,8 +286,49 @@ public class Image { * @return This image */ public Image setDescription(String description) { - this.description = description; + this.description = checkNotNull(description, "description must not be null"); return this; } + // + // FINGERPRINTABLE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public String getFingerprint() { + 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); + } + }