@@ -80,33 +80,52 @@ open class Model(
80
80
* @param jsonObject The [JSONObject] to initialize this model from.
81
81
*/
82
82
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)
83
+ synchronized(data) {
84
+ data.clear()
85
+ for (property in jsonObject.keys()) {
86
+ val jsonValue = jsonObject.get(property)
87
+ if (jsonValue is JSONObject ) {
88
+ val childModel = createModelForProperty(property, jsonValue)
89
+ if (childModel != null ) {
90
+ data[property] = childModel
91
+ }
92
+ } else if (jsonValue is JSONArray ) {
93
+ val listOfItems = createListForProperty(property, jsonValue)
94
+ if (listOfItems != null ) {
95
+ data[property] = listOfItems
96
+ }
101
97
} 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)
98
+ val method = this .javaClass.methods.firstOrNull {
99
+ it.returnType != Void ::class .java && it.name.contains(
100
+ property,
101
+ true
102
+ )
103
+ }
104
+
105
+ if (method == null ) {
106
+ data[property] = jsonObject.get(property)
107
+ } else {
108
+ when (method.returnType) {
109
+ Double ::class .java, java.lang.Double ::class .java -> data[property] =
110
+ jsonObject.getDouble(property)
111
+
112
+ Long ::class .java, java.lang.Long ::class .java -> data[property] =
113
+ jsonObject.getLong(property)
114
+
115
+ Float ::class .java, java.lang.Float ::class .java -> data[property] =
116
+ jsonObject.getDouble(property).toFloat()
117
+
118
+ Int ::class .java, java.lang.Integer ::class .java -> data[property] =
119
+ jsonObject.getInt(property)
120
+
121
+ Boolean ::class .java, java.lang.Boolean ::class .java -> data[property] =
122
+ jsonObject.getBoolean(property)
123
+
124
+ String ::class .java, java.lang.String ::class .java -> data[property] =
125
+ jsonObject.getString(property)
126
+
127
+ else -> data[property] = jsonObject.get(property)
128
+ }
110
129
}
111
130
}
112
131
}
@@ -141,8 +160,10 @@ open class Model(
141
160
}
142
161
143
162
synchronized(initializationLock) {
144
- data.clear()
145
- data.putAll(newData)
163
+ synchronized(data) {
164
+ data.clear()
165
+ data.putAll(newData)
166
+ }
146
167
}
147
168
}
148
169
@@ -436,8 +457,8 @@ open class Model(
436
457
tag : String = ModelChangeTags .NORMAL ,
437
458
forceChange : Boolean = false,
438
459
) {
460
+ val oldValue = data[name]
439
461
synchronized(data) {
440
- val oldValue = data[name]
441
462
if (oldValue == value && ! forceChange) {
442
463
return
443
464
}
@@ -447,9 +468,8 @@ open class Model(
447
468
} else if (data.containsKey(name)) {
448
469
data.remove(name)
449
470
}
450
-
451
- notifyChanged(name, name, tag, oldValue, value)
452
471
}
472
+ notifyChanged(name, name, tag, oldValue, value)
453
473
}
454
474
455
475
/* *
@@ -672,24 +692,28 @@ open class Model(
672
692
fun toJSON (): JSONObject {
673
693
synchronized(initializationLock) {
674
694
val jsonObject = JSONObject ()
675
- for (kvp in data) {
676
- when (val value = kvp.value) {
677
- is Model -> {
678
- jsonObject.put(kvp.key, value.toJSON())
679
- }
680
- is List <* > -> {
681
- val jsonArray = JSONArray ()
682
- for (arrayItem in value) {
683
- if (arrayItem is Model ) {
684
- jsonArray.put(arrayItem.toJSON())
685
- } else {
686
- jsonArray.put(arrayItem)
695
+ synchronized(data) {
696
+ for (kvp in data) {
697
+ when (val value = kvp.value) {
698
+ is Model -> {
699
+ jsonObject.put(kvp.key, value.toJSON())
700
+ }
701
+
702
+ is List <* > -> {
703
+ val jsonArray = JSONArray ()
704
+ for (arrayItem in value) {
705
+ if (arrayItem is Model ) {
706
+ jsonArray.put(arrayItem.toJSON())
707
+ } else {
708
+ jsonArray.put(arrayItem)
709
+ }
687
710
}
711
+ jsonObject.put(kvp.key, jsonArray)
712
+ }
713
+
714
+ else -> {
715
+ jsonObject.put(kvp.key, value)
688
716
}
689
- jsonObject.put(kvp.key, jsonArray)
690
- }
691
- else -> {
692
- jsonObject.put(kvp.key, value)
693
717
}
694
718
}
695
719
}
0 commit comments