2 * Sone - Album.java - Copyright © 2011–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
20 import static java.util.Arrays.asList;
21 import static java.util.Collections.emptyList;
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;
29 import com.google.common.base.Function;
30 import com.google.common.base.Predicate;
31 import com.google.common.collect.FluentIterable;
32 import com.google.common.collect.ImmutableList;
35 * Container for images that can also contain nested {@link Album}s.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public interface Album extends Identified, Fingerprintable {
41 /** Compares two {@link Album}s by {@link #getTitle()}. */
42 Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
45 public int compare(Album leftAlbum, Album rightAlbum) {
46 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
50 /** Function that flattens the given album and all albums beneath it. */
51 Function<Album, List<Album>> FLATTENER = new Function<Album, List<Album>>() {
55 public List<Album> apply(Album album) {
59 List<Album> albums = new ArrayList<Album>();
61 for (Album subAlbum : album.getAlbums()) {
62 albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList());
68 /** Function that transforms an album into the images it contains. */
69 Function<Album, List<Image>> IMAGES = new Function<Album, List<Image>>() {
73 public List<Image> apply(Album album) {
74 return (album != null) ? album.getImages() : Collections.<Image>emptyList();
79 * Filter that removes all albums that do not have any images in any album
82 Predicate<Album> NOT_EMPTY = new Predicate<Album>() {
85 public boolean apply(Album album) {
86 /* so, we flatten all albums below the given one and check whether at least one album… */
87 return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate<Album>() {
90 public boolean apply(Album album) {
91 /* …contains any inserted images. */
92 return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate<Image>() {
95 public boolean apply(Image input) {
96 return input.isInserted();
105 * Returns the ID of this album.
107 * @return The ID of this album
112 * Returns the Sone this album belongs to.
114 * @return The Sone this album belongs to
119 * Sets the owner of the album. The owner can only be set as long as the
120 * current owner is {@code null}.
126 Album setSone(Sone sone);
129 * Returns the nested albums.
131 * @return The nested albums
133 List<Album> getAlbums();
136 * Adds an album to this album.
141 void addAlbum(Album album);
144 * Removes an album from this album.
147 * The album to remove
149 void removeAlbum(Album album);
152 * Moves the given album up in this album’s albums. If the album is already the
153 * first album, nothing happens.
156 * The album to move up
157 * @return The album that the given album swapped the place with, or
158 * <code>null</code> if the album did not change its place
160 Album moveAlbumUp(Album album);
163 * Moves the given album down in this album’s albums. If the album is already
164 * the last album, nothing happens.
167 * The album to move down
168 * @return The album that the given album swapped the place with, or
169 * <code>null</code> if the album did not change its place
171 Album moveAlbumDown(Album album);
174 * Returns the images in this album.
176 * @return The images in this album
178 List<Image> getImages();
181 * Adds the given image to this album.
186 void addImage(Image image);
189 * Removes the given image from this album.
192 * The image to remove
194 void removeImage(Image image);
197 * Moves the given image up in this album’s images. If the image is already the
198 * first image, nothing happens.
201 * The image to move up
202 * @return The image that the given image swapped the place with, or
203 * <code>null</code> if the image did not change its place
205 Image moveImageUp(Image image);
208 * Moves the given image down in this album’s images. If the image is already
209 * the last image, nothing happens.
212 * The image to move down
213 * @return The image that the given image swapped the place with, or
214 * <code>null</code> if the image did not change its place
216 Image moveImageDown(Image image);
219 * Returns the album image of this album, or {@code null} if no album image has
222 * @return The image to show when this album is listed
224 Image getAlbumImage();
227 * Sets the ID of the album image.
230 * The ID of the album image
233 Album setAlbumImage(String id);
236 * Returns whether this album contains any other albums or images.
238 * @return {@code true} if this album is empty, {@code false} otherwise
243 * Returns whether this album is an identitiy’s root album.
245 * @return {@code true} if this album is an identity’s root album, {@code
251 * Returns the parent album of this album.
253 * @return The parent album of this album, or {@code null} if this album does
259 * Sets the parent album of this album.
262 * The new parent album of this album
265 Album setParent(Album parent);
268 * Removes the parent album of this album.
272 Album removeParent();
275 * Returns the title of this album.
277 * @return The title of this album
282 * Sets the title of this album.
285 * The title of this album
288 Album setTitle(String title);
291 * Returns the description of this album.
293 * @return The description of this album
295 String getDescription();
298 * Sets the description of this album.
301 * The description of this album
304 Album setDescription(String description);