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 final String parentId;
41 protected String title;
42 protected String description;
43 protected String albumImage;
45 protected AbstractAlbum(String id, String parentId) {
46 this.id = checkNotNull(id, "id must not be null");
47 this.parentId = parentId;
51 public String getId() {
56 public boolean isEmpty() {
57 return getAlbums().isEmpty() && getImages().isEmpty();
61 public boolean isRoot() {
62 return parentId == null;
66 public String getTitle() {
71 public String getDescription() {
76 public Modifier modify() throws IllegalStateException {
77 // TODO: reenable check for local Sones
78 return new Modifier() {
79 private Optional<String> title = absent();
80 private Optional<String> description = absent();
81 private Optional<String> albumImage = absent();
84 public Modifier setTitle(String title) {
85 this.title = fromNullable(title);
90 public Modifier setDescription(String description) {
91 this.description = fromNullable(description);
96 public Modifier setAlbumImage(String imageId) {
97 this.albumImage = fromNullable(imageId);
102 public Album update() throws IllegalStateException {
103 if (title.isPresent()) {
104 AbstractAlbum.this.title = title.get();
106 if (description.isPresent()) {
107 AbstractAlbum.this.description = description.get();
109 if (albumImage.isPresent()) {
110 AbstractAlbum.this.albumImage = albumImage.get();
112 return AbstractAlbum.this;
118 public String getFingerprint() {
119 Hasher hash = Hashing.sha256().newHasher();
120 hash.putString("Album(");
121 hash.putString("ID(").putString(id).putString(")");
122 hash.putString("Title(").putString(title).putString(")");
123 hash.putString("Description(").putString(description).putString(")");
124 if (albumImage != null) {
125 hash.putString("AlbumImage(").putString(albumImage).putString(")");
128 /* add nested albums. */
129 hash.putString("Albums(");
130 for (Album album : getAlbums()) {
131 hash.putString(album.getFingerprint());
136 hash.putString("Images(");
137 for (Image image : getImages()) {
138 if (image.isInserted()) {
139 hash.putString(image.getFingerprint());
145 return hash.hash().toString();
149 public int hashCode() {
150 return id.hashCode();
154 public boolean equals(Object object) {
155 if (!(object instanceof DefaultAlbum)) {
158 DefaultAlbum album = (DefaultAlbum) object;
159 return id.equals(album.id);