Store parent album ID in abstract album.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AbstractAlbum.java
1 /*
2  * Sone - AbstractAlbum.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data.impl;
19
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;
23
24 import net.pterodactylus.sone.data.Album;
25 import net.pterodactylus.sone.data.Image;
26
27 import com.google.common.base.Optional;
28 import com.google.common.hash.Hasher;
29 import com.google.common.hash.Hashing;
30
31 /**
32  * Abstract {@link Album} implementation that contains only the attributes that
33  * are {@link String}s or primitives.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public abstract class AbstractAlbum implements Album {
38
39         protected final String id;
40         protected final String parentId;
41         protected String title;
42         protected String description;
43         protected String albumImage;
44
45         protected AbstractAlbum(String id, String parentId) {
46                 this.id = checkNotNull(id, "id must not be null");
47                 this.parentId = parentId;
48         }
49
50         @Override
51         public String getId() {
52                 return id;
53         }
54
55         @Override
56         public boolean isEmpty() {
57                 return getAlbums().isEmpty() && getImages().isEmpty();
58         }
59
60         @Override
61         public boolean isRoot() {
62                 return parentId == null;
63         }
64
65         @Override
66         public String getTitle() {
67                 return title;
68         }
69
70         @Override
71         public String getDescription() {
72                 return description;
73         }
74
75         @Override
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();
82
83                         @Override
84                         public Modifier setTitle(String title) {
85                                 this.title = fromNullable(title);
86                                 return this;
87                         }
88
89                         @Override
90                         public Modifier setDescription(String description) {
91                                 this.description = fromNullable(description);
92                                 return this;
93                         }
94
95                         @Override
96                         public Modifier setAlbumImage(String imageId) {
97                                 this.albumImage = fromNullable(imageId);
98                                 return this;
99                         }
100
101                         @Override
102                         public Album update() throws IllegalStateException {
103                                 if (title.isPresent()) {
104                                         AbstractAlbum.this.title = title.get();
105                                 }
106                                 if (description.isPresent()) {
107                                         AbstractAlbum.this.description = description.get();
108                                 }
109                                 if (albumImage.isPresent()) {
110                                         AbstractAlbum.this.albumImage = albumImage.get();
111                                 }
112                                 return AbstractAlbum.this;
113                         }
114                 };
115         }
116
117         @Override
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(")");
126                 }
127
128                 /* add nested albums. */
129                 hash.putString("Albums(");
130                 for (Album album : getAlbums()) {
131                         hash.putString(album.getFingerprint());
132                 }
133                 hash.putString(")");
134
135                 /* add images. */
136                 hash.putString("Images(");
137                 for (Image image : getImages()) {
138                         if (image.isInserted()) {
139                                 hash.putString(image.getFingerprint());
140                         }
141                 }
142                 hash.putString(")");
143
144                 hash.putString(")");
145                 return hash.hash().toString();
146         }
147
148         @Override
149         public int hashCode() {
150                 return id.hashCode();
151         }
152
153         @Override
154         public boolean equals(Object object) {
155                 if (!(object instanceof DefaultAlbum)) {
156                         return false;
157                 }
158                 DefaultAlbum album = (DefaultAlbum) object;
159                 return id.equals(album.id);
160         }
161 }