From 549af4fa7323c58e0660f21dd8605cbc812db286 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 8 Aug 2020 22:30:18 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=9A=20Move=20shells=20closer=20to=20the?= =?utf8?q?=20interfaces?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/net/pterodactylus/sone/data/Post.kt | 10 ++++++++++ src/main/kotlin/net/pterodactylus/sone/data/Reply.kt | 11 +++++++++++ .../net/pterodactylus/sone/database/memory/MemoryDatabase.kt | 9 ++++++--- .../net/pterodactylus/sone/database/memory/MemoryPost.kt | 10 +--------- .../net/pterodactylus/sone/database/memory/MemoryPostReply.kt | 11 +---------- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Post.kt b/src/main/kotlin/net/pterodactylus/sone/data/Post.kt index d87bd3c..5781508 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Post.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Post.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.data +import net.pterodactylus.sone.database.PostBuilder import java.util.Comparator.comparing /** @@ -14,3 +15,12 @@ val noFuturePost: (Post) -> Boolean = { it.time <= System.currentTimeMillis() } */ @get:JvmName("newestPostFirst") val newestPostFirst: Comparator = comparing(Post::getTime).reversed() + +data class PostShell(val id: String, val soneId: String, val recipientId: String?, val time: Long, val text: String) { + + fun build(postBuilder: PostBuilder) = + postBuilder.withId(id).from(soneId).let { if (recipientId != null) it.to(recipientId) else it }.withTime(time).withText(text).build() + +} + +fun Post.toShell() = PostShell(id, sone!!.id, recipient.orNull()?.id, time, text) diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt b/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt index cfc940a..d570f48 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt @@ -17,6 +17,7 @@ package net.pterodactylus.sone.data +import net.pterodactylus.sone.database.PostReplyBuilder import java.util.Comparator.comparing /** @@ -32,3 +33,13 @@ val newestReplyFirst: Comparator> = */ val noFutureReply: (Reply<*>) -> Boolean = { it.getTime() <= System.currentTimeMillis() } + +data class PostReplyShell(val id: String, val soneId: String, val postId: String, val time: Long, val text: String) { + + fun build(postReplyBuilder: PostReplyBuilder): PostReply { + return postReplyBuilder.withId(id).from(soneId).to(postId).withTime(time).withText(text).build() + } + +} + +fun PostReply.toShell() = PostReplyShell(id, sone.id, postId, time, text) diff --git a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt index e2281ac..2ab0938 100644 --- a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt +++ b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabase.kt @@ -29,12 +29,15 @@ import net.pterodactylus.sone.data.Album import net.pterodactylus.sone.data.Image import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.data.PostReplyShell +import net.pterodactylus.sone.data.PostShell import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.data.allAlbums import net.pterodactylus.sone.data.allImages import net.pterodactylus.sone.data.impl.AlbumBuilderImpl import net.pterodactylus.sone.data.impl.ImageBuilderImpl import net.pterodactylus.sone.data.newestReplyFirst +import net.pterodactylus.sone.data.toShell import net.pterodactylus.sone.database.AlbumBuilder import net.pterodactylus.sone.database.Database import net.pterodactylus.sone.database.DatabaseException @@ -62,10 +65,10 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio private val writeLock: WriteLock by lazy { lock.writeLock() } private val configurationLoader = ConfigurationLoader(configuration) private val allSones = mutableMapOf() - private val allPosts = mutableMapOf() - private val sonePosts: Multimap = HashMultimap.create() + private val allPosts = mutableMapOf() + private val sonePosts: Multimap = HashMultimap.create() private val knownPosts = mutableSetOf() - private val allPostReplies = mutableMapOf() + private val allPostReplies = mutableMapOf() private val sonePostReplies: Multimap = TreeMultimap.create(Comparator { leftString, rightString -> leftString.compareTo(rightString) }, newestReplyFirst) private val knownPostReplies = mutableSetOf() private val allAlbums = mutableMapOf() diff --git a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPost.kt b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPost.kt index b3664c2..d12ab5f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPost.kt +++ b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPost.kt @@ -17,6 +17,7 @@ package net.pterodactylus.sone.database.memory import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostShell import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.database.PostBuilder import net.pterodactylus.sone.database.SoneProvider @@ -60,13 +61,4 @@ class MemoryPost( override fun toString() = "${javaClass.name}[id=$id,sone=$soneId,recipient=$recipientId,time=$time,text=$text]" - data class Shell(val id: String, val soneId: String, val recipientId: String?, val time: Long, val text: String) { - - fun build(postBuilder: PostBuilder) = - postBuilder.withId(id).from(soneId).let { if (recipientId != null) it.to(recipientId) else it }.withTime(time).withText(text).build() - - } - } - -fun Post.toShell() = MemoryPost.Shell(id, sone!!.id, recipient.orNull()?.id, time, text) diff --git a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt index 67b44b7..f555924 100644 --- a/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt +++ b/src/main/kotlin/net/pterodactylus/sone/database/memory/MemoryPostReply.kt @@ -19,6 +19,7 @@ package net.pterodactylus.sone.database.memory import com.google.common.base.Optional import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.data.PostReplyShell import net.pterodactylus.sone.database.PostReplyBuilder import net.pterodactylus.sone.database.SoneProvider import net.pterodactylus.sone.utils.asOptional @@ -68,14 +69,4 @@ class MemoryPostReply( '}' } - class Shell(val id: String, val soneId: String, val postId: String, val time: Long, val text: String) { - - fun build(postReplyBuilder: PostReplyBuilder): PostReply { - return postReplyBuilder.withId(id).from(soneId).to(postId).withTime(time).withText(text).build() - } - - } - } - -fun PostReply.toShell() = MemoryPostReply.Shell(id, sone.id, postId, time, text) -- 2.7.4