taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

RequestManager.kt (1371B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2023 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet.backend
     18 
     19 import androidx.annotation.GuardedBy
     20 import java.util.concurrent.ConcurrentHashMap
     21 import kotlin.coroutines.Continuation
     22 
     23 class RequestManager {
     24 
     25     @GuardedBy("this")
     26     private val contMap = ConcurrentHashMap<Int, Continuation<ApiResponse>>()
     27 
     28     @Volatile
     29     @GuardedBy("this")
     30     private var currentId = 0
     31 
     32     @Synchronized
     33     fun addRequest(cont: Continuation<ApiResponse>, block: (Int) -> Unit) {
     34         val id = currentId++
     35         contMap[id] = cont
     36         block(id)
     37     }
     38 
     39     @Synchronized
     40     fun getAndRemoveContinuation(id: Int): Continuation<ApiResponse>? {
     41         return contMap.remove(id)
     42     }
     43 
     44 }