From c7922e90d0c6ae83f513611c706ebea33c1a2b63 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 21 Oct 2023 23:58:55 +0200 Subject: [PATCH] feat: Add getter for default option value --- api/revanced-patcher.api | 1 + .../patcher/patch/options/PatchOption.kt | 36 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/api/revanced-patcher.api b/api/revanced-patcher.api index c4eefa4..bd4b222 100644 --- a/api/revanced-patcher.api +++ b/api/revanced-patcher.api @@ -362,6 +362,7 @@ public abstract interface annotation class app/revanced/patcher/patch/annotation public abstract class app/revanced/patcher/patch/options/PatchOption { public fun (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 getKey ()Ljava/lang/String; public final fun getRequired ()Z diff --git a/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt b/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt index 9c76c42..e737d52 100644 --- a/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt +++ b/src/main/kotlin/app/revanced/patcher/patch/options/PatchOption.kt @@ -15,7 +15,7 @@ import kotlin.reflect.KProperty */ abstract class PatchOption( val key: String, - default: T?, + val default: T?, val title: String?, val description: String?, val required: Boolean, @@ -25,12 +25,42 @@ abstract class PatchOption( * The value of the [PatchOption]. */ 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) { - if (required && value == null) throw PatchOptionException.ValueRequiredException(this) - if (!validator(value)) throw PatchOptionException.ValueValidationException(value, this) + assertRequiredButNotNull(value) + assertValid(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