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.impl;
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.Optional.of;
22 import static com.google.common.base.Preconditions.checkNotNull;
23 import static com.google.common.base.Preconditions.checkState;
25 import java.util.UUID;
27 import net.pterodactylus.sone.data.Album;
28 import net.pterodactylus.sone.data.Image;
29 import net.pterodactylus.sone.data.Sone;
31 import com.google.common.base.Optional;
32 import com.google.common.hash.Hasher;
33 import com.google.common.hash.Hashing;
36 * Container for image metadata.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class ImageImpl implements Image {
42 /** The ID of the image. */
43 private final String id;
45 /** The Sone the image belongs to. */
48 /** The album this image belongs to. */
51 /** The request key of the image. */
54 /** The creation time of the image. */
55 private long creationTime;
57 /** The width of the image. */
60 /** The height of the image. */
63 /** The title of the image. */
66 /** The description of the image. */
67 private String description;
69 /** Creates a new image with a random ID. */
71 this(UUID.randomUUID().toString());
72 this.creationTime = System.currentTimeMillis();
76 * Creates a new image.
81 public ImageImpl(String id) {
82 this.id = checkNotNull(id, "id must not be null");
90 public String getId() {
95 public Sone getSone() {
100 public Album getAlbum() {
105 public Image setAlbum(Album album) {
106 checkNotNull(album, "album must not be null");
107 checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image");
113 public String getKey() {
118 public boolean isInserted() {
123 public long getCreationTime() {
128 public int getWidth() {
133 public int getHeight() {
138 public String getTitle() {
143 public String getDescription() {
147 public Modifier modify() throws IllegalStateException {
148 // TODO: reenable check for local images
149 return new Modifier() {
150 private Optional<Sone> sone = absent();
152 private Optional<Long> creationTime = absent();
154 private Optional<String> key = absent();
156 private Optional<String> title = absent();
158 private Optional<String> description = absent();
160 private Optional<Integer> width = absent();
162 private Optional<Integer> height = absent();
165 public Modifier setSone(Sone sone) {
166 this.sone = fromNullable(sone);
171 public Modifier setCreationTime(long creationTime) {
172 this.creationTime = of(creationTime);
177 public Modifier setKey(String key) {
178 this.key = fromNullable(key);
183 public Modifier setTitle(String title) {
184 this.title = fromNullable(title);
189 public Modifier setDescription(String description) {
190 this.description = fromNullable(description);
195 public Modifier setWidth(int width) {
196 this.width = of(width);
201 public Modifier setHeight(int height) {
202 this.height = of(height);
207 public Image update() throws IllegalStateException {
208 checkState(!sone.isPresent() || (ImageImpl.this.sone == null) || sone.get().equals(ImageImpl.this.sone), "can not change Sone once set");
209 checkState(!creationTime.isPresent() || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime.get())), "can not change creation time once set");
210 checkState(!key.isPresent() || (ImageImpl.this.key == null) || key.get().equals(ImageImpl.this.key), "can not change key once set");
211 if (title.isPresent() && title.get().trim().isEmpty()) {
212 throw new ImageTitleMustNotBeEmpty();
214 checkState(!width.isPresent() || (ImageImpl.this.width == 0) || width.get().equals(ImageImpl.this.width), "can not change width once set");
215 checkState(!height.isPresent() || (ImageImpl.this.height == 0) || height.get().equals(ImageImpl.this.height), "can not change height once set");
217 if (sone.isPresent()) {
218 ImageImpl.this.sone = sone.get();
220 if (creationTime.isPresent()) {
221 ImageImpl.this.creationTime = creationTime.get();
223 if (key.isPresent()) {
224 ImageImpl.this.key = key.get();
226 if (title.isPresent()) {
227 ImageImpl.this.title = title.get();
229 if (description.isPresent()) {
230 ImageImpl.this.description = description.get();
232 if (width.isPresent()) {
233 ImageImpl.this.width = width.get();
235 if (height.isPresent()) {
236 ImageImpl.this.height = height.get();
239 return ImageImpl.this;
245 // FINGERPRINTABLE METHODS
249 public String getFingerprint() {
250 Hasher hash = Hashing.sha256().newHasher();
251 hash.putString("Image(");
252 hash.putString("ID(").putString(id).putString(")");
253 hash.putString("Title(").putString(title).putString(")");
254 hash.putString("Description(").putString(description).putString(")");
256 return hash.hash().toString();
265 public int hashCode() {
266 return id.hashCode();
271 public boolean equals(Object object) {
272 if (!(object instanceof ImageImpl)) {
275 return ((ImageImpl) object).id.equals(id);