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 com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.UUID;
30 import com.google.common.base.Function;
31 import com.google.common.base.Optional;
32 import com.google.common.base.Predicates;
33 import com.google.common.collect.Collections2;
34 import com.google.common.hash.Hasher;
35 import com.google.common.hash.Hashing;
38 * Container for images that can also contain nested {@link AlbumImpl}s.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class AlbumImpl implements Album {
44 /** The ID of this album. */
45 private final String id;
47 /** The Sone this album belongs to. */
51 private final List<Album> albums = new ArrayList<Album>();
53 /** The image IDs in order. */
54 private final List<String> imageIds = new ArrayList<String>();
56 /** The images in this album. */
57 private final Map<String, Image> images = new HashMap<String, Image>();
59 /** The parent album. */
62 /** The title of this album. */
65 /** The description of this album. */
66 private String description;
68 /** The ID of the album picture. */
69 private String albumImage;
71 /** Creates a new album with a random ID. */
73 this(UUID.randomUUID().toString());
77 * Creates a new album with the given ID.
82 public AlbumImpl(String id) {
83 this.id = checkNotNull(id, "id must not be null");
91 public String getId() {
96 public Sone getSone() {
101 public Album setSone(Sone sone) {
102 checkNotNull(sone, "sone must not be null");
103 checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
109 public List<Album> getAlbums() {
110 return new ArrayList<Album>(albums);
114 public void addAlbum(Album album) {
115 checkNotNull(album, "album must not be null");
116 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
117 album.setParent(this);
118 if (!albums.contains(album)) {
124 public void removeAlbum(Album album) {
125 checkNotNull(album, "album must not be null");
126 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
127 checkArgument(equals(album.getParent()), "album must belong to this album");
128 albums.remove(album);
129 album.removeParent();
133 public Album moveAlbumUp(Album album) {
134 checkNotNull(album, "album must not be null");
135 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
136 checkArgument(equals(album.getParent()), "album must belong to this album");
137 int oldIndex = albums.indexOf(album);
141 albums.remove(oldIndex);
142 albums.add(oldIndex - 1, album);
143 return albums.get(oldIndex);
147 public Album moveAlbumDown(Album album) {
148 checkNotNull(album, "album must not be null");
149 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
150 checkArgument(equals(album.getParent()), "album must belong to this album");
151 int oldIndex = albums.indexOf(album);
152 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
155 albums.remove(oldIndex);
156 albums.add(oldIndex + 1, album);
157 return albums.get(oldIndex);
161 public List<Image> getImages() {
162 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
165 @SuppressWarnings("synthetic-access")
166 public Image apply(String imageId) {
167 return images.get(imageId);
169 }), Predicates.notNull()));
173 public void addImage(Image image) {
174 checkNotNull(image, "image must not be null");
175 checkNotNull(image.getSone(), "image must have an owner");
176 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
177 if (image.getAlbum() != null) {
178 image.getAlbum().removeImage(image);
180 image.setAlbum(this);
181 if (imageIds.isEmpty() && (albumImage == null)) {
182 albumImage = image.getId();
184 if (!imageIds.contains(image.getId())) {
185 imageIds.add(image.getId());
186 images.put(image.getId(), image);
191 public void removeImage(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 imageIds.remove(image.getId());
196 images.remove(image.getId());
197 if (image.getId().equals(albumImage)) {
198 if (images.isEmpty()) {
201 albumImage = images.values().iterator().next().getId();
207 public Image moveImageUp(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());
216 imageIds.remove(image.getId());
217 imageIds.add(oldIndex - 1, image.getId());
218 return images.get(imageIds.get(oldIndex));
222 public Image moveImageDown(Image image) {
223 checkNotNull(image, "image must not be null");
224 checkNotNull(image.getSone(), "image must have an owner");
225 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
226 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
227 int oldIndex = imageIds.indexOf(image.getId());
228 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
231 imageIds.remove(image.getId());
232 imageIds.add(oldIndex + 1, image.getId());
233 return images.get(imageIds.get(oldIndex));
237 public Image getAlbumImage() {
238 if (albumImage == null) {
241 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
245 public AlbumImpl setAlbumImage(String id) {
246 this.albumImage = id;
251 public boolean isEmpty() {
252 return albums.isEmpty() && images.isEmpty();
256 public boolean isRoot() {
257 return parent == null;
261 public Album getParent() {
266 public Album setParent(Album parent) {
267 this.parent = checkNotNull(parent, "parent must not be null");
272 public Album removeParent() {
278 public String getTitle() {
283 public Album setTitle(String title) {
284 this.title = checkNotNull(title, "title must not be null");
289 public String getDescription() {
294 public AlbumImpl setDescription(String description) {
295 this.description = checkNotNull(description, "description must not be null");
300 // FINGERPRINTABLE METHODS
304 public String getFingerprint() {
305 Hasher hash = Hashing.sha256().newHasher();
306 hash.putString("Album(");
307 hash.putString("ID(").putString(id).putString(")");
308 hash.putString("Title(").putString(title).putString(")");
309 hash.putString("Description(").putString(description).putString(")");
310 if (albumImage != null) {
311 hash.putString("AlbumImage(").putString(albumImage).putString(")");
314 /* add nested albums. */
315 hash.putString("Albums(");
316 for (Album album : albums) {
317 hash.putString(album.getFingerprint());
322 hash.putString("Images(");
323 for (Image image : getImages()) {
324 if (image.isInserted()) {
325 hash.putString(image.getFingerprint());
331 return hash.hash().toString();
339 public int hashCode() {
340 return id.hashCode();
344 public boolean equals(Object object) {
345 if (!(object instanceof AlbumImpl)) {
348 AlbumImpl album = (AlbumImpl) object;
349 return id.equals(album.id);