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