Skip to content

Commit 4340dcb

Browse files
mateoguzmanafacebook-github-bot
authored andcommitted
Migrate YogaValue to Kotlin
Summary: Migrate com.facebook.yoga.YogaValue to Kotlin. X-link: facebook/yoga#1838 Reviewed By: rshest Differential Revision: D79897668 Pulled By: cortinico fbshipit-source-id: dffe2b29087c35e4797f46dea756c51f841590d8
1 parent 3d12d81 commit 4340dcb

File tree

2 files changed

+66
-80
lines changed

2 files changed

+66
-80
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaValue.java

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.yoga
9+
10+
public class YogaValue
11+
public constructor(@JvmField public val value: Float, @JvmField public val unit: YogaUnit) {
12+
internal constructor(value: Float, unit: Int) : this(value, YogaUnit.fromInt(unit))
13+
14+
override fun equals(other: Any?): Boolean {
15+
if (other is YogaValue) {
16+
val otherValue = other
17+
if (unit == otherValue.unit) {
18+
return unit == YogaUnit.UNDEFINED ||
19+
unit == YogaUnit.AUTO ||
20+
value.compareTo(otherValue.value) == 0
21+
}
22+
}
23+
return false
24+
}
25+
26+
override fun hashCode(): Int = java.lang.Float.floatToIntBits(value) + unit.intValue()
27+
28+
override fun toString(): String =
29+
when (unit) {
30+
YogaUnit.UNDEFINED -> "undefined"
31+
YogaUnit.POINT -> value.toString()
32+
YogaUnit.PERCENT -> "$value%"
33+
YogaUnit.AUTO -> "auto"
34+
else -> throw IllegalStateException()
35+
}
36+
37+
public companion object {
38+
@JvmField
39+
public val UNDEFINED: YogaValue = YogaValue(YogaConstants.UNDEFINED, YogaUnit.UNDEFINED)
40+
41+
@JvmField public val ZERO: YogaValue = YogaValue(0f, YogaUnit.POINT)
42+
43+
@JvmField public val AUTO: YogaValue = YogaValue(YogaConstants.UNDEFINED, YogaUnit.AUTO)
44+
45+
@JvmStatic
46+
public fun parse(s: String?): YogaValue? {
47+
if (s == null) {
48+
return null
49+
}
50+
51+
if ("undefined" == s) {
52+
return UNDEFINED
53+
}
54+
55+
if ("auto" == s) {
56+
return AUTO
57+
}
58+
59+
if (s.endsWith("%")) {
60+
return YogaValue(s.substring(0, s.length - 1).toFloat(), YogaUnit.PERCENT)
61+
}
62+
63+
return YogaValue(s.toFloat(), YogaUnit.POINT)
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)