2 * Sone - Album.java - Copyright © 2011–2019 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.List;
26 import javax.annotation.Nonnull;
28 import com.google.common.base.Function;
29 import com.google.common.base.Predicate;
30 import com.google.common.collect.FluentIterable;
31 import com.google.common.collect.ImmutableList;
34 * Container for images that can also contain nested {@link Album}s.
36 public interface Album extends Identified, Fingerprintable {
38 /** Function that flattens the given album and all albums beneath it. */
39 Function<Album, List<Album>> FLATTENER = new Function<Album, List<Album>>() {
43 public List<Album> apply(Album album) {
47 List<Album> albums = new ArrayList<>();
49 for (Album subAlbum : album.getAlbums()) {
50 albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList());
56 /** Function that transforms an album into the images it contains. */
57 Function<Album, List<Image>> IMAGES = new Function<Album, List<Image>>() {
61 public List<Image> apply(Album album) {
62 return (album != null) ? album.getImages() : Collections.<Image>emptyList();
67 * Filter that removes all albums that do not have any images in any album
70 Predicate<Album> NOT_EMPTY = new Predicate<Album>() {
73 public boolean apply(Album album) {
74 /* so, we flatten all albums below the given one and check whether at least one album… */
75 return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate<Album>() {
78 public boolean apply(Album album) {
79 /* …contains any inserted images. */
80 return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate<Image>() {
83 public boolean apply(Image input) {
84 return input.isInserted();
93 * Returns the ID of this album.
95 * @return The ID of this album
100 * Returns the Sone this album belongs to.
102 * @return The Sone this album belongs to
107 * Returns the nested albums.
109 * @return The nested albums
111 List<Album> getAlbums();
114 * Adds an album to this album.
119 void addAlbum(Album album);
122 * Removes an album from this album.
125 * The album to remove
127 void removeAlbum(Album album);
130 * Moves the given album up in this album’s albums. If the album is already the
131 * first album, nothing happens.
134 * The album to move up
135 * @return The album that the given album swapped the place with, or
136 * <code>null</code> if the album did not change its place
138 Album moveAlbumUp(Album album);
141 * Moves the given album down in this album’s albums. If the album is already
142 * the last album, nothing happens.
145 * The album to move down
146 * @return The album that the given album swapped the place with, or
147 * <code>null</code> if the album did not change its place
149 Album moveAlbumDown(Album album);
152 * Returns the images in this album.
154 * @return The images in this album
156 List<Image> getImages();
159 * Adds the given image to this album.
164 void addImage(Image image);
167 * Removes the given image from this album.
170 * The image to remove
172 void removeImage(Image image);
175 * Moves the given image up in this album’s images. If the image is already the
176 * first image, nothing happens.
179 * The image to move up
180 * @return The image that the given image swapped the place with, or
181 * <code>null</code> if the image did not change its place
183 Image moveImageUp(Image image);
186 * Moves the given image down in this album’s images. If the image is already
187 * the last image, nothing happens.
190 * The image to move down
191 * @return The image that the given image swapped the place with, or
192 * <code>null</code> if the image did not change its place
194 Image moveImageDown(Image image);
197 * Returns whether this album contains any other albums or images.
199 * @return {@code true} if this album is empty, {@code false} otherwise
204 * Returns whether this album is an identitiy’s root album.
206 * @return {@code true} if this album is an identity’s root album, {@code
212 * Returns the parent album of this album.
214 * @return The parent album of this album, or {@code null} if this album does
220 * Sets the parent album of this album.
223 * The new parent album of this album
226 Album setParent(Album parent);
229 * Removes the parent album of this album.
233 Album removeParent();
236 * Returns the title of this album.
238 * @return The title of this album
243 * Returns the description of this album.
245 * @return The description of this album
247 String getDescription();
250 * Returns a modifier for this album.
252 * @return A modifier for this album
253 * @throws IllegalStateException
254 * if this album can not be modified
256 Modifier modify() throws IllegalStateException;
259 * Allows modifying an album. Modifications are only performed once {@link
260 * #update()} has succesfully returned a new album with the modifications
265 Modifier setTitle(String title);
267 Modifier setDescription(String description);
269 Album update() throws IllegalStateException;
271 class AlbumTitleMustNotBeEmpty extends IllegalStateException { }