@@ -51,7 +51,6 @@ open class Model(
5151 * specified, must also specify [_parentModel]
5252 */
5353 private val _parentProperty : String? = null ,
54- private val initializationLock : Any = Any (),
5554) : IEventNotifier<IModelChangedHandler> {
5655 /* *
5756 * A unique identifier for this model.
@@ -80,33 +79,43 @@ open class Model(
8079 * @param jsonObject The [JSONObject] to initialize this model from.
8180 */
8281 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+ }
10196 } 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+ }
110119 }
111120 }
112121 }
@@ -140,7 +149,7 @@ open class Model(
140149 newData[::id.name] = id
141150 }
142151
143- synchronized(initializationLock ) {
152+ synchronized(data ) {
144153 data.clear()
145154 data.putAll(newData)
146155 }
@@ -436,9 +445,8 @@ open class Model(
436445 tag : String = ModelChangeTags .NORMAL ,
437446 forceChange : Boolean = false,
438447 ) {
448+ val oldValue = data[name]
439449 synchronized(data) {
440- val oldValue = data[name]
441-
442450 if (oldValue == value && ! forceChange) {
443451 return
444452 }
@@ -448,9 +456,8 @@ open class Model(
448456 } else if (data.containsKey(name)) {
449457 data.remove(name)
450458 }
451-
452- notifyChanged(name, name, tag, oldValue, value)
453459 }
460+ notifyChanged(name, name, tag, oldValue, value)
454461 }
455462
456463 /* *
@@ -671,13 +678,14 @@ open class Model(
671678 * @return The resulting [JSONObject].
672679 */
673680 fun toJSON (): JSONObject {
674- synchronized(initializationLock) {
675- val jsonObject = JSONObject ()
681+ val jsonObject = JSONObject ()
682+ synchronized(data) {
676683 for (kvp in data) {
677684 when (val value = kvp.value) {
678685 is Model -> {
679686 jsonObject.put(kvp.key, value.toJSON())
680687 }
688+
681689 is List <* > -> {
682690 val jsonArray = JSONArray ()
683691 for (arrayItem in value) {
@@ -689,13 +697,14 @@ open class Model(
689697 }
690698 jsonObject.put(kvp.key, jsonArray)
691699 }
700+
692701 else -> {
693702 jsonObject.put(kvp.key, value)
694703 }
695704 }
696705 }
697- return jsonObject
698706 }
707+ return jsonObject
699708 }
700709
701710 override fun subscribe (handler : IModelChangedHandler ) = changeNotifier.subscribe(handler)
0 commit comments