Move isUndefined check to CSSConstants in java code

This commit is contained in:
Krzysztof Magiera
2014-12-02 22:30:44 +00:00
parent edcc4b5944
commit fbd14263f9
5 changed files with 23 additions and 23 deletions

View File

@@ -24,7 +24,7 @@ function __transpileToJavaCommon(code) {
.replace(/trailing\[([^\]]+)\]/g, 'getTrailing($1)') .replace(/trailing\[([^\]]+)\]/g, 'getTrailing($1)')
.replace(/pos\[([^\]]+)\]/g, 'getPos($1)') .replace(/pos\[([^\]]+)\]/g, 'getPos($1)')
.replace(/dim\[([^\]]+)\]/g, 'getDim($1)') .replace(/dim\[([^\]]+)\]/g, 'getDim($1)')
.replace(/isUndefined/g, 'FloatUtil.isUndefined') .replace(/isUndefined/g, 'CSSConstants.isUndefined')
// Since Java doesn't store its attributes in arrays, we need to use setters/getters to access // Since Java doesn't store its attributes in arrays, we need to use setters/getters to access
// the appropriate layout/style fields // the appropriate layout/style fields

View File

@@ -11,4 +11,8 @@ package com.facebook.csslayout;
public class CSSConstants { public class CSSConstants {
public static final float UNDEFINED = Float.NaN; public static final float UNDEFINED = Float.NaN;
public static boolean isUndefined(float value) {
return Float.compare(value, UNDEFINED) == 0;
}
} }

View File

