From 6c139eacab813116c949215957f1e6768928f265 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Thu, 13 Nov 2014 00:04:20 -0500 Subject: [PATCH] Make the VerseContent immutable Not quite certain this is the way to go, but let's try a functional experiment. --- .../service/format/osisparser/OsisParser.kt | 8 ++--- .../service/format/osisparser/VerseContent.kt | 30 ++++++++----------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt index 962ca56..28872e4 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt @@ -6,7 +6,6 @@ import java.util.ArrayDeque import org.xml.sax.Attributes import org.crosswire.jsword.book.OSISUtil import org.bspeice.minimalbible.SafeValDelegate -import kotlin.properties.Delegates /** * Created by bspeice on 9/10/14. @@ -17,12 +16,12 @@ class OsisParser() : DefaultHandler() { // Don't pass a verse as part of the constructor, but still guarantee // that it will exist public var verse: Verse by SafeValDelegate() - val verseContent: VerseContent by Delegates.lazy { VerseContent(verse) } + var verseContent: VerseContent? = null // TODO: Implement a stack to keep min API 8 val doWrite = ArrayDeque() - fun getJson() = verseContent.toJson() + fun getJson() = verseContent!!.json override fun startElement(uri: String, localName: String, qName: String, attributes: Attributes) { @@ -38,6 +37,7 @@ class OsisParser() : DefaultHandler() { override fun characters(ch: CharArray, start: Int, length: Int) { if (doWrite.peek()) - verseContent.appendContent(String(ch)) + verseContent = verseContent?.appendContent(String(ch)) ?: + VerseContent(verse, content = String(ch)) } } \ No newline at end of file diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/VerseContent.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/VerseContent.kt index 5b67a78..9ec5f03 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/VerseContent.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/VerseContent.kt @@ -8,26 +8,20 @@ import org.crosswire.jsword.passage.Verse import java.util.ArrayList //TODO: JSON Streaming parsing? http://instagram-engineering.tumblr.com/post/97147584853/json-parsing -class VerseContent(v: Verse) { - val id = v.getOrdinal() - val bookName = v.getName() - val chapter = v.getChapter() - val verseNum = v.getVerse() - val chapterTitle = "" - val paraTitle = "" - val references: MutableList = ArrayList() - var content = "" +data class VerseContent(val v: Verse, + val id: Int = v.getOrdinal(), + val bookName: String = v.getName(), + val chapter: Int = v.getChapter(), + val verseNum: Int = v.getVerse(), + val chapterTitle: String = "", + val paraTitle: String = "", + val references: MutableList = ArrayList(), + val content: String = "") { + // Gson is used mostly for serializing the verses public val json: String get() = Gson().toJson(this) - public fun toJson(): String { - // Lazy load Gson - not likely that we'll call this method multiple times, so - // don't have to worry about a penalty there. - return Gson().toJson(this) - } - - public fun appendContent(content: String) { - this.content += content - } + public fun appendContent(content: String): VerseContent = + this.copy(this.v, content = this.content + content) } \ No newline at end of file