From 7104f7c8eb6c160a8d10badc08f9b981bb65e19c Mon Sep 17 00:00:00 2001 From: Andy Street Date: Wed, 29 Jul 2015 11:39:25 +0100 Subject: [PATCH] Lazy init children ArrayList in CSSNode, update removeChildAt to return removed child --- .../src/com/facebook/csslayout/CSSNode.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/java/src/com/facebook/csslayout/CSSNode.java b/src/java/src/com/facebook/csslayout/CSSNode.java index 10448d03566..064ec19315f 100644 --- a/src/java/src/com/facebook/csslayout/CSSNode.java +++ b/src/java/src/com/facebook/csslayout/CSSNode.java @@ -56,18 +56,17 @@ public static interface MeasureFunction { public int lineIndex = 0; - // 4 is kinda arbitrary, but the default of 10 seems really high for an average View. - private final ArrayList mChildren = new ArrayList(4); - + private @Nullable ArrayList mChildren; private @Nullable CSSNode mParent; private @Nullable MeasureFunction mMeasureFunction = null; private LayoutState mLayoutState = LayoutState.DIRTY; public int getChildCount() { - return mChildren.size(); + return mChildren == null ? 0 : mChildren.size(); } public CSSNode getChildAt(int i) { + Assertions.assertNotNull(mChildren); return mChildren.get(i); } @@ -75,15 +74,22 @@ public void addChildAt(CSSNode child, int i) { if (child.mParent != null) { throw new IllegalStateException("Child already has a parent, it must be removed first."); } + if (mChildren == null) { + // 4 is kinda arbitrary, but the default of 10 seems really high for an average View. + mChildren = new ArrayList<>(4); + } mChildren.add(i, child); child.mParent = this; dirty(); } - public void removeChildAt(int i) { - mChildren.remove(i).mParent = null; + public CSSNode removeChildAt(int i) { + Assertions.assertNotNull(mChildren); + CSSNode removed = mChildren.remove(i); + removed.mParent = null; dirty(); + return removed; } public @Nullable CSSNode getParent() { @@ -94,6 +100,7 @@ public void removeChildAt(int i) { * @return the index of the given child, or -1 if the child doesn't exist in this node. */ public int indexOf(CSSNode child) { + Assertions.assertNotNull(mChildren); return mChildren.indexOf(child); }