🔀 Merge branch “release-80”
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AlbumImpl.java
1 /*
2  * Sone - AlbumImpl.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
18 package net.pterodactylus.sone.data.impl;
19
20 import static com.google.common.base.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Preconditions.checkArgument;
23 import static com.google.common.base.Preconditions.checkNotNull;
24 import static java.nio.charset.StandardCharsets.UTF_8;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Sone;
35
36 import com.google.common.base.Function;
37 import com.google.common.base.Optional;
38 import com.google.common.base.Predicates;
39 import com.google.common.collect.Collections2;
40 import com.google.common.hash.Hasher;
41 import com.google.common.hash.Hashing;
42
43 /**
44  * Container for images that can also contain nested {@link AlbumImpl}s.
45  */
46 public class AlbumImpl implements Album {
47
48         /** The ID of this album. */
49         private final String id;
50
51         /** The Sone this album belongs to. */
52         private final Sone sone;
53
54         /** Nested albums. */
55         private final List<Album> albums = new ArrayList<>();
56
57         /** The image IDs in order. */
58         private final List<String> imageIds = new ArrayList<>();
59
60         /** The images in this album. */
61         private final Map<String, Image> images = new HashMap<>();
62
63         /** The parent album. */
64         private Album parent;
65
66         /** The title of this album. */
67         private String title;
68
69         /** The description of this album. */
70         private String description;
71
72         /** Creates a new album with a random ID. */
73         public AlbumImpl(Sone sone) {
74                 this(sone, UUID.randomUUID().toString());
75         }
76
77         /**
78          * Creates a new album with the given ID.
79          *
80          * @param id
81          *              The ID of the album
82          */
83         public AlbumImpl(Sone sone, String id) {
84                 this.sone = checkNotNull(sone, "Sone must not be null");
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 id;
95         }
96
97         @Override
98         public Sone getSone() {
99                 return sone;
100         }
101
102         @Override
103         public List<Album> getAlbums() {
104                 return new ArrayList<>(albums);
105         }
106
107         @Override
108         public void addAlbum(Album album) {
109                 checkNotNull(album, "album must not be null");
110                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
111                 album.setParent(this);
112                 if (!albums.contains(album)) {
113                         albums.add(album);
114                 }
115         }
116
117         @Override
118         public void removeAlbum(Album album) {
119                 checkNotNull(album, "album must not be null");
120                 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
121                 checkArgument(equals(album.getParent()), "album must belong to this album");
122                 albums.remove(album);
123                 album.removeParent();
124         }
125
126         @Override
127         public Album moveAlbumUp(Album album) {
128                 checkNotNull(album, "album must not be null");
129                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
130                 checkArgument(equals(album.getParent()), "album must belong to this album");
131                 int oldIndex = albums.indexOf(album);
132                 if (oldIndex <= 0) {
133                         return album;
134                 }
135                 albums.remove(oldIndex);
136                 albums.add(oldIndex - 1, album);
137                 return albums.get(oldIndex);
138         }
139
140         @Override
141         public Album moveAlbumDown(Album album) {
142                 checkNotNull(album, "album must not be null");
143                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
144                 checkArgument(equals(album.getParent()), "album must belong to this album");
145                 int oldIndex = albums.indexOf(album);
146                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
147                         return album;
148                 }
149                 albums.remove(oldIndex);
150                 albums.add(oldIndex + 1, album);
151                 return albums.get(oldIndex);
152         }
153
154         @Override
155         public List<Image> getImages() {
156                 return new ArrayList<>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
157
158                         @Override
159                         @SuppressWarnings("synthetic-access")
160                         public Image apply(String imageId) {
161                                 return images.get(imageId);
162                         }
163                 }), Predicates.notNull()));
164         }
165
166         @Override
167         public void addImage(Image image) {
168                 checkNotNull(image, "image must not be null");
169                 checkNotNull(image.getSone(), "image must have an owner");
170                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
171                 if (image.getAlbum() != null) {
172                         image.getAlbum().removeImage(image);
173                 }
174                 image.setAlbum(this);
175                 if (!imageIds.contains(image.getId())) {
176                         imageIds.add(image.getId());
177                         images.put(image.getId(), image);
178                 }
179         }
180
181         @Override
182         public void removeImage(Image image) {
183                 checkNotNull(image, "image must not be null");
184                 checkNotNull(image.getSone(), "image must have an owner");
185                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
186                 imageIds.remove(image.getId());
187                 images.remove(image.getId());
188         }
189
190         @Override
191         public Image moveImageUp(Image image) {
192                 checkNotNull(image, "image must not be null");
193                 checkNotNull(image.getSone(), "image must have an owner");
194                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
195                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
196                 int oldIndex = imageIds.indexOf(image.getId());
197                 if (oldIndex <= 0) {
198                         return image;
199                 }
200                 imageIds.remove(image.getId());
201                 imageIds.add(oldIndex - 1, image.getId());
202                 return images.get(imageIds.get(oldIndex));
203         }
204
205         @Override
206         public Image moveImageDown(Image image) {
207                 checkNotNull(image, "image must not be null");
208                 checkNotNull(image.getSone(), "image must have an owner");
209                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
210                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
211                 int oldIndex = imageIds.indexOf(image.getId());
212                 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
213                         return image;
214                 }
215                 imageIds.remove(image.getId());
216                 imageIds.add(oldIndex + 1, image.getId());
217                 return images.get(imageIds.get(oldIndex));
218         }
219
220         @Override
221         public boolean isEmpty() {
222                 return albums.isEmpty() && images.isEmpty();
223         }
224
225         @Override
226         public boolean isRoot() {
227                 return parent == null;
228         }
229
230         @Override
231         public Album getParent() {
232                 return parent;
233         }
234
235         @Override
236         public Album setParent(Album parent) {
237                 this.parent = checkNotNull(parent, "parent must not be null");
238                 return this;
239         }
240
241         @Override
242         public Album removeParent() {
243                 this.parent = null;
244                 return this;
245         }
246
247         @Override
248         public String getTitle() {
249                 return title;
250         }
251
252         @Override
253         public String getDescription() {
254                 return description;
255         }
256
257         @Override
258         public Modifier modify() throws IllegalStateException {
259                 // TODO: reenable check for local Sones
260                 return new Modifier() {
261                         private Optional<String> title = absent();
262
263                         private Optional<String> description = absent();
264
265                         @Override
266                         public Modifier setTitle(String title) {
267                                 this.title = fromNullable(title);
268                                 return this;
269                         }
270
271                         @Override
272                         public Modifier setDescription(String description) {
273                                 this.description = fromNullable(description);
274                                 return this;
275                         }
276
277                         @Override
278                         public Album update() throws IllegalStateException {
279                                 if (title.isPresent() && title.get().trim().isEmpty()) {
280                                         throw new AlbumTitleMustNotBeEmpty();
281                                 }
282                                 if (title.isPresent()) {
283                                         AlbumImpl.this.title = title.get();
284                                 }
285                                 if (description.isPresent()) {
286                                         AlbumImpl.this.description = description.get();
287                                 }
288                                 return AlbumImpl.this;
289                         }
290                 };
291         }
292
293         //
294         // FINGERPRINTABLE METHODS
295         //
296
297         @Override
298         public String getFingerprint() {
299                 Hasher hash = Hashing.sha256().newHasher();
300                 hash.putString("Album(", UTF_8);
301                 hash.putString("ID(", UTF_8).putString(id, UTF_8).putString(")", UTF_8);
302                 hash.putString("Title(", UTF_8).putString(title, UTF_8).putString(")", UTF_8);
303                 hash.putString("Description(", UTF_8).putString(description, UTF_8).putString(")", UTF_8);
304
305                 /* add nested albums. */
306                 hash.putString("Albums(", UTF_8);
307                 for (Album album : albums) {
308                         hash.putString(album.getFingerprint(), UTF_8);
309                 }
310                 hash.putString(")", UTF_8);
311
312                 /* add images. */
313                 hash.putString("Images(", UTF_8);
314                 for (Image image : getImages()) {
315                         if (image.isInserted()) {
316                                 hash.putString(image.getFingerprint(), UTF_8);
317                         }
318                 }
319                 hash.putString(")", UTF_8);
320
321                 hash.putString(")", UTF_8);
322                 return hash.hash().toString();
323         }
324
325         //
326         // OBJECT METHODS
327         //
328
329         @Override
330         public int hashCode() {
331                 return id.hashCode();
332         }
333
334         @Override
335         public boolean equals(Object object) {
336                 if (!(object instanceof AlbumImpl)) {
337                         return false;
338                 }
339                 AlbumImpl album = (AlbumImpl) object;
340                 return id.equals(album.id);
341         }
342
343 }