2 * Sone - AbstractAlbum.java - Copyright © 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.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.checkNotNull;
24 import net.pterodactylus.sone.data.Album;
25 import net.pterodactylus.sone.data.Image;
27 import com.google.common.base.Optional;
28 import com.google.common.hash.Hasher;
29 import com.google.common.hash.Hashing;
32 * Abstract {@link Album} implementation that contains only the attributes that
33 * are {@link String}s or primitives.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public abstract class AbstractAlbum implements Album {
39 protected final String id;
40 protected String title;
41 protected String description;
42 protected String albumImage;
44 protected AbstractAlbum(String id) {
45 this.id = checkNotNull(id, "id must not be null");
49 public String getId() {
54 public boolean isEmpty() {
55 return getAlbums().isEmpty() && getImages().isEmpty();
59 public boolean isRoot() {
60 return getParent() == null;
64 public String getTitle() {
69 public String getDescription() {
74 public Modifier modify() throws IllegalStateException {
75 // TODO: reenable check for local Sones
76 return new Modifier() {
77 private Optional<String> title = absent();
78 private Optional<String> description = absent();
79 private Optional<String> albumImage = absent();
82 public Modifier setTitle(String title) {
83 this.title = fromNullable(title);
88 public Modifier setDescription(String description) {
89 this.description = fromNullable(description);
94 public Modifier setAlbumImage(String imageId) {
95 this.albumImage = fromNullable(imageId);
100 public Album update() throws IllegalStateException {
101 if (title.isPresent()) {
102 AbstractAlbum.this.title = title.get();
104 if (description.isPresent()) {
105 AbstractAlbum.this.description = description.get();
107 if (albumImage.isPresent()) {
108 AbstractAlbum.this.albumImage = albumImage.get();
110 return AbstractAlbum.this;
116 public String getFingerprint() {
117 Hasher hash = Hashing.sha256().newHasher();
118 hash.putString("Album(");
119 hash.putString("ID(").putString(id).putString(")");
120 hash.putString("Title(").putString(title).putString(")");
121 hash.putString("Description(").putString(description).putString(")");
122 if (albumImage != null) {
123 hash.putString("AlbumImage(").putString(albumImage).putString(")");
126 /* add nested albums. */
127 hash.putString("Albums(");
128 for (Album album : getAlbums()) {
129 hash.putString(album.getFingerprint());
134 hash.putString("Images(");
135 for (Image image : getImages()) {
136 if (image.isInserted()) {
137 hash.putString(image.getFingerprint());
143 return hash.hash().toString();
147 public int hashCode() {
148 return id.hashCode();
152 public boolean equals(Object object) {
153 if (!(object instanceof DefaultAlbum)) {
156 DefaultAlbum album = (DefaultAlbum) object;
157 return id.equals(album.id);