Convert 'MessageIdGenerator' to Kotlin
-package com.fsck.k9.mail.internet;
+package com.fsck.k9.mail.internet
+import androidx.annotation.VisibleForTesting
+import com.fsck.k9.mail.Address
+import com.fsck.k9.mail.Message
+import java.util.Locale
+import java.util.UUID
-import java.util.Locale;
-import java.util.UUID;
+open class MessageIdGenerator @VisibleForTesting internal constructor() {
+ fun generateMessageId(message: Message): String {
+ val uuid = generateUuid()
+ val hostname = message.from.firstHostname ?: message.replyTo.firstHostname ?: "email.android.com"
-import androidx.annotation.VisibleForTesting;
-
-import com.fsck.k9.mail.Address;
-import com.fsck.k9.mail.Message;
-
-
-public class MessageIdGenerator {
- public static MessageIdGenerator getInstance() {
- return new MessageIdGenerator();
+ return "<$uuid@$hostname>"
}
@VisibleForTesting
- MessageIdGenerator() {
- }
-
- public String generateMessageId(Message message) {
- String hostname = null;
-
- Address[] from = message.getFrom();
- if (from != null && from.length >= 1) {
- hostname = from[0].getHostname();
- }
-
- if (hostname == null) {
- Address[] replyTo = message.getReplyTo();
- if (replyTo != null && replyTo.length >= 1) {
- hostname = replyTo[0].getHostname();
- }
+ protected open fun generateUuid(): String {
+ // We use upper case here to match Apple Mail Message-ID format (for privacy)
+ return UUID.randomUUID().toString().toUpperCase(Locale.US)
}
- if (hostname == null) {
- hostname = "email.android.com";
- }
+ private val Array<Address>?.firstHostname: String?
+ get() = this?.firstOrNull()?.hostname
- String uuid = generateUuid();
- return "<" + uuid + "@" + hostname + ">";
- }
-
- @VisibleForTesting
- protected String generateUuid() {
- // We use upper case here to match Apple Mail Message-ID format (for privacy)
- return UUID.randomUUID().toString().toUpperCase(Locale.US);
+ companion object {
+ @JvmStatic
+ fun getInstance(): MessageIdGenerator = MessageIdGenerator()
}
}
