From aecb9e4e3733a434b3f5b2f73bb4e17a8d3c71e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20Hu=CC=88sers?= Date: Tue, 19 Mar 2024 15:03:29 +0100 Subject: [PATCH] chore: Extract VssSignal + VssBranch interface --- .../eclipse/kuksa/vsscore/model/VssBranch.kt | 28 ++++++++ .../eclipse/kuksa/vsscore/model/VssNode.kt | 60 ---------------- .../eclipse/kuksa/vsscore/model/VssSignal.kt | 72 +++++++++++++++++++ 3 files changed, 100 insertions(+), 60 deletions(-) create mode 100644 vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssBranch.kt create mode 100644 vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssSignal.kt diff --git a/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssBranch.kt b/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssBranch.kt new file mode 100644 index 00000000..91520d31 --- /dev/null +++ b/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssBranch.kt @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Contributors to the Eclipse Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.eclipse.kuksa.vsscore.model + +/** + * Defines a [VssNode] which is not a [VssSignal] and only acts as a branch with one or more children. The [type] is + * always "branch". + */ +interface VssBranch : VssNode { + override val type: String + get() = "branch" +} diff --git a/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssNode.kt b/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssNode.kt index b9acc481..746ffaad 100644 --- a/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssNode.kt +++ b/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssNode.kt @@ -69,48 +69,6 @@ interface VssNode { get() = null } -/** - * Defines a [VssNode] which is not a [VssSignal] and only acts as a branch with one or more children. The [type] is - * always "branch". - */ -interface VssBranch : VssNode { - override val type: String - get() = "branch" -} - -/** - * Some [VssNode] may have an additional [value] property. These are children [VssSignal] which do not have other - * children. - */ -interface VssSignal : VssNode { - /** - * A primitive type value. - */ - val value: T - - /** - * The VSS data type which is compatible with the Databroker. This may differ from the [value] type because - * Java compatibility needs to be ensured and inline classes like [UInt] (Kotlin) are not known to Java. - * - * ### Example - * Vehicle.Driver.HeartRate: - * datatype: uint16 - * - * generates --> - * - * public data class VssHeartRate ( - * override val `value`: Int = 0, - * ) : VssSignal { - * override val dataType: KClass<*> - * get() = UInt:class - * } - * - * To ensure java compatibility [UInt] is not used here for Kotlin (inline class). - */ - val dataType: KClass<*> - get() = value::class -} - /** * Splits the [VssNode.vssPath] into its parts. */ @@ -241,21 +199,3 @@ fun VssNode.findHeritageLine( return heritageLine } - -/** - * Finds the given [signal] inside the current [VssNode]. - */ -inline fun , V : Any> VssNode.findSignal(signal: T): VssNode { - return heritage - .first { it.uuid == signal.uuid } -} - -/** - * Finds all [VssSignal] which matches the given [KClass.simpleName]. This is useful when multiple nested objects - * with the same Name exists but are pretty much the same besides the [VssNode.vssPath] etc. - */ -inline fun , V : Any> VssNode.findSignal(type: KClass): Map { - return heritage - .filter { it::class.simpleName == type.simpleName } - .associateBy { it.vssPath } -} diff --git a/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssSignal.kt b/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssSignal.kt new file mode 100644 index 00000000..6bc531fa --- /dev/null +++ b/vss-core/src/main/kotlin/org/eclipse/kuksa/vsscore/model/VssSignal.kt @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 Contributors to the Eclipse Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.eclipse.kuksa.vsscore.model + +import kotlin.reflect.KClass + +/** + * Some [VssNode] may have an additional [value] property. These are children [VssSignal] which do not have other + * children. + */ +interface VssSignal : VssNode { + /** + * A primitive type value. + */ + val value: T + + /** + * The VSS data type which is compatible with the Databroker. This may differ from the [value] type because + * Java compatibility needs to be ensured and inline classes like [UInt] (Kotlin) are not known to Java. + * + * ### Example + * Vehicle.Driver.HeartRate: + * datatype: uint16 + * + * generates --> + * + * public data class VssHeartRate ( + * override val `value`: Int = 0, + * ) : VssSignal { + * override val dataType: KClass<*> + * get() = UInt:class + * } + * + * To ensure java compatibility [UInt] is not used here for Kotlin (inline class). + */ + val dataType: KClass<*> + get() = value::class +} + +/** + * Finds the given [signal] inside the current [VssNode]. + */ +inline fun , V : Any> VssNode.findSignal(signal: T): VssNode { + return heritage + .first { it.uuid == signal.uuid } +} + +/** + * Finds all [VssSignal] which matches the given [KClass.simpleName]. This is useful when multiple nested objects + * with the same Name exists but are pretty much the same besides the [VssNode.vssPath] etc. + */ +inline fun , V : Any> VssNode.findSignal(type: KClass): Map { + return heritage + .filter { it::class.simpleName == type.simpleName } + .associateBy { it.vssPath } +}