2 * Sone - ImageImpl.java - Copyright © 2011–2019 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;
24 import static java.nio.charset.StandardCharsets.UTF_8;
26 import java.util.UUID;
28 import net.pterodactylus.sone.data.Album;
29 import net.pterodactylus.sone.data.Image;
30 import net.pterodactylus.sone.data.Sone;
32 import com.google.common.base.Optional;
33 import com.google.common.hash.Hasher;
34 import com.google.common.hash.Hashing;
37 * Container for image metadata.
39 public class ImageImpl implements Image {
41 /** The ID of the image. */
42 private final String id;
44 /** The Sone the image belongs to. */
47 /** The album this image belongs to. */
50 /** The request key of the image. */
53 /** The creation time of the image. */
54 private long creationTime;
56 /** The width of the image. */
59 /** The height of the image. */
62 /** The title of the image. */
65 /** The description of the image. */
66 private String description;
68 /** Creates a new image with a random ID. */
70 this(UUID.randomUUID().toString());
71 this.creationTime = System.currentTimeMillis();
75 * Creates a new image.
80 public ImageImpl(String id) {
81 this.id = checkNotNull(id, "id must not be null");
89 public String getId() {
94 public Sone getSone() {
99 public Album getAlbum() {
104 public Image setAlbum(Album album) {
105 checkNotNull(album, "album must not be null");
106 checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image");
112 public String getKey() {
117 public boolean isInserted() {
122 public long getCreationTime() {
127 public int getWidth() {
132 public int getHeight() {
137 public String getTitle() {
142 public String getDescription() {
146 public Modifier modify() throws IllegalStateException {
147 // TODO: reenable check for local images
148 return new Modifier() {
149 private Optional<Sone> sone = absent();
151 private Optional<Long> creationTime = absent();
153 private Optional<String> key = absent();
155 private Optional<String> title = absent();
157 private Optional<String> description = absent();
159 private Optional<Integer> width = absent();
161 private Optional<Integer> height = absent();
164 public Modifier setSone(Sone sone) {
165 this.sone = fromNullable(sone);
170 public Modifier setCreationTime(long creationTime) {
171 this.creationTime = of(creationTime);
176 public Modifier setKey(String key) {
177 this.key = fromNullable(key);
182 public Modifier setTitle(String title) {
183 this.title = fromNullable(title);
188 public Modifier setDescription(String description) {
189 this.description = fromNullable(description);
194 public Modifier setWidth(int width) {
195 this.width = of(width);
200 public Modifier setHeight(int height) {
201 this.height = of(height);
206 public Image update() throws IllegalStateException {
207 checkState(!sone.isPresent() || (ImageImpl.this.sone == null) || sone.get().equals(ImageImpl.this.sone), "can not change Sone once set");
208 checkState(!creationTime.isPresent() || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime.get())), "can not change creation time once set");
209 checkState(!key.isPresent() || (ImageImpl.this.key == null) || key.get().equals(ImageImpl.this.key), "can not change key once set");
210 if (title.isPresent() && title.get().trim().isEmpty()) {
211 throw new ImageTitleMustNotBeEmpty();
213 checkState(!width.isPresent() || (ImageImpl.this.width == 0) || width.get().equals(ImageImpl.this.width), "can not change width once set");
214 checkState(!height.isPresent() || (ImageImpl.this.height == 0) || height.get().equals(ImageImpl.this.height), "can not change height once set");
216 if (sone.isPresent()) {
217 ImageImpl.this.sone = sone.get();
219 if (creationTime.isPresent()) {
220 ImageImpl.this.creationTime = creationTime.get();
222 if (key.isPresent()) {
223 ImageImpl.this.key = key.get();
225 if (title.isPresent()) {
226 ImageImpl.this.title = title.get();
228 if (description.isPresent()) {
229 ImageImpl.this.description = description.get();
231 if (width.isPresent()) {
232 ImageImpl.this.width = width.get();
234 if (height.isPresent()) {
235 ImageImpl.this.height = height.get();
238 return ImageImpl.this;
244 // FINGERPRINTABLE METHODS
248 public String getFingerprint() {
249 Hasher hash = Hashing.sha256().newHasher();
250 hash.putString("Image(", UTF_8);
251 hash.putString("ID(", UTF_8).putString(id, UTF_8).putString(")", UTF_8);
252 hash.putString("Title(", UTF_8).putString(title, UTF_8).putString(")", UTF_8);
253 hash.putString("Description(", UTF_8).putString(description, UTF_8).putString(")", UTF_8);
254 hash.putString(")", UTF_8);
255 return hash.hash().toString();
264 public int hashCode() {
265 return id.hashCode();
270 public boolean equals(Object object) {
271 if (!(object instanceof ImageImpl)) {
274 return ((ImageImpl) object).id.equals(id);