Rename FinalDelegate to SafeValDelegate

This commit is contained in:
Bradlee Speice 2014-11-11 13:29:55 -05:00
parent 0c74d99a06
commit b778748ee0
3 changed files with 11 additions and 9 deletions

View File

@ -1,19 +1,19 @@
package org.bspeice.minimalbible.test;
import org.bspeice.minimalbible.FinalDelegate;
import org.bspeice.minimalbible.MBTestCase;
import org.bspeice.minimalbible.SafeValDelegate;
import kotlin.PropertyMetadataImpl;
/**
* Test that the FinalDelegate actually obeys its contract
*/
public class FinalDelegateTest extends MBTestCase {
public class SafeValDelegateTest extends MBTestCase {
FinalDelegate<String> delegate;
SafeValDelegate<String> delegate;
public void setUp() {
delegate = new FinalDelegate<String>();
delegate = new SafeValDelegate<String>();
}
public void testDelegateNullSafety() {

View File

@ -1,20 +1,22 @@
package org.bspeice.minimalbible
import kotlin.properties.ReadWriteProperty
/**
* The purpose of this delegate is to guarantee null-safety, while
* also ensuring a pseudo-val type. If you try to read before use, error.
* If you try to set multiple times, error.
*/
public class FinalDelegate<T : Any>() {
public class SafeValDelegate<T : Any>() : ReadWriteProperty<Any?, T> {
private var value: T? = null
private var didAssign: Boolean = false
public fun get(thisRef: Any?, desc: PropertyMetadata): T {
public override fun get(thisRef: Any?, desc: PropertyMetadata): T {
return value ?: throw IllegalStateException("Property ${desc.name} should be initialized before get")
}
public fun set(thisRef: Any?, desc: PropertyMetadata, value: T) {
public override fun set(thisRef: Any?, desc: PropertyMetadata, value: T) {
if (!didAssign) {
this.value = value
this.didAssign = true

View File

@ -5,7 +5,7 @@ import org.crosswire.jsword.passage.Verse
import java.util.ArrayDeque
import org.xml.sax.Attributes
import org.crosswire.jsword.book.OSISUtil
import org.bspeice.minimalbible.FinalDelegate
import org.bspeice.minimalbible.SafeValDelegate
/**
* Created by bspeice on 9/10/14.
@ -15,7 +15,7 @@ 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 FinalDelegate()
public var verse: Verse by SafeValDelegate()
val verseContent: VerseContent
get() = VerseContent(verse)