Use unique IDs for images
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / ImageImpl.java
1 /*
2  * Sone - ImageImpl.java - Copyright © 2011–2013 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
25 import java.util.UUID;
26
27 import net.pterodactylus.sone.data.Album;
28 import net.pterodactylus.sone.data.IdBuilder;
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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class ImageImpl implements Image {
42
43         private final IdBuilder idBuilder = new IdBuilder();
44
45         /** The ID of the image. */
46         private final String id;
47
48         /** The Sone the image belongs to. */
49         private Sone sone;
50
51         /** The album this image belongs to. */
52         private Album album;
53
54         /** The request key of the image. */
55         private String key;
56
57         /** The creation time of the image. */
58         private long creationTime;
59
60         /** The width of the image. */
61         private int width;
62
63         /** The height of the image. */
64         private int height;
65
66         /** The title of the image. */
67         private String title;
68
69         /** The description of the image. */
70         private String description;
71
72         /** Creates a new image with a random ID. */
73         public ImageImpl() {
74                 this(UUID.randomUUID().toString());
75                 this.creationTime = System.currentTimeMillis();
76         }
77
78         /**
79          * Creates a new image.
80          *
81          * @param id
82          *              The ID of the image
83          */
84         public ImageImpl(String id) {
85                 this.id = checkNotNull(id, "id must not be null");
86         }
87
88         //
89         // ACCESSORS
90         //
91
92         @Override
93         public String getId() {
94                 return idBuilder.buildId(sone.getId(), id);
95         }
96
97         @Override
98         public String getInternalId() {
99                 return id;
100         }
101
102         @Override
103         public Sone getSone() {
104                 return sone;
105         }
106
107         @Override
108         public Album getAlbum() {
109                 return album;
110         }
111
112         @Override
113         public Image setAlbum(Album album) {
114                 checkNotNull(album, "album must not be null");
115                 checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image");
116                 this.album = album;
117                 return this;
118         }
119
120         @Override
121         public String getKey() {
122                 return key;
123         }
124
125         @Override
126         public boolean isInserted() {
127                 return key != null;
128         }
129
130         @Override
131         public long getCreationTime() {
132                 return creationTime;
133         }
134
135         @Override
136         public int getWidth() {
137                 return width;
138         }
139
140         @Override
141         public int getHeight() {
142                 return height;
143         }
144
145         @Override
146         public String getTitle() {
147                 return title;
148         }
149
150         @Override
151         public String getDescription() {
152                 return description;
153         }
154
155         public Modifier modify() throws IllegalStateException {
156                 // TODO: reenable check for local images
157                 return new Modifier() {
158                         private Optional<Sone> sone = absent();
159
160                         private Optional<Long> creationTime = absent();
161
162                         private Optional<String> key = absent();
163
164                         private Optional<String> title = absent();
165
166                         private Optional<String> description = absent();
167
168                         private Optional<Integer> width = absent();
169
170                         private Optional<Integer> height = absent();
171
172                         @Override
173                         public Modifier setSone(Sone sone) {
174                                 this.sone = fromNullable(sone);
175                                 return this;
176                         }
177
178                         @Override
179                         public Modifier setCreationTime(long creationTime) {
180                                 this.creationTime = of(creationTime);
181                                 return this;
182                         }
183
184                         @Override
185                         public Modifier setKey(String key) {
186                                 this.key = fromNullable(key);
187                                 return this;
188                         }
189
190                         @Override
191                         public Modifier setTitle(String title) {
192                                 this.title = fromNullable(title);
193                                 return this;
194                         }
195
196                         @Override
197                         public Modifier setDescription(String description) {
198                                 this.description = fromNullable(description);
199                                 return this;
200                         }
201
202                         @Override
203                         public Modifier setWidth(int width) {
204                                 this.width = of(width);
205                                 return this;
206                         }
207
208                         @Override
209                         public Modifier setHeight(int height) {
210                                 this.height = of(height);
211                                 return this;
212                         }
213
214                         @Override
215                         public Image update() throws IllegalStateException {
216                                 checkState(!sone.isPresent() || (ImageImpl.this.sone == null) || sone.get().equals(ImageImpl.this.sone), "can not change Sone once set");
217                                 checkState(!creationTime.isPresent() || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime.get())), "can not change creation time once set");
218                                 checkState(!key.isPresent() || (ImageImpl.this.key == null) || key.get().equals(ImageImpl.this.key), "can not change key once set");
219                                 if (title.isPresent() && title.get().trim().isEmpty()) {
220                                         throw new ImageTitleMustNotBeEmpty();
221                                 }
222                                 checkState(!width.isPresent() || (ImageImpl.this.width == 0) || width.get().equals(ImageImpl.this.width), "can not change width once set");
223                                 checkState(!height.isPresent() || (ImageImpl.this.height == 0) || height.get().equals(ImageImpl.this.height), "can not change height once set");
224
225                                 if (sone.isPresent()) {
226                                         ImageImpl.this.sone = sone.get();
227                                 }
228                                 if (creationTime.isPresent()) {
229                                         ImageImpl.this.creationTime = creationTime.get();
230                                 }
231                                 if (key.isPresent()) {
232                                         ImageImpl.this.key = key.get();
233                                 }
234                                 if (title.isPresent()) {
235                                         ImageImpl.this.title = title.get();
236                                 }
237                                 if (description.isPresent()) {
238                                         ImageImpl.this.description = description.get();
239                                 }
240                                 if (width.isPresent()) {
241                                         ImageImpl.this.width = width.get();
242                                 }
243                                 if (height.isPresent()) {
244                                         ImageImpl.this.height = height.get();
245                                 }
246
247                                 return ImageImpl.this;
248                         }
249                 };
250         }
251
252         //
253         // FINGERPRINTABLE METHODS
254         //
255
256         @Override
257         public String getFingerprint() {
258                 Hasher hash = Hashing.sha256().newHasher();
259                 hash.putString("Image(");
260                 hash.putString("ID(").putString(id).putString(")");
261                 hash.putString("Title(").putString(title).putString(")");
262                 hash.putString("Description(").putString(description).putString(")");
263                 hash.putString(")");
264                 return hash.hash().toString();
265         }
266
267         //
268         // OBJECT METHODS
269         //
270
271         /** {@inheritDoc} */
272         @Override
273         public int hashCode() {
274                 return id.hashCode();
275         }
276
277         /** {@inheritDoc} */
278         @Override
279         public boolean equals(Object object) {
280                 if (!(object instanceof ImageImpl)) {
281                         return false;
282                 }
283                 return ((ImageImpl) object).id.equals(id);
284         }
285
286 }