2 * Sone - AbstractImage.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.Image;
27 import com.google.common.base.Optional;
28 import com.google.common.hash.Hasher;
29 import com.google.common.hash.Hashing;
32 * Abstract {@link Image} implementation that contains only the attributes that
33 * are {@link String}s or primitives.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public abstract class AbstractImage implements Image {
39 protected final String id;
40 protected final long creationTime;
41 protected final int width;
42 protected final int height;
44 protected String title;
45 protected String description;
47 public AbstractImage(String id, String key, long creationTime, int width, int height) {
48 this.id = checkNotNull(id, "id must not be null");
50 this.creationTime = creationTime;
56 public String getId() {
61 public String getKey() {
66 public boolean isInserted() {
71 public long getCreationTime() {
76 public int getWidth() {
81 public int getHeight() {
86 public String getTitle() {
91 public String getDescription() {
96 public Modifier modify() throws IllegalStateException {
97 // TODO: reenable check for local images
98 return new Modifier() {
99 private Optional<String> key = absent();
100 private Optional<String> title = absent();
101 private Optional<String> description = absent();
104 public Modifier setKey(String key) {
105 this.key = fromNullable(key);
110 public Modifier setTitle(String title) {
111 this.title = fromNullable(title);
116 public Modifier setDescription(String description) {
117 this.description = fromNullable(description);
122 public Image update() throws IllegalStateException {
123 checkState(!key.isPresent() || (AbstractImage.this.key == null), "key can not be changed");
125 if (key.isPresent()) {
126 AbstractImage.this.key = key.get();
128 if (title.isPresent()) {
129 AbstractImage.this.title = title.get();
131 if (description.isPresent()) {
132 AbstractImage.this.description = description.get();
135 return AbstractImage.this;
141 public String getFingerprint() {
142 Hasher hash = Hashing.sha256().newHasher();
143 hash.putString("Image(");
144 hash.putString("ID(").putString(id).putString(")");
145 hash.putString("Title(").putString(title).putString(")");
146 hash.putString("Description(").putString(description).putString(")");
148 return hash.hash().toString();
152 public int hashCode() {
153 return id.hashCode();
157 public boolean equals(Object object) {
158 if (!(object instanceof AbstractImage)) {
161 return ((AbstractImage) object).id.equals(id);