@@ -51,7 +51,6 @@ open class Model(
51
51
* specified, must also specify [_parentModel]
52
52
*/
53
53
private val _parentProperty : String? = null ,
54
- private val initializationLock : Any = Any (),
55
54
) : IEventNotifier<IModelChangedHandler> {
56
55
/* *
57
56
* A unique identifier for this model.
@@ -80,33 +79,43 @@ open class Model(
80
79
* @param jsonObject The [JSONObject] to initialize this model from.
81
80
*/
82
81
fun initializeFromJson (jsonObject : JSONObject ) {
83
- data.clear()
84
- for (property in jsonObject.keys()) {
85
- val jsonValue = jsonObject.get(property)
86
- if (jsonValue is JSONObject ) {
87
- val childModel = createModelForProperty(property, jsonValue)
88
- if (childModel != null ) {
89
- data[property] = childModel
90
- }
91
- } else if (jsonValue is JSONArray ) {
92
- val listOfItems = createListForProperty(property, jsonValue)
93
- if (listOfItems != null ) {
94
- data[property] = listOfItems
95
- }
96
- } else {
97
- val method = this .javaClass.methods.firstOrNull { it.returnType != Void ::class .java && it.name.contains(property, true ) }
98
-
99
- if (method == null ) {
100
- data[property] = jsonObject.get(property)
82
+ synchronized(data) {
83
+ data.clear()
84
+ for (property in jsonObject.keys()) {
85
+ val jsonValue = jsonObject.get(property)
86
+ if (jsonValue is JSONObject ) {
87
+ val childModel = createModelForProperty(property, jsonValue)
88
+ if (childModel != null ) {
89
+ data[property] = childModel
90
+ }
91
+ } else if (jsonValue is JSONArray ) {
92
+ val listOfItems = createListForProperty(property, jsonValue)
93
+ if (listOfItems != null ) {
94
+ data[property] = listOfItems
95
+ }
101
96
} else {
102
- when (method.returnType) {
103
- Double ::class .java, java.lang.Double ::class .java -> data[property] = jsonObject.getDouble(property)
104
- Long ::class .java, java.lang.Long ::class .java -> data[property] = jsonObject.getLong(property)
105
- Float ::class .java, java.lang.Float ::class .java -> data[property] = jsonObject.getDouble(property).toFloat()
106
- Int ::class .java, java.lang.Integer ::class .java -> data[property] = jsonObject.getInt(property)
107
- Boolean ::class .java, java.lang.Boolean ::class .java -> data[property] = jsonObject.getBoolean(property)
108
- String ::class .java, java.lang.String ::class .java -> data[property] = jsonObject.getString(property)
109
- else -> data[property] = jsonObject.get(property)
97
+ val method =
98
+ this .javaClass.methods.firstOrNull {
99
+ it.returnType !=
100
+ Void ::class .java &&
101
+ it.name.contains(
102
+ property,
103
+ true ,
104
+ )
105
+ }
106
+
107
+ if (method == null ) {
108
+ data[property] = jsonObject.get(property)
109
+ } else {
110
+ when (method.returnType) {
111
+ Double ::class .java, java.lang.Double ::class .java -> data[property] = jsonObject.getDouble(property)
112
+ Long ::class .java, java.lang.Long ::class .java -> data[property] = jsonObject.getLong(property)
113
+ Float ::class .java, java.lang.Float ::class .java -> data[property] = jsonObject.getDouble(property).toFloat()
114
+ Int ::class .java, java.lang.Integer ::class .java -> data[property] = jsonObject.getInt(property)
115
+ Boolean ::class .java, java.lang.Boolean ::class .java -> data[property] = jsonObject.getBoolean(property)
116
+ String ::class .java, java.lang.String ::class .java -> data[property] = jsonObject.getString(property)
117
+ else -> data[property] = jsonObject.get(property)
118
+ }
110
119
}
111
120
}
112
121
}
@@ -140,7 +149,7 @@ open class Model(
140
149
newData[::id.name] = id
141
150
}
142
151
143
- synchronized(initializationLock ) {
152
+ synchronized(data ) {
144
153
data.clear()
145
154
data.putAll(newData)
146
155
}
@@ -436,9 +445,8 @@ open class Model(
436
445
tag : String = ModelChangeTags .NORMAL ,
437
446
forceChange : Boolean = false,
438
447
) {
448
+ val oldValue = data[name]
439
449
synchronized(data) {
440
- val oldValue = data[name]
441
-
442
450
if (oldValue == value && ! forceChange) {
443
451
return
444
452
}
@@ -448,9 +456,8 @@ open class Model(
448
456
} else if (data.containsKey(name)) {
449
457
data.remove(name)
450
458
}
451
-
452
- notifyChanged(name, name, tag, oldValue, value)
453
459
}
460
+ notifyChanged(name, name, tag, oldValue, value)
454
461
}
455
462
456
463
/* *
@@ -671,13 +678,14 @@ open class Model(
671
678
* @return The resulting [JSONObject].
672
679
*/
673
680
fun toJSON (): JSONObject {
674
- synchronized(initializationLock) {
675
- val jsonObject = JSONObject ()
681
+ val jsonObject = JSONObject ()
682
+ synchronized(data) {
676
683
for (kvp in data) {
677
684
when (val value = kvp.value) {
678
685
is Model -> {
679
686
jsonObject.put(kvp.key, value.toJSON())
680
687
}
688
+
681
689
is List <* > -> {
682
690
val jsonArray = JSONArray ()
683
691
for (arrayItem in value) {
@@ -689,13 +697,14 @@ open class Model(
689
697
}
690
698
jsonObject.put(kvp.key, jsonArray)
691
699
}
700
+
692
701
else -> {
693
702
jsonObject.put(kvp.key, value)
694
703
}
695
704
}
696
705
}
697
- return jsonObject
698
706
}
707
+ return jsonObject
699
708
}
700
709
701
710
override fun subscribe (handler : IModelChangedHandler ) = changeNotifier.subscribe(handler)
0 commit comments