2 * Sone - ImageImpl.java - Copyright © 2011–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package net.pterodactylus.sone.data;
19 import static com.google.common.base.Optional.absent;
20 import static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
24 import java.util.UUID;
26 import com.google.common.base.Optional;
27 import com.google.common.hash.Hasher;
28 import com.google.common.hash.Hashing;
31 * Container for image metadata.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class ImageImpl implements Image {
37 /** The ID of the image. */
38 private final String id;
40 /** The Sone the image belongs to. */
41 private final Sone sone;
43 /** The album this image belongs to. */
46 /** The request key of the image. */
49 /** The creation time of the image. */
50 private final long creationTime;
52 /** The width of the image. */
53 private final int width;
55 /** The height of the image. */
56 private final int height;
58 /** The title of the image. */
61 /** The description of the image. */
62 private String description;
64 /** Creates a new image with a random ID. */
65 public ImageImpl(Sone sone, long creationTime, String key, int width, int height) {
66 this(UUID.randomUUID().toString(), sone, creationTime, key, width, height);
70 * Creates a new image.
76 public ImageImpl(String id, Sone sone, long creationTime, String key, int width, int height) {
77 this.id = checkNotNull(id, "id must not be null");
79 this.creationTime = creationTime;
90 public String getId() {
95 public Sone getSone() {
100 public Album getAlbum() {
105 public String getKey() {
110 public boolean isInserted() {
115 public long getCreationTime() {
120 public int getWidth() {
125 public int getHeight() {
130 public String getTitle() {
135 public String getDescription() {
139 public Modifier modify() throws IllegalStateException {
140 // TODO: reenable check for local images
141 return new Modifier() {
142 private Optional<String> key = absent();
143 private Optional<String> title = absent();
144 private Optional<String> description = absent();
147 public Modifier setKey(String key) {
148 this.key = fromNullable(key);
153 public Modifier setTitle(String title) {
154 this.title = fromNullable(title);
159 public Modifier setDescription(String description) {
160 this.description = fromNullable(description);
165 public Image update() throws IllegalStateException {
166 checkState(!key.isPresent() || (ImageImpl.this.key == null), "key can not be changed");
168 if (key.isPresent()) {
169 ImageImpl.this.key = key.get();
171 if (title.isPresent()) {
172 ImageImpl.this.title = title.get();
174 if (description.isPresent()) {
175 ImageImpl.this.description = description.get();
178 return ImageImpl.this;
184 // FINGERPRINTABLE METHODS
188 public String getFingerprint() {
189 Hasher hash = Hashing.sha256().newHasher();
190 hash.putString("Image(");
191 hash.putString("ID(").putString(id).putString(")");
192 hash.putString("Title(").putString(title).putString(")");
193 hash.putString("Description(").putString(description).putString(")");
195 return hash.hash().toString();
204 public int hashCode() {
205 return id.hashCode();
210 public boolean equals(Object object) {
211 if (!(object instanceof ImageImpl)) {
214 return ((ImageImpl) object).id.equals(id);