feat: Add getter for default option value

This commit is contained in:
oSumAtrIX 2023-10-21 23:58:55 +02:00
parent c1f4c0445a
commit c7922e90d0
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
2 changed files with 34 additions and 3 deletions

View File

@ -362,6 +362,7 @@ public abstract interface annotation class app/revanced/patcher/patch/annotation
public abstract class app/revanced/patcher/patch/options/PatchOption { public abstract class app/revanced/patcher/patch/options/PatchOption {
public fun <init> (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;)V public fun <init> (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;ZLkotlin/jvm/functions/Function1;)V
public final fun getDefault ()Ljava/lang/Object;
public final fun getDescription ()Ljava/lang/String; public final fun getDescription ()Ljava/lang/String;
public final fun getKey ()Ljava/lang/String; public final fun getKey ()Ljava/lang/String;
public final fun getRequired ()Z public final fun getRequired ()Z

View File

@ -15,7 +15,7 @@ import kotlin.reflect.KProperty
*/ */
abstract class PatchOption<T>( abstract class PatchOption<T>(
val key: String, val key: String,
default: T?, val default: T?,
val title: String?, val title: String?,
val description: String?, val description: String?,
val required: Boolean, val required: Boolean,
@ -25,12 +25,42 @@ abstract class PatchOption<T>(
* The value of the [PatchOption]. * The value of the [PatchOption].
*/ */
var value: T? = default var value: T? = default
/**
* Set the value of the [PatchOption].
*
* @param value The value to set.
*
* @throws PatchOptionException.ValueRequiredException If the value is required but null.
* @throws PatchOptionException.ValueValidationException If the value is invalid.
*/
set(value) { set(value) {
if (required && value == null) throw PatchOptionException.ValueRequiredException(this) assertRequiredButNotNull(value)
if (!validator(value)) throw PatchOptionException.ValueValidationException(value, this) assertValid(value)
field = value field = value
} }
/**
* Get the value of the [PatchOption].
*
* @return The value.
*
* @throws PatchOptionException.ValueRequiredException If the value is required but null.
* @throws PatchOptionException.ValueValidationException If the value is invalid.
*/
get() {
assertRequiredButNotNull(field)
assertValid(field)
return field
}
private fun assertRequiredButNotNull(value: T?) {
if (required && value == null) throw PatchOptionException.ValueRequiredException(this)
}
private fun assertValid(value: T?) {
if (!validator(value)) throw PatchOptionException.ValueValidationException(value, this)
}
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value operator fun getValue(thisRef: Any?, property: KProperty<*>) = value