From eae5ceb147d3b6aafcb6d423a593d89e7847c2e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 12 Oct 2013 15:14:57 +0200 Subject: [PATCH] Extract another super class that does not hold anything but primitives. --- .../sone/data/impl/AbstractImage.java | 164 +++++++++++++++++++++ .../pterodactylus/sone/data/impl/DefaultImage.java | 133 +---------------- 2 files changed, 166 insertions(+), 131 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/data/impl/AbstractImage.java diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractImage.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractImage.java new file mode 100644 index 0000000..8b8d473 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractImage.java @@ -0,0 +1,164 @@ +/* + * Sone - AbstractImage.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data.impl; + +import static com.google.common.base.Optional.absent; +import static com.google.common.base.Optional.fromNullable; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + +import net.pterodactylus.sone.data.Image; + +import com.google.common.base.Optional; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; + +/** + * Abstract {@link Image} implementation that contains only the attributes that + * are {@link String}s or primitives. + * + * @author David ‘Bombe’ Roden + */ +public abstract class AbstractImage implements Image { + + protected final String id; + protected final long creationTime; + protected final int width; + protected final int height; + protected String key; + protected String title; + protected String description; + + public AbstractImage(String id, String key, long creationTime, int width, int height) { + this.id = checkNotNull(id, "id must not be null"); + this.key = key; + this.creationTime = creationTime; + this.width = width; + this.height = height; + } + + @Override + public String getId() { + return id; + } + + @Override + public String getKey() { + return key; + } + + @Override + public boolean isInserted() { + return key != null; + } + + @Override + public long getCreationTime() { + return creationTime; + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public String getTitle() { + return title; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public Modifier modify() throws IllegalStateException { + // TODO: reenable check for local images + return new Modifier() { + private Optional key = absent(); + private Optional title = absent(); + private Optional description = absent(); + + @Override + public Modifier setKey(String key) { + this.key = fromNullable(key); + return this; + } + + @Override + public Modifier setTitle(String title) { + this.title = fromNullable(title); + return this; + } + + @Override + public Modifier setDescription(String description) { + this.description = fromNullable(description); + return this; + } + + @Override + public Image update() throws IllegalStateException { + checkState(!key.isPresent() || (AbstractImage.this.key == null), "key can not be changed"); + + if (key.isPresent()) { + AbstractImage.this.key = key.get(); + } + if (title.isPresent()) { + AbstractImage.this.title = title.get(); + } + if (description.isPresent()) { + AbstractImage.this.description = description.get(); + } + + return AbstractImage.this; + } + }; + } + + @Override + public String getFingerprint() { + Hasher hash = Hashing.sha256().newHasher(); + hash.putString("Image("); + hash.putString("ID(").putString(id).putString(")"); + hash.putString("Title(").putString(title).putString(")"); + hash.putString("Description(").putString(description).putString(")"); + hash.putString(")"); + return hash.hash().toString(); + } + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof AbstractImage)) { + return false; + } + return ((AbstractImage) object).id.equals(id); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java index 13af287..085091a 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java @@ -17,49 +17,24 @@ package net.pterodactylus.sone.data.impl; -import static com.google.common.base.Optional.absent; -import static com.google.common.base.Optional.fromNullable; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; - import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; -import com.google.common.base.Optional; -import com.google.common.hash.Hasher; -import com.google.common.hash.Hashing; - /** * Dumb, store-everything-in-memory implementation of an {@link Image}. * * @author David ‘Bombe’ Roden */ -public class DefaultImage implements Image { +public class DefaultImage extends AbstractImage { - private final String id; private final Sone sone; private final Album album; - private final long creationTime; - private final int width; - private final int height; - private String key; - private String title; - private String description; public DefaultImage(String id, Sone sone, Album album, String key, long creationTime, int width, int height) { - this.id = checkNotNull(id, "id must not be null"); + super(id, key, creationTime, width, height); this.sone = sone; this.album = album; - this.key = key; - this.creationTime = creationTime; - this.width = width; - this.height = height; - } - - @Override - public String getId() { - return id; } @Override @@ -72,108 +47,4 @@ public class DefaultImage implements Image { return album; } - @Override - public String getKey() { - return key; - } - - @Override - public boolean isInserted() { - return key != null; - } - - @Override - public long getCreationTime() { - return creationTime; - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public String getTitle() { - return title; - } - - @Override - public String getDescription() { - return description; - } - - @Override - public Modifier modify() throws IllegalStateException { - // TODO: reenable check for local images - return new Modifier() { - private Optional key = absent(); - private Optional title = absent(); - private Optional description = absent(); - - @Override - public Modifier setKey(String key) { - this.key = fromNullable(key); - return this; - } - - @Override - public Modifier setTitle(String title) { - this.title = fromNullable(title); - return this; - } - - @Override - public Modifier setDescription(String description) { - this.description = fromNullable(description); - return this; - } - - @Override - public Image update() throws IllegalStateException { - checkState(!key.isPresent() || (DefaultImage.this.key == null), "key can not be changed"); - - if (key.isPresent()) { - DefaultImage.this.key = key.get(); - } - if (title.isPresent()) { - DefaultImage.this.title = title.get(); - } - if (description.isPresent()) { - DefaultImage.this.description = description.get(); - } - - return DefaultImage.this; - } - }; - } - - @Override - public String getFingerprint() { - Hasher hash = Hashing.sha256().newHasher(); - hash.putString("Image("); - hash.putString("ID(").putString(id).putString(")"); - hash.putString("Title(").putString(title).putString(")"); - hash.putString("Description(").putString(description).putString(")"); - hash.putString(")"); - return hash.hash().toString(); - } - - @Override - public int hashCode() { - return id.hashCode(); - } - - @Override - public boolean equals(Object object) { - if (!(object instanceof DefaultImage)) { - return false; - } - return ((DefaultImage) object).id.equals(id); - } - } -- 2.7.4