forked from facebook/litho
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert RenderUnitIdGenerator to Kotlin
Summary: As per title Reviewed By: zielinskimz Differential Revision: D45039980 fbshipit-source-id: 987b962e94db3e5ebfda75dc323d73f709b9af43
- Loading branch information
1 parent
c6e8566
commit 4349177
Showing
2 changed files
with
59 additions
and
74 deletions.
There are no files selected for viewing
74 changes: 0 additions & 74 deletions
74
litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.java
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
litho-core/src/main/java/com/facebook/litho/RenderUnitIdGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Int>() | ||
|
||
/** | ||
* 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) | ||
} | ||
} |