mirror of
https://github.com/revanced/revanced-patcher.git
synced 2025-04-29 21:04:26 +02:00
feat: Add getter for default option value
This commit is contained in:
parent
c1f4c0445a
commit
c7922e90d0
@ -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 <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 getKey ()Ljava/lang/String;
|
||||
public final fun getRequired ()Z
|
||||
|
@ -15,7 +15,7 @@ import kotlin.reflect.KProperty
|
||||
*/
|
||||
abstract class PatchOption<T>(
|
||||
val key: String,
|
||||
default: T?,
|
||||
val default: T?,
|
||||
val title: String?,
|
||||
val description: String?,
|
||||
val required: Boolean,
|
||||
@ -25,12 +25,42 @@ abstract class PatchOption<T>(
|
||||
* 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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user