2 * Sone - ImageImpl.java - Copyright © 2011–2020 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;
20 import javax.annotation.*;
22 import com.google.common.hash.*;
23 import net.pterodactylus.sone.data.*;
25 import static com.google.common.base.Preconditions.*;
26 import static java.nio.charset.StandardCharsets.*;
29 * Container for image metadata.
31 public class ImageImpl implements Image {
33 /** The ID of the image. */
34 private final String id;
36 /** The Sone the image belongs to. */
39 /** The album this image belongs to. */
42 /** The request key of the image. */
45 /** The creation time of the image. */
46 private long creationTime;
48 /** The width of the image. */
51 /** The height of the image. */
54 /** The title of the image. */
57 /** The description of the image. */
58 private String description;
60 /** Creates a new image with a random ID. */
62 this(UUID.randomUUID().toString());
63 this.creationTime = System.currentTimeMillis();
67 * Creates a new image.
72 public ImageImpl(String id) {
73 this.id = checkNotNull(id, "id must not be null");
81 public String getId() {
86 public Sone getSone() {
91 public Album getAlbum() {
96 public Image setAlbum(Album album) {
97 checkNotNull(album, "album must not be null");
98 checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image");
104 public String getKey() {
109 public boolean isInserted() {
114 public long getCreationTime() {
119 public int getWidth() {
124 public int getHeight() {
129 public String getTitle() {
134 public String getDescription() {
138 public Modifier modify() throws IllegalStateException {
139 // TODO: reenable check for local images
140 return new Modifier() {
144 private Long creationTime;
148 private String title;
150 private String description;
152 private Integer width;
154 private Integer height;
157 public Modifier setSone(Sone sone) {
163 public Modifier setCreationTime(long creationTime) {
164 this.creationTime = creationTime;
169 public Modifier setKey(String key) {
175 public Modifier setTitle(String title) {
181 public Modifier setDescription(String description) {
182 this.description = description;
187 public Modifier setWidth(int width) {
193 public Modifier setHeight(int height) {
194 this.height = height;
199 public Image update() throws IllegalStateException {
200 checkState(sone == null || (ImageImpl.this.sone == null) || sone.equals(ImageImpl.this.sone), "can not change Sone once set");
201 checkState(creationTime == null || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime)), "can not change creation time once set");
202 checkState(key == null || (ImageImpl.this.key == null) || key.equals(ImageImpl.this.key), "can not change key once set");
203 if (title != null && title.trim().isEmpty()) {
204 throw new ImageTitleMustNotBeEmpty();
206 checkState(width == null || (ImageImpl.this.width == 0) || width.equals(ImageImpl.this.width), "can not change width once set");
207 checkState(height == null || (ImageImpl.this.height == 0) || height.equals(ImageImpl.this.height), "can not change height once set");
210 ImageImpl.this.sone = sone;
212 if (creationTime != null) {
213 ImageImpl.this.creationTime = creationTime;
216 ImageImpl.this.key = key;
219 ImageImpl.this.title = title;
221 if (description != null) {
222 ImageImpl.this.description = description;
225 ImageImpl.this.width = width;
227 if (height != null) {
228 ImageImpl.this.height = height;
231 return ImageImpl.this;
237 // FINGERPRINTABLE METHODS
241 public String getFingerprint() {
242 Hasher hash = Hashing.sha256().newHasher();
243 hash.putString("Image(", UTF_8);
244 hash.putString("ID(", UTF_8).putString(id, UTF_8).putString(")", UTF_8);
245 hash.putString("Title(", UTF_8).putString(title, UTF_8).putString(")", UTF_8);
246 hash.putString("Description(", UTF_8).putString(description, UTF_8).putString(")", UTF_8);
247 hash.putString(")", UTF_8);
248 return hash.hash().toString();
257 public int hashCode() {
258 return id.hashCode();
263 public boolean equals(Object object) {
264 if (!(object instanceof ImageImpl)) {
267 return ((ImageImpl) object).id.equals(id);