X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FDefaultImage.java;h=445fa6bc861616590a9ef32179ee09172c9bee0a;hb=60d42fc47435de0b37710af49ad76ceb31dcfb9d;hp=13af2874868f0d9946551ef6c946bd018a055a0e;hpb=fc2165a3e44887c18d6faca054cc4efa384ca797;p=Sone.git
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..445fa6b 100644
--- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java
+++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java
@@ -1,5 +1,5 @@
/*
- * Sone - DefaultImage.java - Copyright © 2013 David Roden
+ * Sone - MemoryImage.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
@@ -17,49 +17,27 @@
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;
+import net.pterodactylus.sone.database.Database;
/**
- * Dumb, store-everything-in-memory implementation of an {@link Image}.
+ * {@link Image} implementation that uses a {@link Database}.
*
* @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;
+ private final Database database;
+ private final Sone sone; /* TODO - store sone ID only. */
+ private final String albumId;
- 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");
+ public DefaultImage(Database database, String id, Sone sone, String albumId, String key, long creationTime, int width, int height) {
+ super(id, key, creationTime, width, height);
+ this.database = database;
this.sone = sone;
- this.album = album;
- this.key = key;
- this.creationTime = creationTime;
- this.width = width;
- this.height = height;
- }
-
- @Override
- public String getId() {
- return id;
+ this.albumId = albumId;
}
@Override
@@ -69,111 +47,22 @@ public class DefaultImage implements Image {
@Override
public Album getAlbum() {
- 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;
- }
- };
+ return database.getAlbum(albumId).get();
}
@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();
+ public void moveUp() throws IllegalStateException {
+ database.moveUp(this);
}
@Override
- public int hashCode() {
- return id.hashCode();
+ public void moveDown() throws IllegalStateException {
+ database.moveDown(this);
}
@Override
- public boolean equals(Object object) {
- if (!(object instanceof DefaultImage)) {
- return false;
- }
- return ((DefaultImage) object).id.equals(id);
+ public void remove() throws IllegalStateException {
+ database.removeImage(this);
}
}