@@ -12,9 +12,29 @@ import io.velocitycareerlabs.impl.domain.infrastructure.executors.Executor
1212import io.velocitycareerlabs.impl.domain.repositories.CredentialTypeSchemaRepository
1313import io.velocitycareerlabs.impl.domain.usecases.CredentialTypeSchemasUseCase
1414import io.velocitycareerlabs.impl.utils.VCLLog
15+ import java.util.Collections
1516import java.util.concurrent.CompletableFuture
1617import 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+ }
1838internal 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