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 /** Immutable list with index-based access. */ 20 sealed class IndexedList<T>(internal val list: ArrayList<T>) : Immutable<MutableIndexedList<T>> { 21 val size: Int 22 get() = list.size 23 isEmptynull24 fun isEmpty(): Boolean = list.isEmpty() 25 26 operator fun contains(element: T): Boolean = list.contains(element) 27 28 @Suppress("ReplaceGetOrSet") operator fun get(index: Int): T = list.get(index) 29 30 override fun toMutable(): MutableIndexedList<T> = MutableIndexedList(this) 31 32 override fun toString(): String = list.toString() 33 } 34 35 /** Mutable list with index-based access. */ 36 class MutableIndexedList<T>(list: ArrayList<T> = ArrayList()) : IndexedList<T>(list) { 37 constructor(indexedList: IndexedList<T>) : this(ArrayList(indexedList.list)) 38 39 @Suppress("ReplaceGetOrSet") 40 operator fun set(index: Int, element: T): T = list.set(index, element) 41 42 fun add(element: T) { 43 list.add(element) 44 } 45 46 fun add(index: Int, element: T) { 47 list.add(index, element) 48 } 49 50 fun remove(element: T) { 51 list.remove(element) 52 } 53 54 fun clear() { 55 list.clear() 56 } 57 58 fun removeAt(index: Int): T = list.removeAt(index) 59 } 60