2 * Sone - DefaultImage.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;
23 import static com.google.common.base.Preconditions.checkState;
25 import net.pterodactylus.sone.data.Album;
26 import net.pterodactylus.sone.data.Image;
27 import net.pterodactylus.sone.data.Sone;
29 import com.google.common.base.Optional;
30 import com.google.common.hash.Hasher;
31 import com.google.common.hash.Hashing;
34 * Dumb, store-everything-in-memory implementation of an {@link Image}.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class DefaultImage implements Image {
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;
48 private String description;
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");
55 this.creationTime = creationTime;
61 public String getId() {
66 public Sone getSone() {
71 public Album getAlbum() {
76 public String getKey() {
81 public boolean isInserted() {
86 public long getCreationTime() {
91 public int getWidth() {
96 public int getHeight() {
101 public String getTitle() {
106 public String getDescription() {
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();
119 public Modifier setKey(String key) {
120 this.key = fromNullable(key);
125 public Modifier setTitle(String title) {
126 this.title = fromNullable(title);
131 public Modifier setDescription(String description) {
132 this.description = fromNullable(description);
137 public Image update() throws IllegalStateException {
138 checkState(!key.isPresent() || (DefaultImage.this.key == null), "key can not be changed");
140 if (key.isPresent()) {
141 DefaultImage.this.key = key.get();
143 if (title.isPresent()) {
144 DefaultImage.this.title = title.get();
146 if (description.isPresent()) {
147 DefaultImage.this.description = description.get();
150 return DefaultImage.this;
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(")");
163 return hash.hash().toString();
167 public int hashCode() {
168 return id.hashCode();
172 public boolean equals(Object object) {
173 if (!(object instanceof DefaultImage)) {
176 return ((DefaultImage) object).id.equals(id);