Repository: thunderbird/thunderbird-android
File: legacy/core/src/main/java/com/fsck/k9/autocrypt/AutocryptGossipHeaderParser.kt (Kotlin)

Code before:
package com.fsck.k9.autocrypt

import androidx.annotation.VisibleForTesting
import com.fsck.k9.mail.Part
import com.fsck.k9.mail.internet.MimeUtility
import net.thunderbird.core.logging.legacy.Log
import okio.ByteString.Companion.decodeBase64

internal class AutocryptGossipHeaderParser private constructor() {

    fun getAllAutocryptGossipHeaders(part: Part): List<AutocryptGossipHeader> {
        return parseAllAutocryptGossipHeaders(
            part.getHeader(AutocryptGossipHeader.AUTOCRYPT_GOSSIP_HEADER),
        )
    }

    @VisibleForTesting
    fun parseAutocryptGossipHeader(headerValue: String): AutocryptGossipHeader? {
        val parameters = MimeUtility.getAllHeaderParameters(headerValue).toMutableMap()

        val type = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_TYPE)
        val base64KeyData = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_KEY_DATA)
        val addr = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_ADDR)

        return when {
            type != null && type != AutocryptHeader.AUTOCRYPT_TYPE_1 -> {
                Log.e("autocrypt: unsupported type parameter %s", type)
                null
            }

            base64KeyData == null -> {
                Log.e("autocrypt: missing key parameter")
                null
            }

            base64KeyData.decodeBase64() == null -> {
                Log.e("autocrypt: error parsing base64 data")
                null
            }

            addr == null -> {

Review comment (suggestion):
The parsed code provides a different result. The method `mapNotNull` returns an instance of `ArrayList`, which can be modified later.

The original code uses `Collections.unmodifiableList` to prevent any kind of modification within the list by transforming the given `List` into a `UnmodifiableList`.

```suggestion
    fun getAllAutocryptGossipHeaders(part: Part): List<AutocryptGossipHeader> {
        val headers = part.getHeader(AutocryptGossipHeader.AUTOCRYPT_GOSSIP_HEADER)
        val autocryptHeaders = parseAllAutocryptGossipHeaders(headers)
        return Collections.unmodifiableList(autocryptHeaders)
    }
```

Code after:
package com.fsck.k9.autocrypt

import androidx.annotation.VisibleForTesting
import com.fsck.k9.mail.Part
import com.fsck.k9.mail.internet.MimeUtility
import java.util.Collections
import net.thunderbird.core.logging.legacy.Log
import okio.ByteString.Companion.decodeBase64

internal object AutocryptGossipHeaderParser {

    fun getAllAutocryptGossipHeaders(part: Part): List<AutocryptGossipHeader> {
        val headers = part.getHeader(AutocryptGossipHeader.AUTOCRYPT_GOSSIP_HEADER)
        val autocryptHeaders = parseAllAutocryptGossipHeaders(headers)
        return Collections.unmodifiableList(autocryptHeaders)
    }

    @VisibleForTesting
    fun parseAutocryptGossipHeader(headerValue: String): AutocryptGossipHeader? {
        val parameters = MimeUtility.getAllHeaderParameters(headerValue).toMutableMap()

        val type = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_TYPE)
        val base64KeyData = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_KEY_DATA)
        val addr = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_ADDR)
        val decodedKey = base64KeyData?.decodeBase64()

        return when {
            type != null && type != AutocryptHeader.AUTOCRYPT_TYPE_1 -> {
                Log.e("autocrypt: unsupported type parameter %s", type)
                null
            }

            base64KeyData == null -> {
                Log.e("autocrypt: missing key parameter")
                null
            }

            decodedKey == null -> {
                Log.e("autocrypt: error parsing base64 data")
                null
            }