Make the VerseContent immutable

Not quite certain this is the way to go, but let's try a functional experiment.
This commit is contained in:
Bradlee Speice 2014-11-13 00:04:20 -05:00
parent 1ee1a41004
commit 6c139eacab
2 changed files with 16 additions and 22 deletions

View File

@ -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<Boolean>()
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))
}
}

View File

@ -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<VerseReference> = 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<VerseReference> = 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)
}