diff options
author | lucaslin <lucaslin@google.com> | 2021-02-01 15:22:56 +0800 |
---|---|---|
committer | lucaslin <lucaslin@google.com> | 2021-02-01 15:22:56 +0800 |
commit | a7af478653f62e5e3c210691eef814453d9a8c90 (patch) | |
tree | 1f8d4b956d3d3094edf15be5c4ea698a452bed77 | |
parent | 8e555572f01547fc2928cdf86d7ecec0dd0db3ac (diff) | |
download | net-a7af478653f62e5e3c210691eef814453d9a8c90.tar.gz |
Add common CollectionUtils.{any,all} methods.
Test: new tests for this
Change-Id: I482c958af499bf8cdc8824c9586e9da619cd1f39
-rw-r--r-- | common/framework/com/android/net/module/util/CollectionUtils.java | 22 | ||||
-rw-r--r-- | common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt | 54 |
2 files changed, 76 insertions, 0 deletions
diff --git a/common/framework/com/android/net/module/util/CollectionUtils.java b/common/framework/com/android/net/module/util/CollectionUtils.java index 74f738da..cb1e3e74 100644 --- a/common/framework/com/android/net/module/util/CollectionUtils.java +++ b/common/framework/com/android/net/module/util/CollectionUtils.java @@ -66,6 +66,28 @@ public final class CollectionUtils { } /** + * @return True if all elements satisfy the predicate, false otherwise. + * Note that means this always returns true for empty collections. + */ + public static <T> boolean all(@NonNull Collection<T> elem, @NonNull Predicate<T> predicate) { + for (final T e : elem) { + if (!predicate.test(e)) return false; + } + return true; + + } + /** + * @return True if any element satisfies the predicate, false otherwise. + * Note that means this always returns false for empty collections. + */ + public static <T> boolean any(@NonNull Collection<T> elem, @NonNull Predicate<T> predicate) { + for (final T e : elem) { + if (predicate.test(e)) return true; + } + return false; + } + + /** * @return True if there exists at least one element in the sparse array for which * condition {@code predicate} */ diff --git a/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt b/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt new file mode 100644 index 00000000..00077420 --- /dev/null +++ b/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2021 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.net.module.util + +import androidx.test.filters.SmallTest +import androidx.test.runner.AndroidJUnit4 +import org.junit.Test +import org.junit.runner.RunWith +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +@RunWith(AndroidJUnit4::class) +@SmallTest +class CollectionUtilsTest { + @Test + fun testAny() { + assertTrue(CollectionUtils.any(listOf("A", "B", "C", "D", "E")) { it == "E" }) + assertFalse(CollectionUtils.any(listOf("A", "B", "C", "D", "E")) { it == "F" }) + assertTrue(CollectionUtils.any(listOf("AA", "BBB")) { it.length >= 3 }) + assertFalse(CollectionUtils.any(listOf("A", "BB", "CCC")) { it.length >= 4 }) + assertFalse(CollectionUtils.any(listOf("A", "BB", "CCC")) { it.length < 0 }) + assertFalse(CollectionUtils.any(listOf<String>()) { true }) + assertFalse(CollectionUtils.any(listOf<String>()) { false }) + assertTrue(CollectionUtils.any(listOf("A")) { true }) + assertFalse(CollectionUtils.any(listOf("A")) { false }) + } + + @Test + fun testAll() { + assertFalse(CollectionUtils.all(listOf("A", "B", "C", "D", "E")) { it != "E" }) + assertTrue(CollectionUtils.all(listOf("A", "B", "C", "D", "E")) { it != "F" }) + assertFalse(CollectionUtils.all(listOf("A", "BB", "CCC")) { it.length > 2 }) + assertTrue(CollectionUtils.all(listOf("A", "BB", "CCC")) { it.length >= 1 }) + assertTrue(CollectionUtils.all(listOf("A", "BB", "CCC")) { it.length < 4 }) + assertTrue(CollectionUtils.all(listOf<String>()) { true }) + assertTrue(CollectionUtils.all(listOf<String>()) { false }) + assertTrue(CollectionUtils.all(listOf(1)) { true }) + assertFalse(CollectionUtils.all(listOf(1)) { false }) + } +} |