Extract default Image implementation as base for all future image implementations.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 12 Oct 2013 12:03:59 +0000 (14:03 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:17:42 +0000 (22:17 +0100)
src/main/java/net/pterodactylus/sone/data/AlbumImpl.java
src/main/java/net/pterodactylus/sone/data/ImageImpl.java
src/main/java/net/pterodactylus/sone/data/impl/AbstractImageBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/data/impl/ImageBuilderImpl.java
src/main/java/net/pterodactylus/sone/database/ImageDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/test/java/net/pterodactylus/sone/data/impl/ImageBuilderImplTest.java

index 0f6ce81..5ee0c23 100644 (file)
@@ -267,7 +267,7 @@ public class AlbumImpl implements Album {
 
        @Override
        public ImageBuilder newImageBuilder() throws IllegalStateException {
-               return new ImageBuilderImpl() {
+               return new ImageBuilderImpl(this) {
                        @Override
                        public Image build() throws IllegalStateException {
                                Image image = super.build();
index 0014a13..47842ec 100644 (file)
  */
 package net.pterodactylus.sone.data;
 
-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 java.util.UUID;
-
-import com.google.common.base.Optional;
-import com.google.common.hash.Hasher;
-import com.google.common.hash.Hashing;
+import net.pterodactylus.sone.data.impl.DefaultImage;
 
 /**
  * Container for image metadata.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class ImageImpl implements Image {
-
-       /** The ID of the image. */
-       private final String id;
-
-       /** The Sone the image belongs to. */
-       private final Sone sone;
-
-       /** The album this image belongs to. */
-       private Album album;
-
-       /** The request key of the image. */
-       private String key;
-
-       /** The creation time of the image. */
-       private final long creationTime;
-
-       /** The width of the image. */
-       private final int width;
-
-       /** The height of the image. */
-       private final int height;
-
-       /** The title of the image. */
-       private String title;
-
-       /** The description of the image. */
-       private String description;
-
-       /** Creates a new image with a random ID. */
-       public ImageImpl(Sone sone, long creationTime, String key, int width, int height) {
-               this(UUID.randomUUID().toString(), sone, creationTime, key, width, height);
-       }
-
-       /**
-        * Creates a new image.
-        *
-        * @param id
-        *              The ID of the image
-        * @param creationTime
-        */
-       public ImageImpl(String id, Sone sone, long creationTime, String key, int width, int height) {
-               this.id = checkNotNull(id, "id must not be null");
-               this.sone = sone;
-               this.creationTime = creationTime;
-               this.key = key;
-               this.width = width;
-               this.height = height;
-       }
-
-       //
-       // ACCESSORS
-       //
-
-       @Override
-       public String getId() {
-               return id;
-       }
-
-       @Override
-       public Sone getSone() {
-               return sone;
-       }
-
-       @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;
-       }
-
-       public Modifier modify() throws IllegalStateException {
-               // TODO: reenable check for local images
-               return new Modifier() {
-                       private Optional<String> key = absent();
-                       private Optional<String> title = absent();
-                       private Optional<String> 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() || (ImageImpl.this.key == null), "key can not be changed");
-
-                               if (key.isPresent()) {
-                                       ImageImpl.this.key = key.get();
-                               }
-                               if (title.isPresent()) {
-                                       ImageImpl.this.title = title.get();
-                               }
-                               if (description.isPresent()) {
-                                       ImageImpl.this.description = description.get();
-                               }
-
-                               return ImageImpl.this;
-                       }
-               };
-       }
-
-       //
-       // FINGERPRINTABLE METHODS
-       //
-
-       @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();
-       }
-
-       //
-       // OBJECT METHODS
-       //
-
-       /** {@inheritDoc} */
-       @Override
-       public int hashCode() {
-               return id.hashCode();
-       }
+public class ImageImpl extends DefaultImage {
 
-       /** {@inheritDoc} */
-       @Override
-       public boolean equals(Object object) {
-               if (!(object instanceof ImageImpl)) {
-                       return false;
-               }
-               return ((ImageImpl) object).id.equals(id);
+       public ImageImpl(String id, Sone sone, Album album, String key, long creationTime, int width, int height) {
+               super(id, sone, album, key, creationTime, width, height);
        }
 
 }
index 1b9e3a8..209ef0f 100644 (file)
  */
 package net.pterodactylus.sone.data.impl;
 
+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.Sone;
 import net.pterodactylus.sone.database.ImageBuilder;
 
@@ -29,6 +31,8 @@ import net.pterodactylus.sone.database.ImageBuilder;
  */
 public abstract class AbstractImageBuilder implements ImageBuilder {
 
+       protected final Album album;
+
        /** Whether to create an album with a random ID. */
        protected boolean randomId;
 
@@ -41,6 +45,10 @@ public abstract class AbstractImageBuilder implements ImageBuilder {
        protected int width;
        protected int height;
 
+       public AbstractImageBuilder(Album album) {
+               this.album = checkNotNull(album, "album must not be null");
+       }
+
        @Override
        public ImageBuilder randomId() {
                randomId = true;
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java
new file mode 100644 (file)
index 0000000..13af287
--- /dev/null
@@ -0,0 +1,179 @@
+/*
+ * Sone - DefaultImage.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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultImage implements Image {
+
+       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");
+               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
+       public Sone getSone() {
+               return sone;
+       }
+
+       @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<String> key = absent();
+                       private Optional<String> title = absent();
+                       private Optional<String> 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);
+       }
+
+}
index 83e1dbc..8135736 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.data.impl;
 
 import static java.util.UUID.randomUUID;
 
+import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.ImageImpl;
 import net.pterodactylus.sone.database.ImageBuilder;
@@ -30,12 +31,16 @@ import net.pterodactylus.sone.database.ImageBuilder;
  */
 public class ImageBuilderImpl extends AbstractImageBuilder {
 
+       public ImageBuilderImpl(Album album) {
+               super(album);
+       }
+
        @Override
        public Image build() throws IllegalStateException {
                validate();
                String id = randomId ? randomUUID().toString() : this.id;
                long creationTime = createdNow ? System.currentTimeMillis() : this.creationTime;
-               return new ImageImpl(id, sone, creationTime, key, width, height);
+               return new ImageImpl(id, sone, album, key, creationTime, width, height);
        }
 
 }
index b5e436d..e1aa28c 100644 (file)
@@ -23,7 +23,7 @@ package net.pterodactylus.sone.database;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public interface ImageDatabase extends ImageProvider, ImageBuilderFactory, ImageStore {
+public interface ImageDatabase extends ImageProvider, ImageStore {
 
        /* nothing here. */
 
index 8b25954..dc827f0 100644 (file)
@@ -41,11 +41,9 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.impl.AlbumBuilderImpl;
-import net.pterodactylus.sone.data.impl.ImageBuilderImpl;
 import net.pterodactylus.sone.database.AlbumBuilder;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.DatabaseException;
-import net.pterodactylus.sone.database.ImageBuilder;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostDatabase;
 import net.pterodactylus.sone.database.PostReplyBuilder;
@@ -484,15 +482,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        //
-       // IMAGEBUILDERFACTORY METHODS
-       //
-
-       @Override
-       public ImageBuilder newImageBuilder() {
-               return new ImageBuilderImpl();
-       }
-
-       //
        // IMAGESTORE METHODS
        //
 
index 503e6f1..b5d2a84 100644 (file)
@@ -22,6 +22,7 @@ import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.mockito.Mockito.mock;
 
+import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.ImageBuilder;
@@ -42,8 +43,9 @@ public class ImageBuilderImplTest {
        private static final int WIDTH = 640;
        private static final int HEIGHT = 270;
 
-       private final ImageBuilder imageBuilder = new ImageBuilderImpl();
        private final Sone sone = mock(Sone.class);
+       private final Album album = mock(Album.class);
+       private final ImageBuilder imageBuilder = new ImageBuilderImpl(album);
 
        @Test
        public void testImageCreationWithAllExplicitParameters() {