|
| 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 | +import kotlin.Any |
| 11 | +import kotlin.Boolean |
| 12 | +import kotlin.IllegalStateException |
| 13 | +import kotlin.Int |
| 14 | +import kotlin.String |
| 15 | + |
| 16 | +public class YogaValue |
| 17 | +public constructor(@JvmField public val value: Float, @JvmField public val unit: YogaUnit) { |
| 18 | + internal constructor(value: Float, unit: Int) : this(value, YogaUnit.fromInt(unit)) |
| 19 | + |
| 20 | + override fun equals(other: Any?): Boolean { |
| 21 | + if (other is YogaValue) { |
| 22 | + val otherValue = other |
| 23 | + if (unit == otherValue.unit) { |
| 24 | + return unit == YogaUnit.UNDEFINED || |
| 25 | + unit == YogaUnit.AUTO || |
| 26 | + value.compareTo(otherValue.value) == 0 |
| 27 | + } |
| 28 | + } |
| 29 | + return false |
| 30 | + } |
| 31 | + |
| 32 | + override fun hashCode(): Int = java.lang.Float.floatToIntBits(value) + unit.intValue() |
| 33 | + |
| 34 | + override fun toString(): String = |
| 35 | + when (unit) { |
| 36 | + YogaUnit.UNDEFINED -> "undefined" |
| 37 | + YogaUnit.POINT -> value.toString() |
| 38 | + YogaUnit.PERCENT -> "$value%" |
| 39 | + YogaUnit.AUTO -> "auto" |
| 40 | + else -> throw IllegalStateException() |
| 41 | + } |
| 42 | + |
| 43 | + public companion object { |
| 44 | + public val UNDEFINED: YogaValue = YogaValue(YogaConstants.UNDEFINED, YogaUnit.UNDEFINED) |
| 45 | + public val ZERO: YogaValue = YogaValue(0f, YogaUnit.POINT) |
| 46 | + public val AUTO: YogaValue = YogaValue(YogaConstants.UNDEFINED, YogaUnit.AUTO) |
| 47 | + |
| 48 | + public fun parse(s: String?): YogaValue? { |
| 49 | + if (s == null) { |
| 50 | + return null |
| 51 | + } |
| 52 | + |
| 53 | + if ("undefined" == s) { |
| 54 | + return UNDEFINED |
| 55 | + } |
| 56 | + |
| 57 | + if ("auto" == s) { |
| 58 | + return AUTO |
| 59 | + } |
| 60 | + |
| 61 | + if (s.endsWith("%")) { |
| 62 | + return YogaValue(s.substring(0, s.length - 1).toFloat(), YogaUnit.PERCENT) |
| 63 | + } |
| 64 | + |
| 65 | + return YogaValue(s.toFloat(), YogaUnit.POINT) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments