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