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