Skip to content

Commit 9bf6018

Browse files
committed
cont thread safety
1 parent bc3dde4 commit 9bf6018

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

VCL/src/main/java/io/velocitycareerlabs/impl/data/usecases/CredentialTypeSchemasUseCaseImpl.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,29 @@ import io.velocitycareerlabs.impl.domain.infrastructure.executors.Executor
1212
import io.velocitycareerlabs.impl.domain.repositories.CredentialTypeSchemaRepository
1313
import io.velocitycareerlabs.impl.domain.usecases.CredentialTypeSchemasUseCase
1414
import io.velocitycareerlabs.impl.utils.VCLLog
15+
import java.util.Collections
1516
import java.util.concurrent.CompletableFuture
1617
import java.util.concurrent.ConcurrentHashMap
1718

19+
class CredentialTypeSchemasMapStorage {
20+
private val credentialTypeSchemasMap: MutableMap<String, VCLCredentialTypeSchema> =
21+
Collections.synchronizedMap(mutableMapOf())
22+
23+
fun add(schemaName: String, schema: VCLCredentialTypeSchema) {
24+
credentialTypeSchemasMap[schemaName] = schema
25+
}
26+
27+
fun isEmpty(): Boolean {
28+
return credentialTypeSchemasMap.isEmpty()
29+
}
30+
31+
fun get(): Map<String, VCLCredentialTypeSchema> {
32+
// Return a snapshot to avoid exposing live mutable state
33+
synchronized(credentialTypeSchemasMap) {
34+
return HashMap(credentialTypeSchemasMap)
35+
}
36+
}
37+
}
1838
internal class CredentialTypeSchemasUseCaseImpl (
1939
private val credentialTypeSchemaRepository: CredentialTypeSchemaRepository,
2040
private val credentialTypes: VCLCredentialTypes,
@@ -23,12 +43,13 @@ internal class CredentialTypeSchemasUseCaseImpl (
2343

2444
private val TAG = CredentialTypeSchemasUseCaseImpl::class.simpleName
2545

46+
private val credentialTypeSchemasStorage = CredentialTypeSchemasMapStorage()
47+
2648
override fun getCredentialTypeSchemas(
2749
cacheSequence: Int,
2850
completionBlock: (VCLResult<VCLCredentialTypeSchemas>) -> Unit
2951
) {
3052
executor.runOnBackground {
31-
val credentialTypeSchemasMap = ConcurrentHashMap<String, VCLCredentialTypeSchema>()
3253

3354
val schemaNamesArr =
3455
credentialTypes.all?.filter { it.schemaName != null }?.map { it.schemaName }
@@ -41,7 +62,7 @@ internal class CredentialTypeSchemasUseCaseImpl (
4162
schemaName,
4263
cacheSequence
4364
) { result ->
44-
result.data?.let { credentialTypeSchemasMap[schemaName] = it }
65+
result.data?.let { credentialTypeSchemasStorage.add(schemaName, it) }
4566
}
4667
}
4768
}
@@ -50,12 +71,12 @@ internal class CredentialTypeSchemasUseCaseImpl (
5071
val allFutures = CompletableFuture.allOf(*completableFutures.toTypedArray())
5172
allFutures.join()
5273

53-
if (credentialTypeSchemasMap.isEmpty()) {
74+
if (credentialTypeSchemasStorage.isEmpty()) {
5475
VCLLog.e(TAG, "Credential type schemas were not fount.")
5576
} else {
5677
executor.runOnMain {
5778
completionBlock(VCLResult.Success(
58-
VCLCredentialTypeSchemas(credentialTypeSchemasMap)
79+
VCLCredentialTypeSchemas(credentialTypeSchemasStorage.get())
5980
))
6081
}
6182
}

0 commit comments

Comments
 (0)