From 39f730db92c425a510e19d33ba06b722c3dc299f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 9 Nov 2014 11:56:24 +0100 Subject: [PATCH] Return empty posts for not-yet loaded bookmarks. --- .../java/net/pterodactylus/sone/data/Post.java | 63 ++++++++++++++++++++++ .../database/memory/MemoryBookmarkDatabase.java | 10 ++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 09198fd..95abae6 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.data; +import static com.google.common.base.Optional.absent; + import java.util.Comparator; import com.google.common.base.Optional; @@ -122,4 +124,65 @@ public interface Post extends Identified { */ public Post setKnown(boolean known); + /** + * Shell for a post that has not yet been loaded. + * + * @author David ‘Bombe’ + * Roden + */ + public static class EmptyPost implements Post { + + private final String id; + + public EmptyPost(String id) { + this.id = id; + } + + @Override + public String getId() { + return id; + } + + @Override + public boolean isLoaded() { + return false; + } + + @Override + public Sone getSone() { + return null; + } + + @Override + public Optional getRecipientId() { + return absent(); + } + + @Override + public Optional getRecipient() { + return absent(); + } + + @Override + public long getTime() { + return 0; + } + + @Override + public String getText() { + return null; + } + + @Override + public boolean isKnown() { + return false; + } + + @Override + public Post setKnown(boolean known) { + return this; + } + + } + } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java index 38c5bd0..594cf2b 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java @@ -8,6 +8,7 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.Post.EmptyPost; import net.pterodactylus.sone.database.BookmarkDatabase; import com.google.common.base.Function; @@ -94,11 +95,12 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase { public Set getBookmarkedPosts() { lock.readLock().lock(); try { - return from(bookmarkedPosts).transformAndConcat( - new Function>() { + return from(bookmarkedPosts).transform( + new Function() { @Override - public Iterable apply(String postId) { - return memoryDatabase.getPost(postId).asSet(); + public Post apply(String postId) { + return memoryDatabase.getPost(postId) + .or(new EmptyPost(postId)); } }).toSet(); } finally { -- 2.7.4