Return a nullable Image instead of an Optional
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 15 Oct 2017 17:13:23 +0000 (19:13 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 15 Oct 2017 17:13:23 +0000 (19:13 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/kotlin/net/pterodactylus/sone/database/ImageProvider.kt
src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java

index 508d167..63c82ed 100644 (file)
@@ -576,9 +576,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        @Nullable
        public Image getImage(String imageId, boolean create) {
-               Optional<Image> image = database.getImage(imageId);
-               if (image.isPresent()) {
-                       return image.get();
+               Image image = database.getImage(imageId);
+               if (image != null) {
+                       return image;
                }
                if (!create) {
                        return null;
index 248592c..79c34d7 100644 (file)
@@ -527,11 +527,12 @@ public class MemoryDatabase extends AbstractService implements Database {
        // IMAGEPROVIDER METHODS
        //
 
+       @Nullable
        @Override
-       public Optional<Image> getImage(String imageId) {
+       public Image getImage(@Nonnull String imageId) {
                lock.readLock().lock();
                try {
-                       return fromNullable(allImages.get(imageId));
+                       return allImages.get(imageId);
                } finally {
                        lock.readLock().unlock();
                }
index 987c52f..c7cc75e 100644 (file)
@@ -26,6 +26,6 @@ import com.google.common.base.Optional
  */
 interface ImageProvider {
 
-       fun getImage(imageId: String): Optional<Image>
+       fun getImage(imageId: String): Image?
 
 }
index daa5e4d..6e66b18 100644 (file)
@@ -198,16 +198,16 @@ public class MemoryDatabaseTest {
                assertThat(memoryDatabase.getAlbum("album3").get(),
                                isAlbum("album3", "album1", "album3", "album-description3"));
                assertThat(memoryDatabase.getAlbum("album4").isPresent(), is(false));
-               assertThat(memoryDatabase.getImage("image1").get(),
+               assertThat(memoryDatabase.getImage("image1"),
                                isImage("image1", 1000L, "KSK@image1", "image1",
                                                "image-description1", 16, 9));
-               assertThat(memoryDatabase.getImage("image2").get(),
+               assertThat(memoryDatabase.getImage("image2"),
                                isImage("image2", 2000L, "KSK@image2", "image2",
                                                "image-description2", 32, 18));
-               assertThat(memoryDatabase.getImage("image3").get(),
+               assertThat(memoryDatabase.getImage("image3"),
                                isImage("image3", 3000L, "KSK@image3", "image3",
                                                "image-description3", 48, 27));
-               assertThat(memoryDatabase.getImage("image4").isPresent(), is(false));
+               assertThat(memoryDatabase.getImage("image4"), nullValue());
        }
 
        @Test