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