2 * Sone - AlbumImpl.java - Copyright © 2011–2016 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.impl;
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;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.UUID;
31 import net.pterodactylus.sone.data.Album;
32 import net.pterodactylus.sone.data.Image;
33 import net.pterodactylus.sone.data.Sone;
35 import com.google.common.base.Function;
36 import com.google.common.base.Optional;
37 import com.google.common.base.Predicates;
38 import com.google.common.collect.Collections2;
39 import com.google.common.hash.Hasher;
40 import com.google.common.hash.Hashing;
43 * Container for images that can also contain nested {@link AlbumImpl}s.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class AlbumImpl implements Album {
49 /** The ID of this album. */
50 private final String id;
52 /** The Sone this album belongs to. */
53 private final Sone sone;
56 private final List<Album> albums = new ArrayList<Album>();
58 /** The image IDs in order. */
59 private final List<String> imageIds = new ArrayList<String>();
61 /** The images in this album. */
62 private final Map<String, Image> images = new HashMap<String, Image>();
64 /** The parent album. */
67 /** The title of this album. */
70 /** The description of this album. */
71 private String description;
73 /** Creates a new album with a random ID. */
74 public AlbumImpl(Sone sone) {
75 this(sone, UUID.randomUUID().toString());
79 * Creates a new album with the given ID.
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");
94 public String getId() {
99 public Sone getSone() {
104 public List<Album> getAlbums() {
105 return new ArrayList<Album>(albums);
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)) {
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();
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);
136 albums.remove(oldIndex);
137 albums.add(oldIndex - 1, album);
138 return albums.get(oldIndex);
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))) {
150 albums.remove(oldIndex);
151 albums.add(oldIndex + 1, album);
152 return albums.get(oldIndex);
156 public List<Image> getImages() {
157 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
160 @SuppressWarnings("synthetic-access")
161 public Image apply(String imageId) {
162 return images.get(imageId);
164 }), Predicates.notNull()));
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);
175 image.setAlbum(this);
176 if (!imageIds.contains(image.getId())) {
177 imageIds.add(image.getId());
178 images.put(image.getId(), image);
183 public void removeImage(Image image) {
184 checkNotNull(image, "image must not be null");
185 checkNotNull(image.getSone(), "image must have an owner");
186 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
187 imageIds.remove(image.getId());
188 images.remove(image.getId());
192 public Image moveImageUp(Image image) {
193 checkNotNull(image, "image must not be null");
194 checkNotNull(image.getSone(), "image must have an owner");
195 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
196 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
197 int oldIndex = imageIds.indexOf(image.getId());
201 imageIds.remove(image.getId());
202 imageIds.add(oldIndex - 1, image.getId());
203 return images.get(imageIds.get(oldIndex));
207 public Image moveImageDown(Image image) {
208 checkNotNull(image, "image must not be null");
209 checkNotNull(image.getSone(), "image must have an owner");
210 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
211 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
212 int oldIndex = imageIds.indexOf(image.getId());
213 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
216 imageIds.remove(image.getId());
217 imageIds.add(oldIndex + 1, image.getId());
218 return images.get(imageIds.get(oldIndex));
222 public boolean isEmpty() {
223 return albums.isEmpty() && images.isEmpty();
227 public boolean isRoot() {
228 return parent == null;
232 public Album getParent() {
237 public Album setParent(Album parent) {
238 this.parent = checkNotNull(parent, "parent must not be null");
243 public Album removeParent() {
249 public String getTitle() {
254 public String getDescription() {
259 public Modifier modify() throws IllegalStateException {
260 // TODO: reenable check for local Sones
261 return new Modifier() {
262 private Optional<String> title = absent();
264 private Optional<String> description = absent();
267 public Modifier setTitle(String title) {
268 this.title = fromNullable(title);
273 public Modifier setDescription(String description) {
274 this.description = fromNullable(description);
279 public Album update() throws IllegalStateException {
280 if (title.isPresent() && title.get().trim().isEmpty()) {
281 throw new AlbumTitleMustNotBeEmpty();
283 if (title.isPresent()) {
284 AlbumImpl.this.title = title.get();
286 if (description.isPresent()) {
287 AlbumImpl.this.description = description.get();
289 return AlbumImpl.this;
295 // FINGERPRINTABLE METHODS
299 public String getFingerprint() {
300 Hasher hash = Hashing.sha256().newHasher();
301 hash.putString("Album(");
302 hash.putString("ID(").putString(id).putString(")");
303 hash.putString("Title(").putString(title).putString(")");
304 hash.putString("Description(").putString(description).putString(")");
306 /* add nested albums. */
307 hash.putString("Albums(");
308 for (Album album : albums) {
309 hash.putString(album.getFingerprint());
314 hash.putString("Images(");
315 for (Image image : getImages()) {
316 if (image.isInserted()) {
317 hash.putString(image.getFingerprint());
323 return hash.hash().toString();
331 public int hashCode() {
332 return id.hashCode();
336 public boolean equals(Object object) {
337 if (!(object instanceof AlbumImpl)) {
340 AlbumImpl album = (AlbumImpl) object;
341 return id.equals(album.id);