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 net.pterodactylus.sone.database.AlbumBuilder;
30 import net.pterodactylus.sone.database.ImageBuilder;
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;
38 * Container for images that can also contain nested {@link Album}s.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public interface Album extends Identified, Fingerprintable {
44 /** Compares two {@link Album}s by {@link #getTitle()}. */
45 Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
48 public int compare(Album leftAlbum, Album rightAlbum) {
49 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
53 /** Function that flattens the given album and all albums beneath it. */
54 Function<Album, List<Album>> FLATTENER = new Function<Album, List<Album>>() {
58 public List<Album> apply(Album album) {
62 List<Album> albums = new ArrayList<Album>();
64 for (Album subAlbum : album.getAlbums()) {
65 albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList());
71 /** Function that transforms an album into the images it contains. */
72 Function<Album, List<Image>> IMAGES = new Function<Album, List<Image>>() {
76 public List<Image> apply(Album album) {
77 return (album != null) ? album.getImages() : Collections.<Image>emptyList();
82 * Filter that removes all albums that do not have any images in any album
85 Predicate<Album> NOT_EMPTY = new Predicate<Album>() {
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>() {
93 public boolean apply(Album album) {
94 /* …contains any inserted images. */
95 return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate<Image>() {
98 public boolean apply(Image input) {
99 return input.isInserted();
108 * Returns the ID of this album.
110 * @return The ID of this album
115 * Returns the Sone this album belongs to.
117 * @return The Sone this album belongs to
121 List<Album> getAlbums();
124 * Returns the images in this album.
126 * @return The images in this album
128 List<Image> getImages();
131 * Moves the given image up in this album’s images. If the image is already the
132 * first image, nothing happens.
135 * The image to move up
136 * @return The image that the given image swapped the place with, or
137 * <code>null</code> if the image did not change its place
139 Image moveImageUp(Image image);
142 * Moves the given image down in this album’s images. If the image is already
143 * the last image, nothing happens.
146 * The image to move down
147 * @return The image that the given image swapped the place with, or
148 * <code>null</code> if the image did not change its place
150 Image moveImageDown(Image image);
153 * Returns the album image of this album, or {@code null} if no album image has
156 * @return The image to show when this album is listed
158 Image getAlbumImage();
161 * Returns whether this album contains any other albums or images.
163 * @return {@code true} if this album is empty, {@code false} otherwise
168 * Returns whether this album is an identitiy’s root album.
170 * @return {@code true} if this album is an identity’s root album, {@code
176 * Returns the parent album of this album.
178 * @return The parent album of this album, or {@code null} if this album does
184 * Returns the title of this album.
186 * @return The title of this album
191 * Returns the description of this album.
193 * @return The description of this album
195 String getDescription();
197 AlbumBuilder newAlbumBuilder() throws IllegalStateException;
199 ImageBuilder newImageBuilder() throws IllegalStateException;
202 * Returns a modifier for this album.
204 * @return A modifier for this album
205 * @throws IllegalStateException
206 * if this album can not be modified
208 Modifier modify() throws IllegalStateException;
214 void remove() throws IllegalStateException;
217 * Allows modifying an album. Modifications are only performed once {@link
218 * #update()} has succesfully returned a new album with the modifications
221 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
225 Modifier setTitle(String title);
227 Modifier setDescription(String description);
229 Modifier setAlbumImage(String imageId);
231 Album update() throws IllegalStateException;