From 4349177badee81bf7911200f62b9818c869401b0 Mon Sep 17 00:00:00 2001 From: Fabio Carballo Date: Mon, 24 Apr 2023 10:33:49 -0700 Subject: [PATCH] Convert RenderUnitIdGenerator to Kotlin Summary: As per title Reviewed By: zielinskimz Differential Revision: D45039980 fbshipit-source-id: 987b962e94db3e5ebfda75dc323d73f709b9af43 --- .../facebook/litho/RenderUnitIdGenerator.java | 74 ------------------- .../facebook/litho/RenderUnitIdGenerator.kt | 59 +++++++++++++++ 2 files changed, 59 insertions(+), 74 deletions(-) delete mode 100644 litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.java create mode 100644 litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.kt diff --git a/litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.java b/litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.java deleted file mode 100644 index 07e880aa339..00000000000 --- a/litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * 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.facebook.litho; - -import androidx.annotation.GuardedBy; -import com.facebook.infer.annotation.Nullsafe; -import java.util.HashMap; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Class that handles generation of unique IDs for RenderUnits for a given ComponentTree. The ID - * generation uses the component key to create an ID, and creates unique IDs for any components - * under the same ComponentTree. - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class RenderUnitIdGenerator { - private final AtomicInteger mNextId = new AtomicInteger(1); - private final int mComponentTreeId; - - @GuardedBy("this") - private final HashMap mKeyToId = new HashMap<>(); - - public RenderUnitIdGenerator(int componentTreeId) { - mComponentTreeId = componentTreeId; - } - - /** Returns the ComponentTree ID that this ID-generator is linked with. */ - public int getComponentTreeId() { - return mComponentTreeId; - } - - /** - * Calculates a returns a unique ID for a given component key and output type. The IDs will be - * unique for components in the ComponentTree this ID generator is linked with. If an ID was - * already generated for a given component, the same ID will be returned. Otherwise, a new unique - * ID will be generated - * - * @param componentKey The component key - * @param type The output type @see OutputUnitType - */ - public long calculateLayoutOutputId(final String componentKey, final @OutputUnitType int type) { - return addTypeAndComponentTreeToId(getId(componentKey), type, mComponentTreeId); - } - - private synchronized int getId(String key) { - final Integer currentId = mKeyToId.get(key); - if (currentId != null) { - return currentId; - } - - int nextId = mNextId.getAndIncrement(); - mKeyToId.put(key, nextId); - return nextId; - } - - private static long addTypeAndComponentTreeToId( - final int id, final @OutputUnitType int type, final int componentTreeId) { - return (long) id | ((long) type) << 32 | ((long) componentTreeId) << 35; - } -} diff --git a/litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.kt b/litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.kt new file mode 100644 index 00000000000..aaab6caad7f --- /dev/null +++ b/litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.kt @@ -0,0 +1,59 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * 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.facebook.litho + +import androidx.annotation.GuardedBy +import java.util.HashMap +import java.util.concurrent.atomic.AtomicInteger + +/** + * Class that handles generation of unique IDs for RenderUnits for a given ComponentTree. The ID + * generation uses the component key to create an ID, and creates unique IDs for any components + * under the same ComponentTree. + */ +class RenderUnitIdGenerator( + /** Returns the ComponentTree ID that this ID-generator is linked with. */ + val componentTreeId: Int +) { + + private val nextId = AtomicInteger(1) + + @GuardedBy("this") private val keyToId = HashMap() + + /** + * Calculates a returns a unique ID for a given component key and output type. The IDs will be + * unique for components in the ComponentTree this ID generator is linked with. If an ID was + * already generated for a given component, the same ID will be returned. Otherwise, a new unique + * ID will be generated + * + * @param componentKey The component key + * @param type The output type @see OutputUnitType + */ + fun calculateLayoutOutputId(componentKey: String, @OutputUnitType type: Int): Long = + addTypeAndComponentTreeToId(getId(componentKey), type, componentTreeId) + + @Synchronized + private fun getId(key: String): Int = keyToId.getOrPut(key) { nextId.getAndIncrement() } + + companion object { + private fun addTypeAndComponentTreeToId( + id: Int, + @OutputUnitType type: Int, + componentTreeId: Int + ): Long = id.toLong() or (type.toLong() shl 32) or (componentTreeId.toLong() shl 35) + } +}