Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd1366b8 authored by shamim-emon's avatar shamim-emon
Browse files

refactor: convert searchresponse class from java to kotlin

parent c91b85dc
Loading
Loading
Loading
Loading
+34 −41
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap;
package com.fsck.k9.mail.store.imap

internal class SearchResponse private constructor(
    /**
     * @return A list of numbers from the SEARCH response(s).
     */
    val numbers: List<Long>,
) {
    companion object {
        @JvmStatic
        fun parse(responses: List<ImapResponse>): SearchResponse {
            val numbers = mutableListOf<Long>()

import java.util.ArrayList;
import java.util.List;

import static com.fsck.k9.mail.store.imap.ImapResponseParser.equalsIgnoreCase;


class SearchResponse {
    private final List<Long> numbers;


    private SearchResponse(List<Long> numbers) {
        this.numbers = numbers;
    }

    public static SearchResponse parse(List<ImapResponse> responses) {
        List<Long> numbers = new ArrayList<>();

        for (ImapResponse response : responses) {
            parseSingleLine(response, numbers);
            for (response in responses) {
                parseSingleLine(response, numbers)
            }

        return new SearchResponse(numbers);
            return SearchResponse(numbers)
        }

    private static void parseSingleLine(ImapResponse response, List<Long> numbers) {
        if (response.isTagged() || response.size() < 2 || !equalsIgnoreCase(response.get(0), Responses.SEARCH)) {
            return;
        private fun parseSingleLine(response: ImapResponse, numbers: MutableList<Long>) {
            if (response.isTagged ||
                response.size < 2 ||
                !ImapResponseParser.equalsIgnoreCase(
                    response[0],
                    Responses.SEARCH,
                )
            ) {
                return
            }

        int end = response.size();
        for (int i = 1; i < end; i++) {
            val end = response.size
            for (i in 1..<end) {
                try {
                long number = response.getLong(i);
                numbers.add(number);
            } catch (NumberFormatException e) {
                return;
                    val number = response.getLong(i)
                    numbers.add(number)
                } catch (_: NumberFormatException) {
                    return
                }
            }
        }

    /**
     * @return A mutable list of numbers from the SEARCH response(s).
     */
    public List<Long> getNumbers() {
        return numbers;
    }
}
+55 −61
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap;
package com.fsck.k9.mail.store.imap

import com.fsck.k9.mail.store.imap.SearchResponse.Companion.parse
import org.junit.Assert
import kotlin.test.Test

import java.util.Collections;
import java.util.List;

import org.junit.Test;

import static com.fsck.k9.mail.store.imap.ImapResponseHelper.createImapResponseList;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


public class SearchResponseTest {
class SearchResponseTest {
    @Test
    public void parse_withSingleSearchResponse_shouldExtractNumbers() throws Exception {
        List<ImapResponse> imapResponses = createImapResponseList(
    fun parse_withSingleSearchResponse_shouldExtractNumbers() {
        val imapResponses = ImapResponseHelper.createImapResponseList(
            "* SEARCH 1 2 3",
            "* 23 EXISTS",
            "* SEARCH 4",
                "1 OK SEARCH completed");
            "1 OK SEARCH completed",
        )

        SearchResponse result = SearchResponse.parse(imapResponses);
        val result = parse(imapResponses)

        assertNotNull(result);
        assertEquals(asList(1L, 2L, 3L, 4L), result.getNumbers());
        Assert.assertNotNull(result)
        Assert.assertEquals(mutableListOf(1L, 2L, 3L, 4L), result.numbers)
    }

    @Test
    public void parse_withMultipleSearchResponses_shouldExtractNumbers() throws Exception {
        List<ImapResponse> imapResponses = createImapResponseList(
    fun parse_withMultipleSearchResponses_shouldExtractNumbers() {
        val imapResponses = ImapResponseHelper.createImapResponseList(
            "* SEARCH 1 2 3",
            "* 23 EXISTS",
            "* SEARCH 4",
@@ -39,51 +32,52 @@ public class SearchResponseTest {
            "* SEARCH 7",
            "2 OK SEARCH completed",
            "* SEARCH 8",
                "3 OK SEARCH completed");
            "3 OK SEARCH completed",
        )

        SearchResponse result = SearchResponse.parse(imapResponses);
        val result = parse(imapResponses)

        assertNotNull(result);
        assertEquals(asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), result.getNumbers());
        Assert.assertNotNull(result)
        Assert.assertEquals(mutableListOf(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), result.numbers)
    }

    @Test
    public void parse_withSingleTaggedSearchResponse_shouldReturnEmptyList() throws Exception {
        List<ImapResponse> imapResponses = createImapResponseList("x SEARCH 7 8 9");
    fun parse_withSingleTaggedSearchResponse_shouldReturnEmptyList() {
        val imapResponses = ImapResponseHelper.createImapResponseList("x SEARCH 7 8 9")

        SearchResponse result = SearchResponse.parse(imapResponses);
        val result = parse(imapResponses)

        assertNotNull(result);
        assertEquals(Collections.emptyList(), result.getNumbers());
        Assert.assertNotNull(result)
        Assert.assertEquals(emptyList<Any>(), result.numbers)
    }

    @Test
    public void parse_withSingleTooShortResponse_shouldReturnEmptyList() throws Exception {
        List<ImapResponse> imapResponses = createImapResponseList("* SEARCH");
    fun parse_withSingleTooShortResponse_shouldReturnEmptyList() {
        val imapResponses = ImapResponseHelper.createImapResponseList("* SEARCH")

        SearchResponse result = SearchResponse.parse(imapResponses);
        val result = parse(imapResponses)

        assertNotNull(result);
        assertEquals(Collections.emptyList(), result.getNumbers());
        Assert.assertNotNull(result)
        Assert.assertEquals(emptyList<Any>(), result.numbers)
    }

    @Test
    public void parse_withSingleNoSearchResponse_shouldReturnEmptyList() throws Exception {
        List<ImapResponse> imapResponses = createImapResponseList("* 23 EXPUNGE");
    fun parse_withSingleNoSearchResponse_shouldReturnEmptyList() {
        val imapResponses = ImapResponseHelper.createImapResponseList("* 23 EXPUNGE")

        SearchResponse result = SearchResponse.parse(imapResponses);
        val result = parse(imapResponses)

        assertNotNull(result);
        assertEquals(Collections.emptyList(), result.getNumbers());
        Assert.assertNotNull(result)
        Assert.assertEquals(emptyList<Any>(), result.numbers)
    }

    @Test
    public void parse_withSingleSearchResponseContainingInvalidNumber_shouldReturnEmptyList() throws Exception {
        List<ImapResponse> imapResponses = createImapResponseList("* SEARCH A");
    fun parse_withSingleSearchResponseContainingInvalidNumber_shouldReturnEmptyList() {
        val imapResponses = ImapResponseHelper.createImapResponseList("* SEARCH A")

        SearchResponse result = SearchResponse.parse(imapResponses);
        val result = parse(imapResponses)

        assertNotNull(result);
        assertEquals(Collections.emptyList(), result.getNumbers());
        Assert.assertNotNull(result)
        Assert.assertEquals(emptyList<Any>(), result.numbers)
    }
}