@@ -3,12 +3,13 @@ package com.segment.analytics.kotlin.android.utils
3
3
import android.content.SharedPreferences
4
4
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
5
5
import androidx.annotation.Nullable
6
+ import java.util.concurrent.ConcurrentHashMap
6
7
7
8
/* *
8
9
* Mock implementation of shared preference, which just saves data in memory using map.
9
10
*/
10
11
class MemorySharedPreferences : SharedPreferences {
11
- internal val preferenceMap: HashMap <String , Any ?> = HashMap ()
12
+ internal val preferenceMap: ConcurrentHashMap <String , Any ?> = ConcurrentHashMap ()
12
13
private val preferenceEditor: MockSharedPreferenceEditor
13
14
override fun getAll (): Map <String , * > {
14
15
return preferenceMap
@@ -74,18 +75,28 @@ class MemorySharedPreferences : SharedPreferences {
74
75
75
76
override fun registerOnSharedPreferenceChangeListener (onSharedPreferenceChangeListener : OnSharedPreferenceChangeListener ) {}
76
77
override fun unregisterOnSharedPreferenceChangeListener (onSharedPreferenceChangeListener : OnSharedPreferenceChangeListener ) {}
77
- class MockSharedPreferenceEditor (private val preferenceMap : HashMap <String , Any ?>) :
78
+ class MockSharedPreferenceEditor (private val preferenceMap : ConcurrentHashMap <String , Any ?>) :
78
79
SharedPreferences .Editor {
79
80
override fun putString (s : String , @Nullable s1 : String? ): SharedPreferences .Editor {
80
- preferenceMap[s] = s1
81
+ if (s1 == null ) {
82
+ remove(s)
83
+ }
84
+ else {
85
+ preferenceMap[s] = s1
86
+ }
81
87
return this
82
88
}
83
89
84
90
override fun putStringSet (
85
91
s : String ,
86
92
@Nullable set : Set <String >?
87
93
): SharedPreferences .Editor {
88
- preferenceMap[s] = set
94
+ if (set == null ) {
95
+ remove(s)
96
+ }
97
+ else {
98
+ preferenceMap[s] = set
99
+ }
89
100
return this
90
101
}
91
102
0 commit comments