🔖 Update version and latest edition
[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 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;
25
26 import java.util.UUID;
27
28 import net.pterodactylus.sone.data.Album;
29 import net.pterodactylus.sone.data.Image;
30 import net.pterodactylus.sone.data.Sone;
31
32 import com.google.common.base.Optional;
33 import com.google.common.hash.Hasher;
34 import com.google.common.hash.Hashing;
35
36 /**
37  * Container for image metadata.
38  */
39 public class ImageImpl implements Image {
40
41         /** The ID of the image. */
42         private final String id;
43
44         /** The Sone the image belongs to. */
45         private Sone sone;
46
47         /** The album this image belongs to. */
48         private Album album;
49
50         /** The request key of the image. */
51         private String key;
52
53         /** The creation time of the image. */
54         private long creationTime;
55
56         /** The width of the image. */
57         private int width;
58
59         /** The height of the image. */
60         private int height;
61
62         /** The title of the image. */
63         private String title;
64
65         /** The description of the image. */
66         private String description;
67
68         /** Creates a new image with a random ID. */
69         public ImageImpl() {
70                 this(UUID.randomUUID().toString());
71                 this.creationTime = System.currentTimeMillis();
72         }
73
74         /**
75          * Creates a new image.
76          *
77          * @param id
78          *              The ID of the image
79          */
80         public ImageImpl(String id) {
81                 this.id = checkNotNull(id, "id must not be null");
82         }
83
84         //
85         // ACCESSORS
86         //
87
88         @Override
89         public String getId() {
90                 return id;
91         }
92
93         @Override
94         public Sone getSone() {
95                 return sone;
96         }
97
98         @Override
99         public Album getAlbum() {
100                 return album;
101         }
102
103         @Override
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");
107                 this.album = album;
108                 return this;
109         }
110
111         @Override
112         public String getKey() {
113                 return key;
114         }
115
116         @Override
117         public boolean isInserted() {
118                 return key != null;
119         }
120
121         @Override
122         public long getCreationTime() {
123                 return creationTime;
124         }
125
126         @Override
127         public int getWidth() {
128                 return width;
129         }
130
131         @Override
132         public int getHeight() {
133                 return height;
134         }
135
136         @Override
137         public String getTitle() {
138                 return title;
139         }
140
141         @Override
142         public String getDescription() {
143                 return description;
144         }
145
146         public Modifier modify() throws IllegalStateException {
147                 // TODO: reenable check for local images
148                 return new Modifier() {
149                         private Optional<Sone> sone = absent();
150
151                         private Optional<Long> creationTime = absent();
152
153                         private Optional<String> key = absent();
154
155                         private Optional<String> title = absent();
156
157                         private Optional<String> description = absent();
158
159                         private Optional<Integer> width = absent();
160
161                         private Optional<Integer> height = absent();
162
163                         @Override
164                         public Modifier setSone(Sone sone) {
165                                 this.sone = fromNullable(sone);
166                                 return this;
167                         }
168
169                         @Override
170                         public Modifier setCreationTime(long creationTime) {
171                                 this.creationTime = of(creationTime);
172                                 return this;
173                         }
174
175                         @Override
176                         public Modifier setKey(String key) {
177                                 this.key = fromNullable(key);
178                                 return this;
179                         }
180
181                         @Override
182                         public Modifier setTitle(String title) {
183                                 this.title = fromNullable(title);
184                                 return this;
185                         }
186
187                         @Override
188                         public Modifier setDescription(String description) {
189                                 this.description = fromNullable(description);
190                                 return this;
191                         }
192
193                         @Override
194                         public Modifier setWidth(int width) {
195                                 this.width = of(width);
196                                 return this;
197                         }
198
199                         @Override
200                         public Modifier setHeight(int height) {
201                                 this.height = of(height);
202                                 return this;
203                         }
204
205                         @Override
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();
212                                 }
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");
215
216                                 if (sone.isPresent()) {
217                                         ImageImpl.this.sone = sone.get();
218                                 }
219                                 if (creationTime.isPresent()) {
220                                         ImageImpl.this.creationTime = creationTime.get();
221                                 }
222                                 if (key.isPresent()) {
223                                         ImageImpl.this.key = key.get();
224                                 }
225                                 if (title.isPresent()) {
226                                         ImageImpl.this.title = title.get();
227                                 }
228                                 if (description.isPresent()) {
229                                         ImageImpl.this.description = description.get();
230                                 }
231                                 if (width.isPresent()) {
232                                         ImageImpl.this.width = width.get();
233                                 }
234                                 if (height.isPresent()) {
235                                         ImageImpl.this.height = height.get();
236                                 }
237
238                                 return ImageImpl.this;
239                         }
240                 };
241         }
242
243         //
244         // FINGERPRINTABLE METHODS
245         //
246
247         @Override
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();
256         }
257
258         //
259         // OBJECT METHODS
260         //
261
262         /** {@inheritDoc} */
263         @Override
264         public int hashCode() {
265                 return id.hashCode();
266         }
267
268         /** {@inheritDoc} */
269         @Override
270         public boolean equals(Object object) {
271                 if (!(object instanceof ImageImpl)) {
272                         return false;
273                 }
274                 return ((ImageImpl) object).id.equals(id);
275         }
276
277 }