fix "list modification count"

GitOrigin-RevId: a28e00e2be6e1ddad11c48c644c61cb355a4a6e3
This commit is contained in:
Vladimir Krivosheev
2021-02-03 19:06:39 +01:00
committed by intellij-monorepo-bot
parent c02fe09398
commit 00326f1ec8
2 changed files with 72 additions and 4 deletions

View File

@@ -1,6 +1,5 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
@file:Suppress("UsePropertyAccessSyntax")
package com.intellij.configurationStore
import com.intellij.openapi.components.BaseState
@@ -188,6 +187,54 @@ class StoredPropertyStateTest {
assertThat(state.modificationCount).isNotEqualTo(oldModificationCount)
assertThat(state.isEqualToDefault()).isFalse()
}
@Test
fun `list modification count`() {
class TestOptions : BaseState() {
@get:XMap
val foo by list<NestedState>()
}
val state = TestOptions()
var oldModificationCount = state.modificationCount
val list = state.foo
list.clear()
list.add(NestedState())
list.add(NestedState())
assertThat(state.modificationCount).isNotEqualTo(oldModificationCount)
assertThat(state.isEqualToDefault()).isFalse()
val element = serialize(state)
assertThat(element).isEqualTo("""
<TestOptions>
<foo>
<list>
<NestedState />
<NestedState />
</list>
</foo>
</TestOptions>""")
oldModificationCount = state.modificationCount
list.clear()
assertThat(state.modificationCount).isNotEqualTo(oldModificationCount)
assertThat(state.isEqualToDefault()).isTrue()
val firstElement = NestedState()
list.add(firstElement)
list.add(NestedState())
oldModificationCount = state.modificationCount
firstElement.childProperty = "33"
assertThat(state.modificationCount).isNotEqualTo(oldModificationCount)
assertThat(state.isEqualToDefault()).isFalse()
oldModificationCount = state.modificationCount
assertThat(list.remove(firstElement)).isTrue()
assertThat(state.modificationCount).isNotEqualTo(oldModificationCount)
assertThat(state.isEqualToDefault()).isFalse()
}
}
internal class AState(languageLevel: String? = null, nestedComplex: NestedState? = null) : BaseState() {

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.serialization.stateProperties
import com.intellij.openapi.components.BaseState
@@ -53,5 +53,26 @@ open class CollectionStoredProperty<E : Any, C : MutableCollection<E>>(protected
}
internal class ListStoredProperty<T : Any> : CollectionStoredProperty<T, SmartList<T>>(SmartList(), null) {
override fun getModificationCount() = value.modificationCount.toLong()
private var modCount = 0L
private var lastItemModCount = 0L
override fun getModificationCount(): Long {
modCount = value.modificationCount.toLong()
var itemModCount = 0L
for (item in value) {
if (item is BaseState) {
itemModCount += item.modificationCount
}
else {
// or all values are BaseState or not
return modCount
}
}
if (itemModCount != lastItemModCount) {
lastItemModCount = itemModCount
modCount++
}
return modCount
}
}