Skip to content

Commit

Permalink
Look up properties from RHMIApplication even if not set in XML
Browse files Browse the repository at this point in the history
  • Loading branch information
hufman committed Feb 29, 2024
1 parent a56301a commit 8ebc5d4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
21 changes: 21 additions & 0 deletions src/main/java/io/bimmergestalt/idriveconnectkit/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,25 @@ object Utils {
hasher.update(data)
return hasher.digest()
}
}

fun <K, V> MutableMap<K, V>.withRealDefault(defaultValue: (key: K) -> V): MutableMap<K, V> = MutableMapWithRealDefault(this, defaultValue)

class MutableMapWithRealDefault<K, V>(public val map: MutableMap<K, V>, private val default: (key: K) -> V) : MutableMap<K, V> {
override fun equals(other: Any?): Boolean = map.equals(other)
override fun hashCode(): Int = map.hashCode()
override fun toString(): String = map.toString()
override val size: Int get() = map.size
override fun isEmpty(): Boolean = map.isEmpty()
override fun containsKey(key: K): Boolean = map.containsKey(key)
override fun containsValue(value: @UnsafeVariance V): Boolean = map.containsValue(value)
override fun get(key: K): V? = map[key] ?: default(key)
override val keys: MutableSet<K> get() = map.keys
override val values: MutableCollection<V> get() = map.values
override val entries: MutableSet<MutableMap.MutableEntry<K, V>> get() = map.entries

override fun put(key: K, value: V): V? = map.put(key, value)
override fun remove(key: K): V? = map.remove(key)
override fun putAll(from: Map<out K, V>) = map.putAll(from)
override fun clear() = map.clear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.bimmergestalt.idriveconnectkit.rhmi

import io.bimmergestalt.idriveconnectkit.Utils.etchAsInt
import io.bimmergestalt.idriveconnectkit.rhmi.mocking.RHMIApplicationMock
import io.bimmergestalt.idriveconnectkit.withRealDefault
import io.bimmergestalt.idriveconnectkit.xmlutils.XMLUtils
import io.bimmergestalt.idriveconnectkit.xmlutils.getAttributesMap
import io.bimmergestalt.idriveconnectkit.xmlutils.getChildElements
Expand All @@ -11,7 +12,10 @@ import org.w3c.dom.Node

abstract class RHMIComponent private constructor(open val app: RHMIApplication, open val id: Int) {

val properties: MutableMap<Int, RHMIProperty> = HashMap()
val properties: MutableMap<Int, RHMIProperty> = HashMap<Int, RHMIProperty>().withRealDefault { propertyId ->
// look up from the app storage if the property wasn't loaded from xml
RHMIProperty.AppProperty(app, id, propertyId)
}

companion object {
fun loadFromXML(app: RHMIApplication, node: Node): RHMIComponent? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.bimmergestalt.idriveconnectkit.rhmi
import io.bimmergestalt.idriveconnectkit.xmlutils.*
import org.w3c.dom.Node

abstract class RHMIProperty(val id: Int, open var value: Any = 0) {
abstract class RHMIProperty(val id: Int, open var value: Any? = null) {

enum class PropertyId(val id: Int) {
ENABLED(1),
Expand Down Expand Up @@ -67,21 +67,21 @@ abstract class RHMIProperty(val id: Int, open var value: Any = 0) {
}
}

open fun getForLayout(layout: Int): Any {
open fun getForLayout(layout: Int): Any? {
return value
}

class SimpleProperty(id: Int, value: Any = 0): RHMIProperty(id, value)

class LayoutBag(id: Int, value: Any = 0, val values: Map<Int, Any>): RHMIProperty(id, value) {
override fun getForLayout(layout: Int): Any {
override fun getForLayout(layout: Int): Any? {
return values.getOrElse(layout) { value }
}
}

class AppProperty(val app: RHMIApplication, val componentId: Int, id: Int, private val defaultValue: Any = 0): RHMIProperty(id) {
override fun getForLayout(layout: Int): Any = app.getProperty(componentId, id) ?: value
override var value: Any
class AppProperty(val app: RHMIApplication, val componentId: Int, id: Int, private val defaultValue: Any? = null): RHMIProperty(id) {
override fun getForLayout(layout: Int): Any? = app.getProperty(componentId, id) ?: value
override var value: Any?
get() = app.getProperty(componentId, id) ?: defaultValue
set(value) { app.setProperty(componentId, id, value) }
}
Expand Down

0 comments on commit 8ebc5d4

Please sign in to comment.