/*
 * Copyright 2014-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.litho.sections;

import static android.support.v4.util.Pools.SynchronizedPool;

import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.util.Pools.Pool;
import com.facebook.litho.sections.annotations.GroupSectionSpec;
import java.util.ArrayList;
import java.util.List;

/**
 * Utility class that represents the children of a {@link GroupSectionSpec}. This is used to mimic
 * litho's usage of the Container class in {@link com.facebook.litho.annotations.LayoutSpec}'s API
 */
public class Children {

  private static final Pool<Builder> sBuildersPool = new SynchronizedPool<>(2);

  private List<Section> mSections;

  private Children() {
    mSections = new ArrayList<>();
  }

  public static Builder create() {
    Builder builder = sBuildersPool.acquire();
    if (builder == null) {
      builder = new Builder();
    }
    builder.init(new Children());

    return builder;
  }

  @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
  public List<Section> getChildren() {
    return mSections;
  }

  public static class Builder {

    private Children mChildren;

    private Builder() {}

    private void init(Children children) {
      mChildren = children;
    }

    public Builder child(@Nullable Section section) {
      verifyValidState();

      if (section != null) {
        mChildren.mSections.add(section.makeShallowCopy());
      }

      return this;
    }

    public Builder child(@Nullable List<Section> sectionList) {
      verifyValidState();

      if (sectionList == null || sectionList.isEmpty()) {
        return this;
      }

      for (int i = 0; i < sectionList.size(); i++) {
        Section section = sectionList.get(i);
        if (section != null) {
          mChildren.mSections.add(section.makeShallowCopy());
        }
      }

      return this;
    }

    public Builder child(@Nullable Section.Builder<?> sectionBuilder) {
      verifyValidState();

      if (sectionBuilder != null) {
        mChildren.mSections.add(sectionBuilder.build());
      }

      return this;
    }

    public Builder children(@Nullable List<Section.Builder<?>> sectionBuilderList) {
      verifyValidState();

      if (sectionBuilderList == null || sectionBuilderList.isEmpty()) {
        return this;
      }

      for (int i = 0; i < sectionBuilderList.size(); i++) {
        Section.Builder<?> sectionBuilder = sectionBuilderList.get(i);
        if (sectionBuilder != null) {
          mChildren.mSections.add(sectionBuilder.build());
        }
      }

      return this;
    }

    public Children build() {
      verifyValidState();

      Children children = mChildren;
      mChildren = null;
      sBuildersPool.release(this);

      return children;
    }

    private void verifyValidState() {
      if (mChildren == null) {
        throw new IllegalStateException(".build() call has been already made on this Builder.");
      }
    }
  }
}

@@ -640,71 +640,8 @@
 s;%0A%0A
-import static android.support.v4.util.Pools.SynchronizedPool;%0A%0A
 impo
@@ -680,16 +680,16 @@
 llable;%0A
+
 import a
@@ -737,51 +737,8 @@
 ng;%0A
-import android.support.v4.util.Pools.Pool;%0A
 impo
@@ -1085,89 +1085,8 @@
  %7B%0A%0A
-  private static final Pool%3CBuilder%3E sBuildersPool = new SynchronizedPool%3C%3E(2);%0A%0A
   pr
@@ -1223,168 +1223,28 @@
     
-Builder builder = sBuildersPool.acquire();%0A    if (builder == null) %7B%0A      builder = new Builder();%0A    %7D%0A    builder.init(new Children());%0A%0A    return builder
+return new Builder()
 ;%0A  
@@ -1425,24 +1425,30 @@
 private 
+final 
 Children
  mChildr
@@ -1443,95 +1443,8 @@
 dren
- mChildren;%0A%0A    private Builder() %7B%7D%0A%0A    private void init(Children children) %7B%0A     
  mCh
@@ -1452,31 +1452,31 @@
 ldren = 
-c
+new C
 hildren
-;%0A    %7D
+();
 %0A%0A    pu
@@ -1527,35 +1527,8 @@
 ) %7B%0A
-      verifyValidState();%0A%0A
     
@@ -1715,35 +1715,8 @@
 ) %7B%0A
-      verifyValidState();%0A%0A
     
@@ -2112,35 +2112,8 @@
 ) %7B%0A
-      verifyValidState();%0A%0A
     
@@ -2325,35 +2325,8 @@
 ) %7B%0A
-      verifyValidState();%0A%0A
     
@@ -2736,329 +2736,25 @@
     
-verifyValidState();%0A%0A      Children children = mChildren;%0A      mChildren = null;%0A      sBuildersPool.release(this);%0A%0A      return children;%0A    %7D%0A%0A    private void verifyValidState() %7B%0A      if (mChildren == null) %7B%0A        throw new IllegalStateException(%22.build() call has been already made on this Builder.%22);%0A      %7D
+return mChildren;
 %0A   

Remove pooling from Children