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.xml.sax.Attributes
import org.crosswire.jsword.book.OSISUtil import org.crosswire.jsword.book.OSISUtil
import org.bspeice.minimalbible.SafeValDelegate import org.bspeice.minimalbible.SafeValDelegate
import kotlin.properties.Delegates
/** /**
* Created by bspeice on 9/10/14. * 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 // Don't pass a verse as part of the constructor, but still guarantee
// that it will exist // that it will exist
public var verse: Verse by SafeValDelegate() 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 // TODO: Implement a stack to keep min API 8
val doWrite = ArrayDeque<Boolean>() val doWrite = ArrayDeque<Boolean>()
fun getJson() = verseContent.toJson() fun getJson() = verseContent!!.json
override fun startElement(uri: String, localName: String, override fun startElement(uri: String, localName: String,
qName: String, attributes: Attributes) { qName: String, attributes: Attributes) {
@ -38,6 +37,7 @@ class OsisParser() : DefaultHandler() {
override fun characters(ch: CharArray, start: Int, length: Int) { override fun characters(ch: CharArray, start: Int, length: Int) {
if (doWrite.peek()) 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 import java.util.ArrayList
//TODO: JSON Streaming parsing? http://instagram-engineering.tumblr.com/post/97147584853/json-parsing //TODO: JSON Streaming parsing? http://instagram-engineering.tumblr.com/post/97147584853/json-parsing
class VerseContent(v: Verse) { data class VerseContent(val v: Verse,
val id = v.getOrdinal() val id: Int = v.getOrdinal(),
val bookName = v.getName() val bookName: String = v.getName(),
val chapter = v.getChapter() val chapter: Int = v.getChapter(),
val verseNum = v.getVerse() val verseNum: Int = v.getVerse(),
val chapterTitle = "" val chapterTitle: String = "",
val paraTitle = "" val paraTitle: String = "",
val references: MutableList<VerseReference> = ArrayList() val references: MutableList<VerseReference> = ArrayList(),
var content = "" val content: String = "") {
// Gson is used mostly for serializing the verses
public val json: String public val json: String
get() = Gson().toJson(this) get() = Gson().toJson(this)
public fun toJson(): String { public fun appendContent(content: String): VerseContent =
// Lazy load Gson - not likely that we'll call this method multiple times, so this.copy(this.v, content = this.content + content)
// don't have to worry about a penalty there.
return Gson().toJson(this)
}
public fun appendContent(content: String) {
this.content += content
}
} }