Extract album baseclass that only stores the primitives.
[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 String title;
41         protected String description;
42         protected String albumImage;
43
44         protected AbstractAlbum(String id) {
45                 this.id = checkNotNull(id, "id must not be null");
46         }
47
48         @Override
49         public String getId() {
50                 return id;
51         }
52
53         @Override
54         public boolean isEmpty() {
55                 return getAlbums().isEmpty() && getImages().isEmpty();
56         }
57
58         @Override
59         public boolean isRoot() {
60                 return getParent() == null;
61         }
62
63         @Override
64         public String getTitle() {
65                 return title;
66         }
67
68         @Override
69         public String getDescription() {
70                 return description;
71         }
72
73         @Override
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();
80
81                         @Override
82                         public Modifier setTitle(String title) {
83                                 this.title = fromNullable(title);
84                                 return this;
85                         }
86
87                         @Override
88                         public Modifier setDescription(String description) {
89                                 this.description = fromNullable(description);
90                                 return this;
91                         }
92
93                         @Override
94                         public Modifier setAlbumImage(String imageId) {
95                                 this.albumImage = fromNullable(imageId);
96                                 return this;
97                         }
98
99                         @Override
100                         public Album update() throws IllegalStateException {
101                                 if (title.isPresent()) {
102                                         AbstractAlbum.this.title = title.get();
103                                 }
104                                 if (description.isPresent()) {
105                                         AbstractAlbum.this.description = description.get();
106                                 }
107                                 if (albumImage.isPresent()) {
108                                         AbstractAlbum.this.albumImage = albumImage.get();
109                                 }
110                                 return AbstractAlbum.this;
111                         }
112                 };
113         }
114
115         @Override
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(")");
124                 }
125
126                 /* add nested albums. */
127                 hash.putString("Albums(");
128                 for (Album album : getAlbums()) {
129                         hash.putString(album.getFingerprint());
130                 }
131                 hash.putString(")");
132
133                 /* add images. */
134                 hash.putString("Images(");
135                 for (Image image : getImages()) {
136                         if (image.isInserted()) {
137                                 hash.putString(image.getFingerprint());
138                         }
139                 }
140                 hash.putString(")");
141
142                 hash.putString(")");
143                 return hash.hash().toString();
144         }
145
146         @Override
147         public int hashCode() {
148                 return id.hashCode();
149         }
150
151         @Override
152         public boolean equals(Object object) {
153                 if (!(object instanceof DefaultAlbum)) {
154                         return false;
155                 }
156                 DefaultAlbum album = (DefaultAlbum) object;
157                 return id.equals(album.id);
158         }
159 }