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