@@ -12,13 +12,9 @@ public class FloatUtil {
private static final float EPSILON = .00001f; private static final float EPSILON = .00001f;
public static boolean isUndefined(float f) {
return Float.compare(f, CSSConstants.UNDEFINED) == 0;
}
public static boolean floatsEqual(float f1, float f2) { public static boolean floatsEqual(float f1, float f2) {
if (isUndefined(f1)) { if (Float.isNaN(f1) || Float.isNaN(f2)) {
return isUndefined(f2); return Float.isNaN(f1) && Float.isNaN(f2);
} }
return Math.abs(f2 - f1) < EPSILON; return Math.abs(f2 - f1) < EPSILON;
} }

View File

@@ -116,16 +116,16 @@ public class LayoutEngine {
} }
private static boolean isDimDefined(CSSNode node, CSSFlexDirection axis) { private static boolean isDimDefined(CSSNode node, CSSFlexDirection axis) {
return !FloatUtil.isUndefined(getStyleDimension(node, getDim(axis))); return !CSSConstants.isUndefined(getStyleDimension(node, getDim(axis)));
} }
private static boolean isPosDefined(CSSNode node, PositionIndex position) { private static boolean isPosDefined(CSSNode node, PositionIndex position) {
return !FloatUtil.isUndefined(getStylePosition(node, position)); return !CSSConstants.isUndefined(getStylePosition(node, position));
} }
private static float getPosition(CSSNode node, PositionIndex position) { private static float getPosition(CSSNode node, PositionIndex position) {
float result = getStylePosition(node, position); float result = getStylePosition(node, position);
return FloatUtil.isUndefined(result) ? 0 : result; return CSSConstants.isUndefined(result) ? 0 : result;
} }
private static float getMargin(CSSNode node, PositionIndex position) { private static float getMargin(CSSNode node, PositionIndex position) {
@@ -189,7 +189,7 @@ public class LayoutEngine {
private static void setDimensionFromStyle(CSSNode node, CSSFlexDirection axis) { private static void setDimensionFromStyle(CSSNode node, CSSFlexDirection axis) {
// The parent already computed us a width or height. We just skip it // The parent already computed us a width or height. We just skip it
if (!FloatUtil.isUndefined(getLayoutDimension(node, getDim(axis)))) { if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(axis)))) {
return; return;
} }
// We only run if there's a width or height defined // We only run if there's a width or height defined
@@ -206,7 +206,7 @@ public class LayoutEngine {
private static float getRelativePosition(CSSNode node, CSSFlexDirection axis) { private static float getRelativePosition(CSSNode node, CSSFlexDirection axis) {
float lead = getStylePosition(node, getLeading(axis)); float lead = getStylePosition(node, getLeading(axis));
if (!FloatUtil.isUndefined(lead)) { if (!CSSConstants.isUndefined(lead)) {
return lead; return lead;
} }
return -getPosition(node, getTrailing(axis)); return -getPosition(node, getTrailing(axis));
@@ -299,7 +299,7 @@ public class LayoutEngine {
float width = CSSConstants.UNDEFINED; float width = CSSConstants.UNDEFINED;
if (isDimDefined(node, CSSFlexDirection.ROW)) { if (isDimDefined(node, CSSFlexDirection.ROW)) {
width = node.style.width; width = node.style.width;
} else if (!FloatUtil.isUndefined(getLayoutDimension(node, getDim(CSSFlexDirection.ROW)))) { } else if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(CSSFlexDirection.ROW)))) {
width = getLayoutDimension(node, getDim(CSSFlexDirection.ROW)); width = getLayoutDimension(node, getDim(CSSFlexDirection.ROW));
} else { } else {
width = parentMaxWidth - width = parentMaxWidth -
@@ -311,9 +311,9 @@ public class LayoutEngine {
// for it computed yet. It can either be from the style attribute or because // for it computed yet. It can either be from the style attribute or because
// the element is flexible. // the element is flexible.
boolean isRowUndefined = !isDimDefined(node, CSSFlexDirection.ROW) && boolean isRowUndefined = !isDimDefined(node, CSSFlexDirection.ROW) &&
FloatUtil.isUndefined(getLayoutDimension(node, getDim(CSSFlexDirection.ROW))); CSSConstants.isUndefined(getLayoutDimension(node, getDim(CSSFlexDirection.ROW)));
boolean isColumnUndefined = !isDimDefined(node, CSSFlexDirection.COLUMN) && boolean isColumnUndefined = !isDimDefined(node, CSSFlexDirection.COLUMN) &&
FloatUtil.isUndefined(getLayoutDimension(node, getDim(CSSFlexDirection.COLUMN))); CSSConstants.isUndefined(getLayoutDimension(node, getDim(CSSFlexDirection.COLUMN)));
// Let's not measure the text if we already know both dimensions // Let's not measure the text if we already know both dimensions
if (isRowUndefined || isColumnUndefined) { if (isRowUndefined || isColumnUndefined) {
@@ -339,7 +339,7 @@ public class LayoutEngine {
// we call the recursive layout pass // we call the recursive layout pass
if (getAlignItem(node, child) == CSSAlign.STRETCH && if (getAlignItem(node, child) == CSSAlign.STRETCH &&
getPositionType(child) == CSSPositionType.RELATIVE && getPositionType(child) == CSSPositionType.RELATIVE &&
!FloatUtil.isUndefined(getLayoutDimension(node, getDim(crossAxis))) && !CSSConstants.isUndefined(getLayoutDimension(node, getDim(crossAxis))) &&
!isDimDefined(child, crossAxis) && !isDimDefined(child, crossAxis) &&
!isPosDefined(child, getLeading(crossAxis))) { !isPosDefined(child, getLeading(crossAxis))) {
setLayoutDimension(child, getDim(crossAxis), Math.max( setLayoutDimension(child, getDim(crossAxis), Math.max(
@@ -354,7 +354,7 @@ public class LayoutEngine {
// left and right or top and bottom). // left and right or top and bottom).
for (int ii = 0; ii < 2; ii++) { for (int ii = 0; ii < 2; ii++) {
CSSFlexDirection axis = (ii != 0) ? CSSFlexDirection.ROW : CSSFlexDirection.COLUMN; CSSFlexDirection axis = (ii != 0) ? CSSFlexDirection.ROW : CSSFlexDirection.COLUMN;
if (!FloatUtil.isUndefined(getLayoutDimension(node, getDim(axis))) && if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(axis))) &&
!isDimDefined(child, axis) && !isDimDefined(child, axis) &&
isPosDefined(child, getLeading(axis)) && isPosDefined(child, getLeading(axis)) &&
isPosDefined(child, getTrailing(axis))) { isPosDefined(child, getTrailing(axis))) {
@@ -390,7 +390,7 @@ public class LayoutEngine {
// It only makes sense to consider a child flexible if we have a computed // It only makes sense to consider a child flexible if we have a computed
// dimension for the node. // dimension for the node.
if (!FloatUtil.isUndefined(getLayoutDimension(node, getDim(mainAxis))) && isFlex(child)) { if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis))) && isFlex(child)) {
flexibleChildrenCount++; flexibleChildrenCount++;
totalFlexible = totalFlexible + getFlex(child); totalFlexible = totalFlexible + getFlex(child);
@@ -438,7 +438,7 @@ public class LayoutEngine {
// If the dimensions of the current node is defined by its children, they // If the dimensions of the current node is defined by its children, they
// are all going to be packed together and we don't need to compute // are all going to be packed together and we don't need to compute
// anything. // anything.
if (!FloatUtil.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) { if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) {
// The remaining available space that needs to be allocated // The remaining available space that needs to be allocated
float remainingMainDim = getLayoutDimension(node, getDim(mainAxis)) - float remainingMainDim = getLayoutDimension(node, getDim(mainAxis)) -
getPaddingAndBorderAxis(node, mainAxis) - getPaddingAndBorderAxis(node, mainAxis) -
@@ -546,7 +546,7 @@ public class LayoutEngine {
// If the user didn't specify a width or height, and it has not been set // If the user didn't specify a width or height, and it has not been set
// by the container, then we set it via the children. // by the container, then we set it via the children.
if (FloatUtil.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) { if (CSSConstants.isUndefined(getLayoutDimension(node, getDim(mainAxis)))) {
setLayoutDimension(node, getDim(mainAxis), Math.max( setLayoutDimension(node, getDim(mainAxis), Math.max(
// We're missing the last padding at this point to get the final // We're missing the last padding at this point to get the final
// dimension // dimension
@@ -556,7 +556,7 @@ public class LayoutEngine {
)); ));
} }
if (FloatUtil.isUndefined(getLayoutDimension(node, getDim(crossAxis)))) { if (CSSConstants.isUndefined(getLayoutDimension(node, getDim(crossAxis)))) {
setLayoutDimension(node, getDim(crossAxis), Math.max( setLayoutDimension(node, getDim(crossAxis), Math.max(
// For the cross dim, we add both sides at the end because the value // For the cross dim, we add both sides at the end because the value
// is aggregate via a max function. Intermediate negative values // is aggregate via a max function. Intermediate negative values
@@ -631,7 +631,7 @@ public class LayoutEngine {
// left and right or top and bottom). // left and right or top and bottom).
for (int ii = 0; ii < 2; ii++) { for (int ii = 0; ii < 2; ii++) {
CSSFlexDirection axis = (ii != 0) ? CSSFlexDirection.ROW : CSSFlexDirection.COLUMN; CSSFlexDirection axis = (ii != 0) ? CSSFlexDirection.ROW : CSSFlexDirection.COLUMN;
if (!FloatUtil.isUndefined(getLayoutDimension(node, getDim(axis))) && if (!CSSConstants.isUndefined(getLayoutDimension(node, getDim(axis))) &&
!isDimDefined(child, axis) && !isDimDefined(child, axis) &&
isPosDefined(child, getLeading(axis)) && isPosDefined(child, getLeading(axis)) &&
isPosDefined(child, getTrailing(axis))) { isPosDefined(child, getTrailing(axis))) {

View File

@@ -21,7 +21,7 @@ public class LayoutEngineTest {
@Override @Override
public void measure(CSSNode node, float width, MeasureOutput measureOutput) { public void measure(CSSNode node, float width, MeasureOutput measureOutput) {
if (FloatUtil.isUndefined(width)) { if (CSSConstants.isUndefined(width)) {
width = 10000000; width = 10000000;
} }