Set Sone of an album in the album builder, use album builder to add albums.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.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 java.util.Arrays.asList;
21 import static java.util.Collections.emptyList;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27 import javax.annotation.Nonnull;
28
29 import net.pterodactylus.sone.database.AlbumBuilder;
30 import net.pterodactylus.sone.database.ImageBuilder;
31
32 import com.google.common.base.Function;
33 import com.google.common.base.Predicate;
34 import com.google.common.collect.FluentIterable;
35 import com.google.common.collect.ImmutableList;
36
37 /**
38  * Container for images that can also contain nested {@link Album}s.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public interface Album extends Identified, Fingerprintable {
43
44         /** Compares two {@link Album}s by {@link #getTitle()}. */
45         Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
46
47                 @Override
48                 public int compare(Album leftAlbum, Album rightAlbum) {
49                         return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
50                 }
51         };
52
53         /** Function that flattens the given album and all albums beneath it. */
54         Function<Album, List<Album>> FLATTENER = new Function<Album, List<Album>>() {
55
56                 @Override
57                 @Nonnull
58                 public List<Album> apply(Album album) {
59                         if (album == null) {
60                                 return emptyList();
61                         }
62                         List<Album> albums = new ArrayList<Album>();
63                         albums.add(album);
64                         for (Album subAlbum : album.getAlbums()) {
65                                 albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList());
66                         }
67                         return albums;
68                 }
69         };
70
71         /** Function that transforms an album into the images it contains. */
72         Function<Album, List<Image>> IMAGES = new Function<Album, List<Image>>() {
73
74                 @Override
75                 @Nonnull
76                 public List<Image> apply(Album album) {
77                         return (album != null) ? album.getImages() : Collections.<Image>emptyList();
78                 }
79         };
80
81         /**
82          * Filter that removes all albums that do not have any images in any album
83          * below it.
84          */
85         Predicate<Album> NOT_EMPTY = new Predicate<Album>() {
86
87                 @Override
88                 public boolean apply(Album album) {
89                         /* so, we flatten all albums below the given one and check whether at least one album… */
90                         return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate<Album>() {
91
92                                 @Override
93                                 public boolean apply(Album album) {
94                                         /* …contains any inserted images. */
95                                         return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate<Image>() {
96
97                                                 @Override
98                                                 public boolean apply(Image input) {
99                                                         return input.isInserted();
100                                                 }
101                                         });
102                                 }
103                         });
104                 }
105         };
106
107         /**
108          * Returns the ID of this album.
109          *
110          * @return The ID of this album
111          */
112         String getId();
113
114         /**
115          * Returns the Sone this album belongs to.
116          *
117          * @return The Sone this album belongs to
118          */
119         Sone getSone();
120
121         List<Album> getAlbums();
122
123         /**
124          * Removes an album from this album.
125          *
126          * @param album
127          *              The album to remove
128          */
129         void removeAlbum(Album album);
130
131         /**
132          * Moves the given album up in this album’s albums. If the album is already the
133          * first album, nothing happens.
134          *
135          * @param album
136          *              The album to move up
137          * @return The album that the given album swapped the place with, or
138          *         <code>null</code> if the album did not change its place
139          */
140         Album moveAlbumUp(Album album);
141
142         /**
143          * Moves the given album down in this album’s albums. If the album is already
144          * the last album, nothing happens.
145          *
146          * @param album
147          *              The album to move down
148          * @return The album that the given album swapped the place with, or
149          *         <code>null</code> if the album did not change its place
150          */
151         Album moveAlbumDown(Album album);
152
153         /**
154          * Returns the images in this album.
155          *
156          * @return The images in this album
157          */
158         List<Image> getImages();
159
160         /**
161          * Removes the given image from this album.
162          *
163          * @param image
164          *              The image to remove
165          */
166         void removeImage(Image image);
167
168         /**
169          * Moves the given image up in this album’s images. If the image is already the
170          * first image, nothing happens.
171          *
172          * @param image
173          *              The image to move up
174          * @return The image that the given image swapped the place with, or
175          *         <code>null</code> if the image did not change its place
176          */
177         Image moveImageUp(Image image);
178
179         /**
180          * Moves the given image down in this album’s images. If the image is already
181          * the last image, nothing happens.
182          *
183          * @param image
184          *              The image to move down
185          * @return The image that the given image swapped the place with, or
186          *         <code>null</code> if the image did not change its place
187          */
188         Image moveImageDown(Image image);
189
190         /**
191          * Returns the album image of this album, or {@code null} if no album image has
192          * been set.
193          *
194          * @return The image to show when this album is listed
195          */
196         Image getAlbumImage();
197
198         /**
199          * Returns whether this album contains any other albums or images.
200          *
201          * @return {@code true} if this album is empty, {@code false} otherwise
202          */
203         boolean isEmpty();
204
205         /**
206          * Returns whether this album is an identitiy’s root album.
207          *
208          * @return {@code true} if this album is an identity’s root album, {@code
209          *         false} otherwise
210          */
211         boolean isRoot();
212
213         /**
214          * Returns the parent album of this album.
215          *
216          * @return The parent album of this album, or {@code null} if this album does
217          *         not have a parent
218          */
219         Album getParent();
220
221         /**
222          * Sets the parent album of this album.
223          *
224          * @param parent
225          *              The new parent album of this album
226          * @return This album
227          */
228         Album setParent(Album parent);
229
230         /**
231          * Removes the parent album of this album.
232          *
233          * @return This album
234          */
235         Album removeParent();
236
237         /**
238          * Returns the title of this album.
239          *
240          * @return The title of this album
241          */
242         String getTitle();
243
244         /**
245          * Returns the description of this album.
246          *
247          * @return The description of this album
248          */
249         String getDescription();
250
251         AlbumBuilder newAlbumBuilder() throws IllegalStateException;
252
253         ImageBuilder newImageBuilder() throws IllegalStateException;
254
255         /**
256          * Returns a modifier for this album.
257          *
258          * @return A modifier for this album
259          * @throws IllegalStateException
260          *              if this album can not be modified
261          */
262         Modifier modify() throws IllegalStateException;
263
264         /**
265          * Allows modifying an album. Modifications are only performed once {@link
266          * #update()} has succesfully returned a new album with the modifications
267          * made.
268          *
269          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
270          */
271         interface Modifier {
272
273                 Modifier setTitle(String title);
274
275                 Modifier setDescription(String description);
276
277                 Modifier setAlbumImage(String imageId);
278
279                 Album update() throws IllegalStateException;
280
281         }
282
283 }