Move default Sone implementation to better package.
[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 final 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(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 null;
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 null;
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.isEmpty() && (albumImage == null)) {
177                         albumImage = image.getId();
178                 }
179                 if (!imageIds.contains(image.getId())) {
180                         imageIds.add(image.getId());
181                         images.put(image.getId(), image);
182                 }
183         }
184
185         @Override
186         public void removeImage(Image image) {
187                 checkNotNull(image, "image must not be null");
188                 checkNotNull(image.getSone(), "image must have an owner");
189                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
190                 imageIds.remove(image.getId());
191                 images.remove(image.getId());
192                 if (image.getId().equals(albumImage)) {
193                         if (images.isEmpty()) {
194                                 albumImage = null;
195                         } else {
196                                 albumImage = images.values().iterator().next().getId();
197                         }
198                 }
199         }
200
201         @Override
202         public Image moveImageUp(Image image) {
203                 checkNotNull(image, "image must not be null");
204                 checkNotNull(image.getSone(), "image must have an owner");
205                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
206                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
207                 int oldIndex = imageIds.indexOf(image.getId());
208                 if (oldIndex <= 0) {
209                         return null;
210                 }
211                 imageIds.remove(image.getId());
212                 imageIds.add(oldIndex - 1, image.getId());
213                 return images.get(imageIds.get(oldIndex));
214         }
215
216         @Override
217         public Image moveImageDown(Image image) {
218                 checkNotNull(image, "image must not be null");
219                 checkNotNull(image.getSone(), "image must have an owner");
220                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
221                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
222                 int oldIndex = imageIds.indexOf(image.getId());
223                 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
224                         return null;
225                 }
226                 imageIds.remove(image.getId());
227                 imageIds.add(oldIndex + 1, image.getId());
228                 return images.get(imageIds.get(oldIndex));
229         }
230
231         @Override
232         public Image getAlbumImage() {
233                 if (albumImage == null) {
234                         return null;
235                 }
236                 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
237         }
238
239         @Override
240         public boolean isEmpty() {
241                 return albums.isEmpty() && images.isEmpty();
242         }
243
244         @Override
245         public boolean isRoot() {
246                 return parent == null;
247         }
248
249         @Override
250         public Album getParent() {
251                 return parent;
252         }
253
254         @Override
255         public Album setParent(Album parent) {
256                 this.parent = checkNotNull(parent, "parent must not be null");
257                 return this;
258         }
259
260         @Override
261         public Album removeParent() {
262                 this.parent = null;
263                 return this;
264         }
265
266         @Override
267         public String getTitle() {
268                 return title;
269         }
270
271         @Override
272         public String getDescription() {
273                 return description;
274         }
275
276         @Override
277         public Modifier modify() throws IllegalStateException {
278                 // TODO: reenable check for local Sones
279                 return new Modifier() {
280                         private Optional<String> title = absent();
281
282                         private Optional<String> description = absent();
283
284                         private Optional<String> albumImage = absent();
285
286                         @Override
287                         public Modifier setTitle(String title) {
288                                 this.title = fromNullable(title);
289                                 return this;
290                         }
291
292                         @Override
293                         public Modifier setDescription(String description) {
294                                 this.description = fromNullable(description);
295                                 return this;
296                         }
297
298                         @Override
299                         public Modifier setAlbumImage(String imageId) {
300                                 this.albumImage = fromNullable(imageId);
301                                 return this;
302                         }
303
304                         @Override
305                         public Album update() throws IllegalStateException {
306                                 if (title.isPresent() && title.get().trim().isEmpty()) {
307                                         throw new AlbumTitleMustNotBeEmpty();
308                                 }
309                                 if (title.isPresent()) {
310                                         AlbumImpl.this.title = title.get();
311                                 }
312                                 if (description.isPresent()) {
313                                         AlbumImpl.this.description = description.get();
314                                 }
315                                 if (albumImage.isPresent()) {
316                                         AlbumImpl.this.albumImage = albumImage.get();
317                                 }
318                                 return AlbumImpl.this;
319                         }
320                 };
321         }
322
323         public static class AlbumTitleMustNotBeEmpty extends IllegalStateException { }
324
325         //
326         // FINGERPRINTABLE METHODS
327         //
328
329         @Override
330         public String getFingerprint() {
331                 Hasher hash = Hashing.sha256().newHasher();
332                 hash.putString("Album(");
333                 hash.putString("ID(").putString(id).putString(")");
334                 hash.putString("Title(").putString(title).putString(")");
335                 hash.putString("Description(").putString(description).putString(")");
336                 if (albumImage != null) {
337                         hash.putString("AlbumImage(").putString(albumImage).putString(")");
338                 }
339
340                 /* add nested albums. */
341                 hash.putString("Albums(");
342                 for (Album album : albums) {
343                         hash.putString(album.getFingerprint());
344                 }
345                 hash.putString(")");
346
347                 /* add images. */
348                 hash.putString("Images(");
349                 for (Image image : getImages()) {
350                         if (image.isInserted()) {
351                                 hash.putString(image.getFingerprint());
352                         }
353                 }
354                 hash.putString(")");
355
356                 hash.putString(")");
357                 return hash.hash().toString();
358         }
359
360         //
361         // OBJECT METHODS
362         //
363
364         @Override
365         public int hashCode() {
366                 return id.hashCode();
367         }
368
369         @Override
370         public boolean equals(Object object) {
371                 if (!(object instanceof AlbumImpl)) {
372                         return false;
373                 }
374                 AlbumImpl album = (AlbumImpl) object;
375                 return id.equals(album.id);
376         }
377
378 }