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

Commit 261bbcfa authored by Chris Göllner's avatar Chris Göllner
Browse files

Create Collection extension function `containsExactly`

This allows to check whether certain elements exactly match the ones in
a collection, taking into account duplicates.

Test: ConvenienceExtensionsKtTest
Bug: 362720432
Flag: EXEMPT no behavior change
Change-Id: I541813f1ddb3735fc3c48df321eccc3d0f81004f
parent 798934e5
Loading
Loading
Loading
Loading
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class ConvenienceExtensionsKtTest : SysuiTestCase() {

    @Test
    fun containsExactly_notDuplicatedElements_allSame_returnsTrue() {
        val list = listOf(1, 2, 3)

        assertThat(list.containsExactly(2, 1, 3)).isTrue()
    }

    @Test
    fun containsExactly_duplicatedElements_allSame_returnsTrue() {
        val list = listOf(1, 1, 2, 3, 3)

        assertThat(list.containsExactly(1, 1, 2, 3, 3)).isTrue()
    }

    @Test
    fun containsExactly_duplicatedElements_sameButNotDuplicated_returnsFalse() {
        val list = listOf(1, 1, 2, 3, 3)

        assertThat(list.containsExactly(1, 2, 3)).isFalse()
    }

    @Test
    fun containsExactly_duplicatedElements_sameButNotSameAmount_returnsFalse() {
        val list = listOf(1, 1, 2, 3, 3)

        assertThat(list.containsExactly(1, 2, 2, 3, 3)).isFalse()
    }

    @Test
    fun eachCountMap_returnsExpectedCount() {
        val list = listOf(1, 3, 1, 3, 3, 3, 2)

        assertThat(list.eachCountMap()).isEqualTo(mapOf(1 to 2, 2 to 1, 3 to 4))
    }
}
+23 −3
Original line number Diff line number Diff line
@@ -25,9 +25,7 @@ import java.io.PrintWriter

/** [Sequence] that yields all of the direct children of this [ViewGroup] */
val ViewGroup.children
    get() = sequence {
        for (i in 0 until childCount) yield(getChildAt(i))
    }
    get() = sequence { for (i in 0 until childCount) yield(getChildAt(i)) }

/** Inclusive version of [Iterable.takeWhile] */
fun <T> Sequence<T>.takeUntil(pred: (T) -> Boolean): Sequence<T> = sequence {
@@ -62,3 +60,25 @@ val View.boundsOnScreen: Rect
fun <T> Lazy<T>.toKotlinLazy(): kotlin.Lazy<T> {
    return lazy { this.get() }
}

/**
 * Returns whether this [Collection] contains exactly all [elements].
 *
 * Order of elements is not taken into account, but multiplicity is. For example, an element
 * duplicated exactly 3 times in the parameter asserts that the element must likewise be duplicated
 * exactly 3 times in this [Collection].
 */
fun <T> Collection<T>.containsExactly(vararg elements: T): Boolean {
    return eachCountMap() == elements.asList().eachCountMap()
}

/**
 * Returns a map where keys are the distinct elements of the collection and values are their
 * corresponding counts.
 *
 * This is a convenient extension function for any [Collection] that allows you to easily count the
 * occurrences of each element.
 */
fun <T> Collection<T>.eachCountMap(): Map<T, Int> {
    return groupingBy { it }.eachCount()
}