Add storage for user factors
authorVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Thu, 23 Nov 2017 06:10:04 +0000 (09:10 +0300)
committerVitaliy.Bibaev <vitaliy.bibaev@jetbrains.com>
Mon, 27 Nov 2017 16:04:24 +0000 (19:04 +0300)
plugins/stats-collector/src/com/intellij/stats/personalization/UserFactor.kt [new file with mode: 0644]
plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorStorage.kt [new file with mode: 0644]
plugins/stats-collector/src/com/intellij/stats/personalization/impl/ApplicationUserFactorStorage.kt [new file with mode: 0644]
plugins/stats-collector/src/com/intellij/stats/personalization/impl/ProjectUserFactorStorage.kt [new file with mode: 0644]
plugins/stats-collector/src/com/intellij/stats/personalization/impl/UserFactorStorageBase.kt [new file with mode: 0644]

diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/UserFactor.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/UserFactor.kt
new file mode 100644 (file)
index 0000000..11c2815
--- /dev/null
@@ -0,0 +1,13 @@
+package com.intellij.stats.personalization
+
+import com.intellij.codeInsight.lookup.LookupElement
+
+/**
+ * @author Vitaliy.Bibaev
+ */
+interface UserFactor {
+    val id: String
+
+    fun store(element: LookupElement)
+    fun extract(element: LookupElement): Comparable<Nothing>
+}
diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorStorage.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/UserFactorStorage.kt
new file mode 100644 (file)
index 0000000..bfd5fdd
--- /dev/null
@@ -0,0 +1,26 @@
+package com.intellij.stats.personalization
+
+import com.intellij.openapi.application.ApplicationManager
+import com.intellij.openapi.project.Project
+import com.intellij.stats.personalization.impl.ApplicationUserFactorStorage
+import com.intellij.stats.personalization.impl.ProjectUserFactorStorage
+
+/**
+ * @author Vitaliy.Bibaev
+ */
+interface UserFactorStorage {
+  companion object {
+    fun getInstance(): UserFactorStorage =
+        ApplicationManager.getApplication().getComponent(ApplicationUserFactorStorage::class.java)
+
+    fun getInstance(project: Project): UserFactorStorage = project.getComponent(ProjectUserFactorStorage::class.java)
+  }
+
+  fun getBoolean(factorId: String): Boolean?
+  fun getDouble(factorId: String): Double?
+  fun getString(factorId: String): String?
+
+  fun setBoolean(factorId: String, value: Boolean)
+  fun setDouble(factorId: String, value: Double)
+  fun setString(factorId: String, value: String)
+}
\ No newline at end of file
diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/impl/ApplicationUserFactorStorage.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/impl/ApplicationUserFactorStorage.kt
new file mode 100644 (file)
index 0000000..f587a02
--- /dev/null
@@ -0,0 +1,11 @@
+package com.intellij.stats.personalization.impl
+
+import com.intellij.openapi.components.ApplicationComponent
+import com.intellij.openapi.components.State
+import com.intellij.openapi.components.Storage
+
+/**
+ * @author Vitaliy.Bibaev
+ */
+@State(name = "ApplicationUserFactors", storages = arrayOf(Storage("completion.factors.user.xml")))
+class ApplicationUserFactorStorage : ApplicationComponent, UserFactorStorageBase()
\ No newline at end of file
diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/impl/ProjectUserFactorStorage.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/impl/ProjectUserFactorStorage.kt
new file mode 100644 (file)
index 0000000..f085a6d
--- /dev/null
@@ -0,0 +1,11 @@
+package com.intellij.stats.personalization.impl
+
+import com.intellij.openapi.components.ProjectComponent
+import com.intellij.openapi.components.State
+import com.intellij.openapi.components.Storage
+
+/**
+ * @author Vitaliy.Bibaev
+ */
+@State(name = "ProjectUserFactors", storages = arrayOf(Storage("completion.factors.user.xml")))
+class ProjectUserFactorStorage : ProjectComponent, UserFactorStorageBase()
\ No newline at end of file
diff --git a/plugins/stats-collector/src/com/intellij/stats/personalization/impl/UserFactorStorageBase.kt b/plugins/stats-collector/src/com/intellij/stats/personalization/impl/UserFactorStorageBase.kt
new file mode 100644 (file)
index 0000000..3b22f6a
--- /dev/null
@@ -0,0 +1,45 @@
+package com.intellij.stats.personalization.impl
+
+import com.intellij.openapi.components.PersistentStateComponent
+import com.intellij.stats.personalization.UserFactorStorage
+import com.intellij.util.xmlb.annotations.MapAnnotation
+import java.util.HashMap
+
+abstract class UserFactorStorageBase
+  : UserFactorStorage, PersistentStateComponent<UserFactorStorageBase.CollectorState> {
+
+  private var state = CollectorState()
+
+  override fun getBoolean(factorId: String): Boolean? = state.booleanFactors[factorId]
+
+  override fun getString(factorId: String): String? = state.stringFactors[factorId]
+
+  override fun getDouble(factorId: String): Double? = state.doubleFactors[factorId]
+
+  override fun setBoolean(factorId: String, value: Boolean) = state.booleanFactors.update(factorId, value)
+
+  override fun setDouble(factorId: String, value: Double) = state.doubleFactors.update(factorId, value)
+
+  override fun setString(factorId: String, value: String) = state.stringFactors.update(factorId, value)
+
+  private fun <T> MutableMap<String, T>.update(factorId: String, value: T) {
+    this[factorId] = value
+  }
+
+  override fun getState(): CollectorState = state
+
+  override fun loadState(newState: CollectorState) {
+    state = newState
+  }
+
+  class CollectorState {
+    @MapAnnotation(surroundKeyWithTag = false, keyAttributeName = "name", sortBeforeSave = true)
+    var booleanFactors: MutableMap<String, Boolean> = HashMap()
+
+    @MapAnnotation(surroundKeyWithTag = false, keyAttributeName = "name", sortBeforeSave = true)
+    var doubleFactors: MutableMap<String, Double> = HashMap()
+
+    @MapAnnotation(surroundKeyWithTag = false, keyAttributeName = "name", sortBeforeSave = true)
+    var stringFactors: MutableMap<String, String> = HashMap()
+  }
+}
\ No newline at end of file