Merge branch 'release-0.8.9'
[Sone.git] / src / main / java / net / pterodactylus / sone / data / 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;
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 com.google.common.base.Function;
33 import com.google.common.base.Optional;
34 import com.google.common.base.Predicates;
35 import com.google.common.collect.Collections2;
36 import com.google.common.hash.Hasher;
37 import com.google.common.hash.Hashing;
38
39 /**
40  * Container for images that can also contain nested {@link AlbumImpl}s.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class AlbumImpl implements Album {
45
46         /** The ID of this album. */
47         private final String id;
48
49         /** The Sone this album belongs to. */
50         private Sone sone;
51
52         /** Nested albums. */
53         private final List<Album> albums = new ArrayList<Album>();
54
55         /** The image IDs in order. */
56         private final List<String> imageIds = new ArrayList<String>();
57
58         /** The images in this album. */
59         private final Map<String, Image> images = new HashMap<String, Image>();
60
61         /** The parent album. */
62         private Album parent;
63
64         /** The title of this album. */
65         private String title;
66
67         /** The description of this album. */
68         private String description;
69
70         /** The ID of the album picture. */
71         private String albumImage;
72
73         /** Creates a new album with a random ID. */
74         public AlbumImpl() {
75                 this(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(String id) {
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 Album setSone(Sone sone) {
104                 checkNotNull(sone, "sone must not be null");
105                 checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
106                 this.sone = sone;
107                 return this;
108         }
109
110         @Override
111         public List<Album> getAlbums() {
112                 return new ArrayList<Album>(albums);
113         }
114
115         @Override
116         public void addAlbum(Album album) {
117                 checkNotNull(album, "album must not be null");
118                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
119                 album.setParent(this);
120                 if (!albums.contains(album)) {
121                         albums.add(album);
122                 }
123         }
124
125         @Override
126         public void removeAlbum(Album album) {
127                 checkNotNull(album, "album must not be null");
128                 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
129                 checkArgument(equals(album.getParent()), "album must belong to this album");
130                 albums.remove(album);
131                 album.removeParent();
132         }
133
134         @Override
135         public Album moveAlbumUp(Album album) {
136                 checkNotNull(album, "album must not be null");
137                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
138                 checkArgument(equals(album.getParent()), "album must belong to this album");
139                 int oldIndex = albums.indexOf(album);
140                 if (oldIndex <= 0) {
141                         return null;
142                 }
143                 albums.remove(oldIndex);
144                 albums.add(oldIndex - 1, album);
145                 return albums.get(oldIndex);
146         }
147
148         @Override
149         public Album moveAlbumDown(Album album) {
150                 checkNotNull(album, "album must not be null");
151                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
152                 checkArgument(equals(album.getParent()), "album must belong to this album");
153                 int oldIndex = albums.indexOf(album);
154                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
155                         return null;
156                 }
157                 albums.remove(oldIndex);
158                 albums.add(oldIndex + 1, album);
159                 return albums.get(oldIndex);
160         }
161
162         @Override
163         public List<Image> getImages() {
164                 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
165
166                         @Override
167                         @SuppressWarnings("synthetic-access")
168                         public Image apply(String imageId) {
169                                 return images.get(imageId);
170                         }
171                 }), Predicates.notNull()));
172         }
173
174         @Override
175         public void addImage(Image image) {
176                 checkNotNull(image, "image must not be null");
177                 checkNotNull(image.getSone(), "image must have an owner");
178                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
179                 if (image.getAlbum() != null) {
180                         image.getAlbum().removeImage(image);
181                 }
182                 image.setAlbum(this);
183                 if (imageIds.isEmpty() && (albumImage == null)) {
184                         albumImage = image.getId();
185                 }
186                 if (!imageIds.contains(image.getId())) {
187                         imageIds.add(image.getId());
188                         images.put(image.getId(), image);
189                 }
190         }
191
192         @Override
193         public void removeImage(Image image) {
194                 checkNotNull(image, "image must not be null");
195                 checkNotNull(image.getSone(), "image must have an owner");
196                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
197                 imageIds.remove(image.getId());
198                 images.remove(image.getId());
199                 if (image.getId().equals(albumImage)) {
200                         if (images.isEmpty()) {
201                                 albumImage = null;
202                         } else {
203                                 albumImage = images.values().iterator().next().getId();
204                         }
205                 }
206         }
207
208         @Override
209         public Image moveImageUp(Image image) {
210                 checkNotNull(image, "image must not be null");
211                 checkNotNull(image.getSone(), "image must have an owner");
212                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
213                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
214                 int oldIndex = imageIds.indexOf(image.getId());
215                 if (oldIndex <= 0) {
216                         return null;
217                 }
218                 imageIds.remove(image.getId());
219                 imageIds.add(oldIndex - 1, image.getId());
220                 return images.get(imageIds.get(oldIndex));
221         }
222
223         @Override
224         public Image moveImageDown(Image image) {
225                 checkNotNull(image, "image must not be null");
226                 checkNotNull(image.getSone(), "image must have an owner");
227                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
228                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
229                 int oldIndex = imageIds.indexOf(image.getId());
230                 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
231                         return null;
232                 }
233                 imageIds.remove(image.getId());
234                 imageIds.add(oldIndex + 1, image.getId());
235                 return images.get(imageIds.get(oldIndex));
236         }
237
238         @Override
239         public Image getAlbumImage() {
240                 if (albumImage == null) {
241                         return null;
242                 }
243                 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
244         }
245
246         @Override
247         public boolean isEmpty() {
248                 return albums.isEmpty() && images.isEmpty();
249         }
250
251         @Override
252         public boolean isRoot() {
253                 return parent == null;
254         }
255
256         @Override
257         public Album getParent() {
258                 return parent;
259         }
260
261         @Override
262         public Album setParent(Album parent) {
263                 this.parent = checkNotNull(parent, "parent must not be null");
264                 return this;
265         }
266
267         @Override
268         public Album removeParent() {
269                 this.parent = null;
270                 return this;
271         }
272
273         @Override
274         public String getTitle() {
275                 return title;
276         }
277
278         @Override
279         public String getDescription() {
280                 return description;
281         }
282
283         @Override
284         public Modifier modify() throws IllegalStateException {
285                 // TODO: reenable check for local Sones
286                 return new Modifier() {
287                         private Optional<String> title = absent();
288
289                         private Optional<String> description = absent();
290
291                         private Optional<String> albumImage = absent();
292
293                         @Override
294                         public Modifier setTitle(String title) {
295                                 this.title = fromNullable(title);
296                                 return this;
297                         }
298
299                         @Override
300                         public Modifier setDescription(String description) {
301                                 this.description = fromNullable(description);
302                                 return this;
303                         }
304
305                         @Override
306                         public Modifier setAlbumImage(String imageId) {
307                                 this.albumImage = fromNullable(imageId);
308                                 return this;
309                         }
310
311                         @Override
312                         public Album update() throws IllegalStateException {
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 }