Extract default Image implementation as base for all future image implementations.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultImage.java
1 /*
2  * Sone - DefaultImage.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 import static com.google.common.base.Preconditions.checkState;
24
25 import net.pterodactylus.sone.data.Album;
26 import net.pterodactylus.sone.data.Image;
27 import net.pterodactylus.sone.data.Sone;
28
29 import com.google.common.base.Optional;
30 import com.google.common.hash.Hasher;
31 import com.google.common.hash.Hashing;
32
33 /**
34  * Dumb, store-everything-in-memory implementation of an {@link Image}.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class DefaultImage implements Image {
39
40         private final String id;
41         private final Sone sone;
42         private final Album album;
43         private final long creationTime;
44         private final int width;
45         private final int height;
46         private String key;
47         private String title;
48         private String description;
49
50         public DefaultImage(String id, Sone sone, Album album, String key, long creationTime, int width, int height) {
51                 this.id = checkNotNull(id, "id must not be null");
52                 this.sone = sone;
53                 this.album = album;
54                 this.key = key;
55                 this.creationTime = creationTime;
56                 this.width = width;
57                 this.height = height;
58         }
59
60         @Override
61         public String getId() {
62                 return id;
63         }
64
65         @Override
66         public Sone getSone() {
67                 return sone;
68         }
69
70         @Override
71         public Album getAlbum() {
72                 return album;
73         }
74
75         @Override
76         public String getKey() {
77                 return key;
78         }
79
80         @Override
81         public boolean isInserted() {
82                 return key != null;
83         }
84
85         @Override
86         public long getCreationTime() {
87                 return creationTime;
88         }
89
90         @Override
91         public int getWidth() {
92                 return width;
93         }
94
95         @Override
96         public int getHeight() {
97                 return height;
98         }
99
100         @Override
101         public String getTitle() {
102                 return title;
103         }
104
105         @Override
106         public String getDescription() {
107                 return description;
108         }
109
110         @Override
111         public Modifier modify() throws IllegalStateException {
112                 // TODO: reenable check for local images
113                 return new Modifier() {
114                         private Optional<String> key = absent();
115                         private Optional<String> title = absent();
116                         private Optional<String> description = absent();
117
118                         @Override
119                         public Modifier setKey(String key) {
120                                 this.key = fromNullable(key);
121                                 return this;
122                         }
123
124                         @Override
125                         public Modifier setTitle(String title) {
126                                 this.title = fromNullable(title);
127                                 return this;
128                         }
129
130                         @Override
131                         public Modifier setDescription(String description) {
132                                 this.description = fromNullable(description);
133                                 return this;
134                         }
135
136                         @Override
137                         public Image update() throws IllegalStateException {
138                                 checkState(!key.isPresent() || (DefaultImage.this.key == null), "key can not be changed");
139
140                                 if (key.isPresent()) {
141                                         DefaultImage.this.key = key.get();
142                                 }
143                                 if (title.isPresent()) {
144                                         DefaultImage.this.title = title.get();
145                                 }
146                                 if (description.isPresent()) {
147                                         DefaultImage.this.description = description.get();
148                                 }
149
150                                 return DefaultImage.this;
151                         }
152                 };
153         }
154
155         @Override
156         public String getFingerprint() {
157                 Hasher hash = Hashing.sha256().newHasher();
158                 hash.putString("Image(");
159                 hash.putString("ID(").putString(id).putString(")");
160                 hash.putString("Title(").putString(title).putString(")");
161                 hash.putString("Description(").putString(description).putString(")");
162                 hash.putString(")");
163                 return hash.hash().toString();
164         }
165
166         @Override
167         public int hashCode() {
168                 return id.hashCode();
169         }
170
171         @Override
172         public boolean equals(Object object) {
173                 if (!(object instanceof DefaultImage)) {
174                         return false;
175                 }
176                 return ((DefaultImage) object).id.equals(id);
177         }
178
179 }