From e69a80f7f9c330bb0dfa3f72181f503b3982a714 Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Fri, 17 Mar 2017 13:21:10 -0700 Subject: [PATCH] Revert D4713847: [rn] Optimize ReactShadowNode by more manually converting between string and enums Differential Revision: D4713847 fbshipit-source-id: c1a60ad133ec010556c0b4c6e5fd39cdc0eab337 --- .../react/uimanager/LayoutShadowNode.java | 301 ++---------------- 1 file changed, 26 insertions(+), 275 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/LayoutShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/LayoutShadowNode.java index e57c9f6158e..3f5020e3ad4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/LayoutShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/LayoutShadowNode.java @@ -4,9 +4,9 @@ package com.facebook.react.uimanager; import javax.annotation.Nullable; +import java.util.Locale; import com.facebook.react.bridge.Dynamic; -import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.ReadableType; import com.facebook.yoga.YogaAlign; @@ -246,34 +246,9 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (flexDirection == null) { - setFlexDirection(YogaFlexDirection.COLUMN); - return; - } - - switch (flexDirection) { - case "column": { - setFlexDirection(YogaFlexDirection.COLUMN); - break; - } - case "column-reverse": { - setFlexDirection(YogaFlexDirection.COLUMN_REVERSE); - break; - } - case "row": { - setFlexDirection(YogaFlexDirection.ROW); - break; - } - case "row-reverse": { - setFlexDirection(YogaFlexDirection.ROW_REVERSE); - break; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for flexDirection: " + flexDirection); - } - } + setFlexDirection( + flexDirection == null ? YogaFlexDirection.COLUMN : YogaFlexDirection.valueOf( + flexDirection.toUpperCase(Locale.US).replace("-", "_"))); } @ReactProp(name = ViewProps.FLEX_WRAP) @@ -281,25 +256,12 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (flexWrap == null) { + if (flexWrap == null || flexWrap.equals("nowrap")) { setFlexWrap(YogaWrap.NO_WRAP); - return; - } - - switch (flexWrap) { - case "nowrap": { - setFlexWrap(YogaWrap.NO_WRAP); - break; - } - case "wrap": { - setFlexWrap(YogaWrap.WRAP); - break; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for flexWrap: " + flexWrap); - } + } else if (flexWrap.equals("wrap")) { + setFlexWrap(YogaWrap.WRAP); + } else { + throw new IllegalArgumentException("Unknown flexWrap value: " + flexWrap); } } @@ -308,50 +270,8 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (alignSelf == null) { - setAlignSelf(YogaAlign.AUTO); - return; - } - - switch (alignSelf) { - case "auto": { - setAlignSelf(YogaAlign.FLEX_START); - return; - } - case "flex-start": { - setAlignSelf(YogaAlign.CENTER); - return; - } - case "center": { - setAlignSelf(YogaAlign.CENTER); - return; - } - case "flex-end": { - setAlignSelf(YogaAlign.FLEX_END); - return; - } - case "stretch": { - setAlignSelf(YogaAlign.STRETCH); - return; - } - case "baseline": { - setAlignSelf(YogaAlign.BASELINE); - return; - } - case "space-between": { - setAlignSelf(YogaAlign.SPACE_BETWEEN); - return; - } - case "space-around": { - setAlignSelf(YogaAlign.SPACE_AROUND); - return; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for alignSelf: " + alignSelf); - } - } + setAlignSelf(alignSelf == null ? YogaAlign.AUTO : YogaAlign.valueOf( + alignSelf.toUpperCase(Locale.US).replace("-", "_"))); } @ReactProp(name = ViewProps.ALIGN_ITEMS) @@ -359,50 +279,9 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (alignItems == null) { - setAlignItems(YogaAlign.STRETCH); - return; - } - - switch (alignItems) { - case "auto": { - setAlignItems(YogaAlign.FLEX_START); - return; - } - case "flex-start": { - setAlignItems(YogaAlign.CENTER); - return; - } - case "center": { - setAlignItems(YogaAlign.CENTER); - return; - } - case "flex-end": { - setAlignItems(YogaAlign.FLEX_END); - return; - } - case "stretch": { - setAlignItems(YogaAlign.STRETCH); - return; - } - case "baseline": { - setAlignItems(YogaAlign.BASELINE); - return; - } - case "space-between": { - setAlignItems(YogaAlign.SPACE_BETWEEN); - return; - } - case "space-around": { - setAlignItems(YogaAlign.SPACE_AROUND); - return; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for alignItems: " + alignItems); - } - } + setAlignItems( + alignItems == null ? YogaAlign.STRETCH : YogaAlign.valueOf( + alignItems.toUpperCase(Locale.US).replace("-", "_"))); } @ReactProp(name = ViewProps.ALIGN_CONTENT) @@ -410,50 +289,9 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (alignContent == null) { - setAlignContent(YogaAlign.FLEX_START); - return; - } - - switch (alignContent) { - case "auto": { - setAlignContent(YogaAlign.FLEX_START); - return; - } - case "flex-start": { - setAlignContent(YogaAlign.CENTER); - return; - } - case "center": { - setAlignContent(YogaAlign.CENTER); - return; - } - case "flex-end": { - setAlignContent(YogaAlign.FLEX_END); - return; - } - case "stretch": { - setAlignContent(YogaAlign.STRETCH); - return; - } - case "baseline": { - setAlignContent(YogaAlign.BASELINE); - return; - } - case "space-between": { - setAlignContent(YogaAlign.SPACE_BETWEEN); - return; - } - case "space-around": { - setAlignContent(YogaAlign.SPACE_AROUND); - return; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for alignContent: " + alignContent); - } - } + setAlignContent( + alignContent == null ? YogaAlign.FLEX_START : YogaAlign.valueOf( + alignContent.toUpperCase(Locale.US).replace("-", "_"))); } @ReactProp(name = ViewProps.JUSTIFY_CONTENT) @@ -461,38 +299,8 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (justifyContent == null) { - setJustifyContent(YogaJustify.FLEX_START); - return; - } - - switch (justifyContent) { - case "flex-start": { - setJustifyContent(YogaJustify.FLEX_START); - break; - } - case "center": { - setJustifyContent(YogaJustify.CENTER); - break; - } - case "flex-end": { - setJustifyContent(YogaJustify.FLEX_END); - break; - } - case "space-between": { - setJustifyContent(YogaJustify.SPACE_BETWEEN); - break; - } - case "space-around": { - setJustifyContent(YogaJustify.SPACE_AROUND); - break; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for justifyContent: " + justifyContent); - } - } + setJustifyContent(justifyContent == null ? YogaJustify.FLEX_START : YogaJustify.valueOf( + justifyContent.toUpperCase(Locale.US).replace("-", "_"))); } @ReactProp(name = ViewProps.OVERFLOW) @@ -500,30 +308,8 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (overflow == null) { - setOverflow(YogaOverflow.VISIBLE); - return; - } - - switch (overflow) { - case "visible": { - setOverflow(YogaOverflow.VISIBLE); - break; - } - case "hidden": { - setOverflow(YogaOverflow.HIDDEN); - break; - } - case "scroll": { - setOverflow(YogaOverflow.SCROLL); - break; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for overflow: " + overflow); - } - } + setOverflow(overflow == null ? YogaOverflow.VISIBLE : YogaOverflow.valueOf( + overflow.toUpperCase(Locale.US).replace("-", "_"))); } @ReactProp(name = ViewProps.DISPLAY) @@ -531,26 +317,8 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (display == null) { - setDisplay(YogaDisplay.FLEX); - return; - } - - switch (display) { - case "flex": { - setDisplay(YogaDisplay.FLEX); - break; - } - case "none": { - setDisplay(YogaDisplay.NONE); - break; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for display: " + display); - } - } + setDisplay(display == null ? YogaDisplay.FLEX : YogaDisplay.valueOf( + display.toUpperCase(Locale.US).replace("-", "_"))); } @ReactPropGroup(names = { @@ -656,26 +424,9 @@ public class LayoutShadowNode extends ReactShadowNode { if (isVirtual()) { return; } - - if (position == null) { - setPositionType(YogaPositionType.RELATIVE); - return; - } - - switch (position) { - case "relative": { - setPositionType(YogaPositionType.RELATIVE); - break; - } - case "absolute": { - setPositionType(YogaPositionType.ABSOLUTE); - break; - } - default: { - throw new JSApplicationIllegalArgumentException( - "invalid value for position: " + position); - } - } + YogaPositionType positionType = position == null ? + YogaPositionType.RELATIVE : YogaPositionType.valueOf(position.toUpperCase(Locale.US)); + setPositionType(positionType); } @Override