0014a131fc5a53e3136bc026e721195e207be41e
[Sone.git] / src / main / java / net / pterodactylus / sone / data / ImageImpl.java
1 /*
2  * Sone - ImageImpl.java - Copyright © 2011–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 package net.pterodactylus.sone.data;
18
19 import static com.google.common.base.Optional.absent;
20 import static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
23
24 import java.util.UUID;
25
26 import com.google.common.base.Optional;
27 import com.google.common.hash.Hasher;
28 import com.google.common.hash.Hashing;
29
30 /**
31  * Container for image metadata.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class ImageImpl implements Image {
36
37         /** The ID of the image. */
38         private final String id;
39
40         /** The Sone the image belongs to. */
41         private final Sone sone;
42
43         /** The album this image belongs to. */
44         private Album album;
45
46         /** The request key of the image. */
47         private String key;
48
49         /** The creation time of the image. */
50         private final long creationTime;
51
52         /** The width of the image. */
53         private final int width;
54
55         /** The height of the image. */
56         private final int height;
57
58         /** The title of the image. */
59         private String title;
60
61         /** The description of the image. */
62         private String description;
63
64         /** Creates a new image with a random ID. */
65         public ImageImpl(Sone sone, long creationTime, String key, int width, int height) {
66                 this(UUID.randomUUID().toString(), sone, creationTime, key, width, height);
67         }
68
69         /**
70          * Creates a new image.
71          *
72          * @param id
73          *              The ID of the image
74          * @param creationTime
75          */
76         public ImageImpl(String id, Sone sone, long creationTime, String key, int width, int height) {
77                 this.id = checkNotNull(id, "id must not be null");
78                 this.sone = sone;
79                 this.creationTime = creationTime;
80                 this.key = key;
81                 this.width = width;
82                 this.height = height;
83         }
84
85         //
86         // ACCESSORS
87         //
88
89         @Override
90         public String getId() {
91                 return id;
92         }
93
94         @Override
95         public Sone getSone() {
96                 return sone;
97         }
98
99         @Override
100         public Album getAlbum() {
101                 return album;
102         }
103
104         @Override
105         public String getKey() {
106                 return key;
107         }
108
109         @Override
110         public boolean isInserted() {
111                 return key != null;
112         }
113
114         @Override
115         public long getCreationTime() {
116                 return creationTime;
117         }
118
119         @Override
120         public int getWidth() {
121                 return width;
122         }
123
124         @Override
125         public int getHeight() {
126                 return height;
127         }
128
129         @Override
130         public String getTitle() {
131                 return title;
132         }
133
134         @Override
135         public String getDescription() {
136                 return description;
137         }
138
139         public Modifier modify() throws IllegalStateException {
140                 // TODO: reenable check for local images
141                 return new Modifier() {
142                         private Optional<String> key = absent();
143                         private Optional<String> title = absent();
144                         private Optional<String> description = absent();
145
146                         @Override
147                         public Modifier setKey(String key) {
148                                 this.key = fromNullable(key);
149                                 return this;
150                         }
151
152                         @Override
153                         public Modifier setTitle(String title) {
154                                 this.title = fromNullable(title);
155                                 return this;
156                         }
157
158                         @Override
159                         public Modifier setDescription(String description) {
160                                 this.description = fromNullable(description);
161                                 return this;
162                         }
163
164                         @Override
165                         public Image update() throws IllegalStateException {
166                                 checkState(!key.isPresent() || (ImageImpl.this.key == null), "key can not be changed");
167
168                                 if (key.isPresent()) {
169                                         ImageImpl.this.key = key.get();
170                                 }
171                                 if (title.isPresent()) {
172                                         ImageImpl.this.title = title.get();
173                                 }
174                                 if (description.isPresent()) {
175                                         ImageImpl.this.description = description.get();
176                                 }
177
178                                 return ImageImpl.this;
179                         }
180                 };
181         }
182
183         //
184         // FINGERPRINTABLE METHODS
185         //
186
187         @Override
188         public String getFingerprint() {
189                 Hasher hash = Hashing.sha256().newHasher();
190                 hash.putString("Image(");
191                 hash.putString("ID(").putString(id).putString(")");
192                 hash.putString("Title(").putString(title).putString(")");
193                 hash.putString("Description(").putString(description).putString(")");
194                 hash.putString(")");
195                 return hash.hash().toString();
196         }
197
198         //
199         // OBJECT METHODS
200         //
201
202         /** {@inheritDoc} */
203         @Override
204         public int hashCode() {
205                 return id.hashCode();
206         }
207
208         /** {@inheritDoc} */
209         @Override
210         public boolean equals(Object object) {
211                 if (!(object instanceof ImageImpl)) {
212                         return false;
213                 }
214                 return ((ImageImpl) object).id.equals(id);
215         }
216
217 }