Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d1a442
commit 7569159
Showing
6 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
krate/src/androidTest/java/hu/autsoft/krate/DefaultTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package hu.autsoft.krate | ||
|
||
import android.support.test.InstrumentationRegistry | ||
import android.support.test.runner.AndroidJUnit4 | ||
import junit.framework.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class DefaultTests { | ||
|
||
private lateinit var testKrate: TestKrate | ||
|
||
@Before | ||
fun setup() { | ||
val appContext = InstrumentationRegistry.getTargetContext() | ||
testKrate = TestKrate(appContext) | ||
} | ||
|
||
@Test | ||
fun testValidatedDefaultFloatDelegate() { | ||
testKrate.defaultValidatedFloat = .67f | ||
|
||
assertEquals(.67f, testKrate.defaultValidatedFloat) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testValidatedDefaultFloatDelegateThrowsException() { | ||
testKrate.defaultValidatedFloat = 5.0f | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
@file:Suppress("RedundantVisibilityModifier") | ||
|
||
package hu.autsoft.krate | ||
|
||
import hu.autsoft.krate.validated.ValidatedPreferenceDelegate | ||
import hu.autsoft.krate.default.FloatDelegateWithDefault | ||
import hu.autsoft.krate.default.IntDelegateWithDefault | ||
import hu.autsoft.krate.default.LongDelegateWithDefault | ||
import hu.autsoft.krate.default.StringDelegateWithDefault | ||
import hu.autsoft.krate.default.StringSetDelegateWithDefault | ||
import hu.autsoft.krate.optional.FloatDelegate | ||
import hu.autsoft.krate.optional.IntDelegate | ||
import hu.autsoft.krate.optional.LongDelegate | ||
import hu.autsoft.krate.optional.StringDelegate | ||
import hu.autsoft.krate.optional.StringSetDelegate | ||
import kotlin.properties.ReadWriteProperty | ||
|
||
/** | ||
* Creates a validated, optional preference of type [Float] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.floatPref(key: String, isValid: (Float?) -> Boolean): ReadWriteProperty<Krate, Float?> { | ||
return ValidatedPreferenceDelegate(FloatDelegate(key), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, non-optional preference of type [Float] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.floatPref(key: String, default: Float, isValid: (Float) -> Boolean): ReadWriteProperty<Krate, Float> { | ||
return ValidatedPreferenceDelegate(FloatDelegateWithDefault(key, default), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, optional preference of type [Int] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.intPref(key: String, isValid: (Int?) -> Boolean): ReadWriteProperty<Krate, Int?> { | ||
return ValidatedPreferenceDelegate(IntDelegate(key), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, non-optional preference of type [Int] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.intPref(key: String, default: Int, isValid: (Int) -> Boolean): ReadWriteProperty<Krate, Int> { | ||
return ValidatedPreferenceDelegate(IntDelegateWithDefault(key, default), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, optional preference of type [Long] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.longPref(key: String, isValid: (Long?) -> Boolean): ReadWriteProperty<Krate, Long?> { | ||
return ValidatedPreferenceDelegate(LongDelegate(key), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, non-optional preference of type [Long] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.longPref(key: String, defaultValue: Long, isValid: (Long) -> Boolean): ReadWriteProperty<Krate, Long> { | ||
return ValidatedPreferenceDelegate(LongDelegateWithDefault(key, defaultValue), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, optional preference of type [String] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.stringPref(key: String, isValid: (String?) -> Boolean): ReadWriteProperty<Krate, String?> { | ||
return ValidatedPreferenceDelegate(StringDelegate(key), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, non-optional preference of type [String] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.stringPref(key: String, default: String, isValid: (String) -> Boolean): ReadWriteProperty<Krate, String> { | ||
return ValidatedPreferenceDelegate(StringDelegateWithDefault(key, default), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, optional preference of type [Set] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.stringSetPref(key: String, isValid: (Set<String>?) -> Boolean): ReadWriteProperty<Krate, Set<String>?> { | ||
return ValidatedPreferenceDelegate(StringSetDelegate(key), isValid) | ||
} | ||
|
||
/** | ||
* Creates a validated, non-optional preference of type [Set] with the given [key]. | ||
* | ||
* If a value being set to this preference returns `false` when checked by [isValid], | ||
* an [IllegalArgumentException] will be thrown. | ||
*/ | ||
public fun Krate.stringSetPref(key: String, defaultValue: Set<String>, isValid: (Set<String>) -> Boolean): ReadWriteProperty<Krate, Set<String>> { | ||
return ValidatedPreferenceDelegate(StringSetDelegateWithDefault(key, defaultValue), isValid) | ||
} |
27 changes: 27 additions & 0 deletions
27
krate/src/main/kotlin/hu/autsoft/krate/validated/ValidatedPreferenceDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package hu.autsoft.krate.validated | ||
|
||
import hu.autsoft.krate.Krate | ||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* [ValidatedPreferenceDelegate] is a generic [ReadWriteProperty] that can be used to | ||
* validate values that are being set. | ||
* | ||
* @param [delegate] the [ReadWriteProperty] implementation that is used for delegation | ||
* @param [isValid] the lambda used to validate property values on [setValue] | ||
*/ | ||
internal class ValidatedPreferenceDelegate<T>( | ||
private val delegate: ReadWriteProperty<Krate, T>, | ||
private val isValid: (T) -> Boolean | ||
) : ReadWriteProperty<Krate, T> by delegate { | ||
|
||
override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { | ||
if (!isValid(value)) { | ||
throw IllegalArgumentException("$value is not valid for ${property.name}.") | ||
} | ||
|
||
delegate.setValue(thisRef, property, value) | ||
} | ||
|
||
} |