1 /* 2 * Copyright (C) 2023 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.immutable 18 19 import android.util.ArraySet 20 21 /** Immutable set with index-based access. */ 22 sealed class IndexedSet<T>(internal val set: ArraySet<T>) : Immutable<MutableIndexedSet<T>> { 23 val size: Int 24 get() = set.size 25 isEmptynull26 fun isEmpty(): Boolean = set.isEmpty() 27 28 operator fun contains(element: T): Boolean = set.contains(element) 29 30 fun indexOf(element: T): Int = set.indexOf(element) 31 32 fun elementAt(index: Int): T = set.elementAt(index) 33 34 override fun toMutable(): MutableIndexedSet<T> = MutableIndexedSet(this) 35 36 override fun toString(): String = set.toString() 37 } 38 39 /** Mutable set with index-based access. */ 40 class MutableIndexedSet<T>(set: ArraySet<T> = ArraySet()) : IndexedSet<T>(set) { 41 constructor(indexedSet: IndexedSet<T>) : this(ArraySet(indexedSet.set)) 42 43 fun add(element: T): Boolean = set.add(element) 44 45 fun remove(element: T): Boolean = set.remove(element) 46 47 fun clear() { 48 set.clear() 49 } 50 51 fun removeAt(index: Int): T = set.removeAt(index) 52 } 53