Don’t hand in null to function not allowing it
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryPost.java
index 84f3714..75eae49 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - PostImpl.java - Copyright © 2010–2013 David Roden
+ * Sone - MemoryPost.java - Copyright © 2010–2016 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
 
 package net.pterodactylus.sone.database.memory;
 
+import static com.google.common.base.Optional.fromNullable;
+
 import java.util.UUID;
 
+import javax.annotation.Nullable;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.SoneProvider;
 
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 
 /**
  * A post is a short message that a user writes in his Sone to let other users
  * know what is going on.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 class MemoryPost implements Post {
 
        /** The post database. */
-       private final MemoryPostDatabase postDatabase;
+       private final MemoryDatabase postDatabase;
 
        /** The Sone provider. */
        private final SoneProvider soneProvider;
@@ -72,7 +75,7 @@ class MemoryPost implements Post {
         * @param text
         *            The text of the post
         */
-       public MemoryPost(MemoryPostDatabase postDatabase, SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
+       public MemoryPost(MemoryDatabase postDatabase, SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
                this.postDatabase = postDatabase;
                this.soneProvider = soneProvider;
                this.id = UUID.fromString(id);
@@ -94,12 +97,17 @@ class MemoryPost implements Post {
                return id.toString();
        }
 
+       @Override
+       public boolean isLoaded() {
+               return true;
+       }
+
        /**
         * {@inheritDoc}
         */
        @Override
        public Sone getSone() {
-               return soneProvider.getSone(soneId).get();
+               return soneProvider.getSone(soneId);
        }
 
        /**
@@ -107,7 +115,7 @@ class MemoryPost implements Post {
         */
        @Override
        public Optional<String> getRecipientId() {
-               return Optional.fromNullable(recipientId);
+               return fromNullable(recipientId);
        }
 
        /**
@@ -115,7 +123,13 @@ class MemoryPost implements Post {
         */
        @Override
        public Optional<Sone> getRecipient() {
-               return soneProvider.getSone(recipientId);
+               return Optional.fromNullable(recipientId).transform(new Function<String, Sone>() {
+                       @Nullable
+                       @Override
+                       public Sone apply(String input) {
+                               return soneProvider.getSone(input);
+                       }
+               });
        }
 
        /**