• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.permission.access.collection
18 
19 typealias IndexedList<T> = ArrayList<T>
20 
21 inline fun <T> IndexedList<T>.allIndexed(predicate: (Int, T) -> Boolean): Boolean {
22     forEachIndexed { index, element ->
23         if (!predicate(index, element)) {
24             return false
25         }
26     }
27     return true
28 }
29 
anyIndexednull30 inline fun <T> IndexedList<T>.anyIndexed(predicate: (Int, T) -> Boolean): Boolean {
31     forEachIndexed { index, element ->
32         if (predicate(index, element)) {
33             return true
34         }
35     }
36     return false
37 }
38 
39 @Suppress("NOTHING_TO_INLINE")
copynull40 inline fun <T> IndexedList<T>.copy(): IndexedList<T> = IndexedList(this)
41 
42 inline fun <T> IndexedList<T>.forEachIndexed(action: (Int, T) -> Unit) {
43     for (index in indices) {
44         action(index, this[index])
45     }
46 }
47 
forEachReversedIndexednull48 inline fun <T> IndexedList<T>.forEachReversedIndexed(action: (Int, T) -> Unit) {
49     for (index in lastIndex downTo 0) {
50         action(index, this[index])
51     }
52 }
53 
54 @Suppress("NOTHING_TO_INLINE")
minusnull55 inline operator fun <T> IndexedList<T>.minus(element: T): IndexedList<T> =
56     copy().apply { this -= element }
57 
58 @Suppress("NOTHING_TO_INLINE")
minusAssignnull59 inline operator fun <T> IndexedList<T>.minusAssign(element: T) {
60     remove(element)
61 }
62 
noneIndexednull63 inline fun <T> IndexedList<T>.noneIndexed(predicate: (Int, T) -> Boolean): Boolean {
64     forEachIndexed { index, element ->
65         if (predicate(index, element)) {
66             return false
67         }
68     }
69     return true
70 }
71 
72 @Suppress("NOTHING_TO_INLINE")
plusnull73 inline operator fun <T> IndexedList<T>.plus(element: T): IndexedList<T> =
74     copy().apply { this += element }
75 
76 @Suppress("NOTHING_TO_INLINE")
plusAssignnull77 inline operator fun <T> IndexedList<T>.plusAssign(element: T) {
78     add(element)
79 }
80 
removeAllIndexednull81 inline fun <T> IndexedList<T>.removeAllIndexed(predicate: (Int, T) -> Boolean): Boolean {
82     var isChanged = false
83     forEachReversedIndexed { index, element ->
84         if (predicate(index, element)) {
85             removeAt(index)
86             isChanged = true
87         }
88     }
89     return isChanged
90 }
91 
retainAllIndexednull92 inline fun <T> IndexedList<T>.retainAllIndexed(predicate: (Int, T) -> Boolean): Boolean {
93     var isChanged = false
94     forEachReversedIndexed { index, element ->
95         if (!predicate(index, element)) {
96             removeAt(index)
97             isChanged = true
98         }
99     }
100     return isChanged
101 }
102 
mapNotNullIndexednull103 inline fun <T, R> IndexedList<T>.mapNotNullIndexed(transform: (T) -> R?): IndexedList<R> =
104     IndexedList<R>().also { destination ->
105         forEachIndexed { _, element ->
106             transform(element)?.let { destination += it }
107         }
108     }
109