1 /* 2 * Copyright 2024 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 @file:Suppress("BanConcurrentHashMap") 18 19 package androidx.xr.scenecore 20 21 import androidx.xr.runtime.internal.Entity as RtEntity 22 import java.util.concurrent.ConcurrentHashMap 23 24 /** Manages the mapping between [RuntimeEntity] and [Entity] for a given SceneCore [Session]. */ 25 internal class EntityManager { 26 private val rtEntityEntityMap = ConcurrentHashMap<RtEntity, Entity>() 27 28 /** 29 * Returns the [Entity] associated with the given [RtEntity]. 30 * 31 * @param rtEntity the [RtEntity] to get the associated [Entity] for. 32 * @return [java.util.Optional] containing the [Entity] associated with the given [RtEntity], or 33 * empty if no such [Entity] exists. 34 */ getEntityForRtEntitynull35 internal fun getEntityForRtEntity(rtEntity: RtEntity): Entity? = rtEntityEntityMap[rtEntity] 36 37 /** 38 * Sets the [Entity] associated with the given [RtEntity]. 39 * 40 * @param rtEntity the [RtEntity] to set the associated [Entity] for. 41 * @param entity the [Entity] to associate with the given [RtEntity]. 42 */ 43 internal fun setEntityForRtEntity(rtEntity: RtEntity, entity: Entity) { 44 rtEntityEntityMap[rtEntity] = entity 45 } 46 47 /** 48 * Inline function to get all entities of a given type. 49 * 50 * @param T the type of [Entity] to return. 51 * @return a list of all [Entity]s of type [T] (including subtypes of [T]). 52 */ getEntitiesnull53 internal inline fun <reified T : Entity> getEntities(): List<T> { 54 return rtEntityEntityMap.values.filterIsInstance<T>() 55 } 56 57 /** 58 * Returns all [Entity]s of the given type or its subtypes. 59 * 60 * @param type the type of [Entity] to return. 61 * @return a list of all [Entity]s of the given type or its subtypes. 62 */ getEntitiesOfTypenull63 internal fun <T : Entity> getEntitiesOfType(type: Class<out T>): List<T> = 64 rtEntityEntityMap.values.filterIsInstance(type) 65 66 /** 67 * Returns a collection of all [Entity]s. 68 * 69 * @return a collection of all [Entity]s. 70 */ 71 internal fun getAllEntities(): Collection<Entity> { 72 return rtEntityEntityMap.values 73 } 74 75 /** 76 * Removes the given [Entity] from the map. 77 * 78 * @param entity the [Entity] to remove from the map. 79 */ removeEntitynull80 internal fun removeEntity(entity: Entity) { 81 rtEntityEntityMap.remove((entity as BaseEntity<*>).rtEntity) 82 } 83 84 /** 85 * Removes the given [RtEntity] from the map. 86 * 87 * @param entity the [RtEntity] to remove from the map. 88 */ removeEntitynull89 internal fun removeEntity(entity: RtEntity) { 90 rtEntityEntityMap.remove(entity) 91 } 92 93 /** Clears the EntityManager. */ clearnull94 internal fun clear() { 95 rtEntityEntityMap.clear() 96 } 97 } 98