b357b6af02f40a5347478dc93370dcd9ab019f1e
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / ImageImpl.java
1 /*
2  * Sone - ImageImpl.java - Copyright © 2011–2019 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package net.pterodactylus.sone.data.impl;
18
19 import java.util.*;
20 import javax.annotation.*;
21
22 import com.google.common.hash.*;
23 import net.pterodactylus.sone.data.*;
24
25 import static com.google.common.base.Preconditions.*;
26 import static java.nio.charset.StandardCharsets.*;
27
28 /**
29  * Container for image metadata.
30  */
31 public class ImageImpl implements Image {
32
33         /** The ID of the image. */
34         private final String id;
35
36         /** The Sone the image belongs to. */
37         private Sone sone;
38
39         /** The album this image belongs to. */
40         private Album album;
41
42         /** The request key of the image. */
43         private String key;
44
45         /** The creation time of the image. */
46         private long creationTime;
47
48         /** The width of the image. */
49         private int width;
50
51         /** The height of the image. */
52         private int height;
53
54         /** The title of the image. */
55         private String title;
56
57         /** The description of the image. */
58         private String description;
59
60         /** Creates a new image with a random ID. */
61         public ImageImpl() {
62                 this(UUID.randomUUID().toString());
63                 this.creationTime = System.currentTimeMillis();
64         }
65
66         /**
67          * Creates a new image.
68          *
69          * @param id
70          *              The ID of the image
71          */
72         public ImageImpl(String id) {
73                 this.id = checkNotNull(id, "id must not be null");
74         }
75
76         //
77         // ACCESSORS
78         //
79
80         @Override
81         public String getId() {
82                 return id;
83         }
84
85         @Override
86         public Sone getSone() {
87                 return sone;
88         }
89
90         @Override
91         public Album getAlbum() {
92                 return album;
93         }
94
95         @Override
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");
99                 this.album = album;
100                 return this;
101         }
102
103         @Override
104         public String getKey() {
105                 return key;
106         }
107
108         @Override
109         public boolean isInserted() {
110                 return key != null;
111         }
112
113         @Override
114         public long getCreationTime() {
115                 return creationTime;
116         }
117
118         @Override
119         public int getWidth() {
120                 return width;
121         }
122
123         @Override
124         public int getHeight() {
125                 return height;
126         }
127
128         @Override
129         public String getTitle() {
130                 return title;
131         }
132
133         @Override
134         public String getDescription() {
135                 return description;
136         }
137
138         public Modifier modify() throws IllegalStateException {
139                 // TODO: reenable check for local images
140                 return new Modifier() {
141                         @Nullable
142                         private Sone sone;
143                         @Nullable
144                         private Long creationTime;
145                         @Nullable
146                         private String key;
147                         @Nullable
148                         private String title;
149                         @Nullable
150                         private String description;
151                         @Nullable
152                         private Integer width;
153                         @Nullable
154                         private Integer height;
155
156                         @Override
157                         public Modifier setSone(Sone sone) {
158                                 this.sone = sone;
159                                 return this;
160                         }
161
162                         @Override
163                         public Modifier setCreationTime(long creationTime) {
164                                 this.creationTime = creationTime;
165                                 return this;
166                         }
167
168                         @Override
169                         public Modifier setKey(String key) {
170                                 this.key = key;
171                                 return this;
172                         }
173
174                         @Override
175                         public Modifier setTitle(String title) {
176                                 this.title = title;
177                                 return this;
178                         }
179
180                         @Override
181                         public Modifier setDescription(String description) {
182                                 this.description = description;
183                                 return this;
184                         }
185
186                         @Override
187                         public Modifier setWidth(int width) {
188                                 this.width = width;
189                                 return this;
190                         }
191
192                         @Override
193                         public Modifier setHeight(int height) {
194                                 this.height = height;
195                                 return this;
196                         }
197
198                         @Override
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();
205                                 }
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");
208
209                                 if (sone != null) {
210                                         ImageImpl.this.sone = sone;
211                                 }
212                                 if (creationTime != null) {
213                                         ImageImpl.this.creationTime = creationTime;
214                                 }
215                                 if (key != null) {
216                                         ImageImpl.this.key = key;
217                                 }
218                                 if (title != null) {
219                                         ImageImpl.this.title = title;
220                                 }
221                                 if (description != null) {
222                                         ImageImpl.this.description = description;
223                                 }
224                                 if (width != null) {
225                                         ImageImpl.this.width = width;
226                                 }
227                                 if (height != null) {
228                                         ImageImpl.this.height = height;
229                                 }
230
231                                 return ImageImpl.this;
232                         }
233                 };
234         }
235
236         //
237         // FINGERPRINTABLE METHODS
238         //
239
240         @Override
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();
249         }
250
251         //
252         // OBJECT METHODS
253         //
254
255         /** {@inheritDoc} */
256         @Override
257         public int hashCode() {
258                 return id.hashCode();
259         }
260
261         /** {@inheritDoc} */
262         @Override
263         public boolean equals(Object object) {
264                 if (!(object instanceof ImageImpl)) {
265                         return false;
266                 }
267                 return ((ImageImpl) object).id.equals(id);
268         }
269
270 }