2 * Sone - AlbumImpl.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.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;
24 import static java.nio.charset.StandardCharsets.UTF_8;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.UUID;
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Sone;
36 import com.google.common.base.Function;
37 import com.google.common.base.Optional;
38 import com.google.common.base.Predicates;
39 import com.google.common.collect.Collections2;
40 import com.google.common.hash.Hasher;
41 import com.google.common.hash.Hashing;
44 * Container for images that can also contain nested {@link AlbumImpl}s.
46 public class AlbumImpl implements Album {
48 /** The ID of this album. */
49 private final String id;
51 /** The Sone this album belongs to. */
52 private final Sone sone;
55 private final List<Album> albums = new ArrayList<>();
57 /** The image IDs in order. */
58 private final List<String> imageIds = new ArrayList<>();
60 /** The images in this album. */
61 private final Map<String, Image> images = new HashMap<>();
63 /** The parent album. */
66 /** The title of this album. */
69 /** The description of this album. */
70 private String description;
72 /** Creates a new album with a random ID. */
73 public AlbumImpl(Sone sone) {
74 this(sone, UUID.randomUUID().toString());
78 * Creates a new album with the given ID.
83 public AlbumImpl(Sone sone, String id) {
84 this.sone = checkNotNull(sone, "Sone must not be null");
85 this.id = checkNotNull(id, "id must not be null");
93 public String getId() {
98 public Sone getSone() {
103 public List<Album> getAlbums() {
104 return new ArrayList<>(albums);
108 public void addAlbum(Album album) {
109 checkNotNull(album, "album must not be null");
110 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
111 album.setParent(this);
112 if (!albums.contains(album)) {
118 public void removeAlbum(Album album) {
119 checkNotNull(album, "album must not be null");
120 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
121 checkArgument(equals(album.getParent()), "album must belong to this album");
122 albums.remove(album);
123 album.removeParent();
127 public Album moveAlbumUp(Album album) {
128 checkNotNull(album, "album must not be null");
129 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
130 checkArgument(equals(album.getParent()), "album must belong to this album");
131 int oldIndex = albums.indexOf(album);
135 albums.remove(oldIndex);
136 albums.add(oldIndex - 1, album);
137 return albums.get(oldIndex);
141 public Album moveAlbumDown(Album album) {
142 checkNotNull(album, "album must not be null");
143 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
144 checkArgument(equals(album.getParent()), "album must belong to this album");
145 int oldIndex = albums.indexOf(album);
146 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
149 albums.remove(oldIndex);
150 albums.add(oldIndex + 1, album);
151 return albums.get(oldIndex);
155 public List<Image> getImages() {
156 return new ArrayList<>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
159 @SuppressWarnings("synthetic-access")
160 public Image apply(String imageId) {
161 return images.get(imageId);
163 }), Predicates.notNull()));
167 public void addImage(Image image) {
168 checkNotNull(image, "image must not be null");
169 checkNotNull(image.getSone(), "image must have an owner");
170 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
171 if (image.getAlbum() != null) {
172 image.getAlbum().removeImage(image);
174 image.setAlbum(this);
175 if (!imageIds.contains(image.getId())) {
176 imageIds.add(image.getId());
177 images.put(image.getId(), image);
182 public void removeImage(Image image) {
183 checkNotNull(image, "image must not be null");
184 checkNotNull(image.getSone(), "image must have an owner");
185 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
186 imageIds.remove(image.getId());
187 images.remove(image.getId());
191 public Image moveImageUp(Image image) {
192 checkNotNull(image, "image must not be null");
193 checkNotNull(image.getSone(), "image must have an owner");
194 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
195 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
196 int oldIndex = imageIds.indexOf(image.getId());
200 imageIds.remove(image.getId());
201 imageIds.add(oldIndex - 1, image.getId());
202 return images.get(imageIds.get(oldIndex));
206 public Image moveImageDown(Image image) {
207 checkNotNull(image, "image must not be null");
208 checkNotNull(image.getSone(), "image must have an owner");
209 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
210 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
211 int oldIndex = imageIds.indexOf(image.getId());
212 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
215 imageIds.remove(image.getId());
216 imageIds.add(oldIndex + 1, image.getId());
217 return images.get(imageIds.get(oldIndex));
221 public boolean isEmpty() {
222 return albums.isEmpty() && images.isEmpty();
226 public boolean isRoot() {
227 return parent == null;
231 public Album getParent() {
236 public Album setParent(Album parent) {
237 this.parent = checkNotNull(parent, "parent must not be null");
242 public Album removeParent() {
248 public String getTitle() {
253 public String getDescription() {
258 public Modifier modify() throws IllegalStateException {
259 // TODO: reenable check for local Sones
260 return new Modifier() {
261 private Optional<String> title = absent();
263 private Optional<String> description = absent();
266 public Modifier setTitle(String title) {
267 this.title = fromNullable(title);
272 public Modifier setDescription(String description) {
273 this.description = fromNullable(description);
278 public Album update() throws IllegalStateException {
279 if (title.isPresent() && title.get().trim().isEmpty()) {
280 throw new AlbumTitleMustNotBeEmpty();
282 if (title.isPresent()) {
283 AlbumImpl.this.title = title.get();
285 if (description.isPresent()) {
286 AlbumImpl.this.description = description.get();
288 return AlbumImpl.this;
294 // FINGERPRINTABLE METHODS
298 public String getFingerprint() {
299 Hasher hash = Hashing.sha256().newHasher();
300 hash.putString("Album(", UTF_8);
301 hash.putString("ID(", UTF_8).putString(id, UTF_8).putString(")", UTF_8);
302 hash.putString("Title(", UTF_8).putString(title, UTF_8).putString(")", UTF_8);
303 hash.putString("Description(", UTF_8).putString(description, UTF_8).putString(")", UTF_8);
305 /* add nested albums. */
306 hash.putString("Albums(", UTF_8);
307 for (Album album : albums) {
308 hash.putString(album.getFingerprint(), UTF_8);
310 hash.putString(")", UTF_8);
313 hash.putString("Images(", UTF_8);
314 for (Image image : getImages()) {
315 if (image.isInserted()) {
316 hash.putString(image.getFingerprint(), UTF_8);
319 hash.putString(")", UTF_8);
321 hash.putString(")", UTF_8);
322 return hash.hash().toString();
330 public int hashCode() {
331 return id.hashCode();
335 public boolean equals(Object object) {
336 if (!(object instanceof AlbumImpl)) {
339 AlbumImpl album = (AlbumImpl) object;
340 return id.equals(album.id);