jvmMain.validations.ScopedJvmValidations.kt Maven / Gradle / Ivy
package io.kform.validations
import io.kform.ValidationContext
import io.kform.ValidationIssue
import java.util.*
import java.util.concurrent.CompletionStage
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.future.await
/** [ScopedValidation] more easily implementable from Java. */
public abstract class ScopedJvmValidation
@JvmOverloads
constructor(scopeExternalContextName: String = DEFAULT_SCOPE_EXTERNAL_CONTEXT_NAME) :
ScopedValidation(scopeExternalContextName) {
public override var dependsOnDescendants: Boolean = false
protected set
public final override val scopes: ValidationScopes
get() = scopes()
/** Returns the validation scopes of this validation. */
public abstract fun scopes(): ValidationScopes
public abstract override fun ValidationContext.scopedValidate(): Flow
}
/** Synchronous [scoped validation][ScopedValidation] more easily implementable from Java. */
public abstract class ScopedSyncValidation
@JvmOverloads
constructor(scopeExternalContextName: String = DEFAULT_SCOPE_EXTERNAL_CONTEXT_NAME) :
ScopedJvmValidation(scopeExternalContextName) {
/**
* Synchronous implementation of [ValidationContext.scopedValidate]. Validation issues should be
* added to [issues].
*/
public abstract fun scopedValidate(
context: ValidationContext,
issues: MutableList
)
final override fun ValidationContext.scopedValidate(): Flow {
val issues = ArrayList(10)
scopedValidate(this, issues)
return issues.asFlow()
}
}
/** Asynchronous [scoped validation][ScopedValidation] more easily implementable from Java. */
public abstract class ScopedAsyncValidation
@JvmOverloads
constructor(scopeExternalContextName: String = DEFAULT_SCOPE_EXTERNAL_CONTEXT_NAME) :
ScopedJvmValidation(scopeExternalContextName) {
/**
* Asynchronous implementation of [ValidationContext.scopedValidate] returning a completion
* stage. Validation issues should be added to [issues] (which is a synchronized list).
*/
public abstract fun scopedValidate(
context: ValidationContext,
issues: MutableList
): CompletionStage?
final override fun ValidationContext.scopedValidate(): Flow = flow {
val issues = Collections.synchronizedList(ArrayList(10))
scopedValidate(this@scopedValidate, issues)?.await() ?: return@flow
for (issue in issues) {
emit(issue)
}
}
}