Repository: corona-warn-app/cwa-app-android
File: Corona-Warn-App/src/main/java/de/rki/coronawarnapp/dccreissuance/core/reissuer/DccReissuer.kt (Kotlin)

Code before:
    private val dccQrCodeExtractor: DccQrCodeExtractor,
) {

    /**
     * Requests a new certificate, registers it and moves certificates into the recycle bin as indicated
     */
    @Throws(
        DccReissuanceException::class,
        InvalidHealthCertificateException::class
    )
    suspend fun startReissuance(certificateReissuance: CertificateReissuance) {
        val updates = certificateReissuance.asCertificateReissuanceCompat().certificates?.map {
            reissue(it)
        }
        updates?.map {
            it.recycleBin
        }?.flatten()?.toSet()?.forEach {
            moveToBin(it)
        }

        updates?.map {
            it.register
        }?.flatten()?.toSet()?.forEach {
            register(it)
        }
    }

    suspend fun reissue(item: CertificateReissuanceItem): CertificateUpdate {
        val recycleBin = mutableSetOf<QrCodeString>()
        val register = mutableSetOf<QrCodeString>()
        val allQrCodes = mutableListOf(item.certificateToReissue) + item.accompanyingCertificates

        val response = dccReissuanceServer.requestDccReissuance(
            action = item.action,
            certificates = allQrCodes.map { it.certificateRef.barcodeData }
        )

        response.dccReissuances.forEach { issuance ->
            register.add(issuance.certificate)
            issuance.relations.filter { relation ->
                relation.action == ACTION_REPLACE
            }.forEach {
                try {
                    recycleBin.add(allQrCodes[it.index].certificateRef.barcodeData)
                } catch (e: IndexOutOfBoundsException) {
                    Timber.d(e, "No certificate at index ${it.index}. Size is ${allQrCodes.size}")
                    throw DccReissuanceException(DccReissuanceException.ErrorCode.DCC_RI_SERVER_ERR)
                }
            }
        }


Review comment (suggestion):
`flatMap` ? with default emptySet of updates I think it can be enhanced a bit 

```Kotlin
       val updates =  ...}.orEmpty()

        updates.flatMap {
            it.recycleBin
        }.toSet().forEach {
            moveToBin(it)
        }
        
        updates.flatMap {
            it.register
        }.toSet().forEach {
            register(it)
        }
```

Code after:
    private val dccQrCodeExtractor: DccQrCodeExtractor,
) {

    /**
     * Requests a new certificate, registers it and moves certificates into the recycle bin as indicated
     */
    @Throws(
        DccReissuanceException::class,
        InvalidHealthCertificateException::class
    )
    suspend fun startReissuance(certificateReissuance: CertificateReissuance) {
        val updates = certificateReissuance.asCertificateReissuanceCompat().certificates?.map {
            reissue(it)
        }.orEmpty()

        updates.flatMap {
            it.recycleBin
        }.toSet().forEach {
            moveToBin(it)
        }

        updates.flatMap {
            it.register
        }.toSet().forEach {
            register(it)
        }
    }

    suspend fun reissue(item: CertificateReissuanceItem): CertificateUpdate {
        val recycleBin = mutableSetOf<QrCodeString>()
        val register = mutableSetOf<QrCodeString>()
        val allQrCodes = mutableListOf(item.certificateToReissue) + item.accompanyingCertificates

        val response = dccReissuanceServer.requestDccReissuance(
            action = item.action,
            certificates = allQrCodes.map { it.certificateRef.barcodeData }
        )

        response.dccReissuances.forEach { issuance ->
            register.add(issuance.certificate)
            issuance.relations.filter { relation ->
                relation.action == ACTION_REPLACE
            }.forEach {
                try {
                    recycleBin.add(allQrCodes[it.index].certificateRef.barcodeData)
                } catch (e: IndexOutOfBoundsException) {
                    Timber.d(e, "No certificate at index ${it.index}. Size is ${allQrCodes.size}")
                    throw DccReissuanceException(DccReissuanceException.ErrorCode.DCC_RI_SERVER_ERR)
                }
            }
        }